annotate lib/ttyname_r.c @ 15727:144db791c6fa

Ensure EBADF returns for socket functions on mingw. * lib/accept.c (rpl_accept): Fail with error EBADF if the file descriptor is invalid. * lib/bind.c (rpl_bind): Likewise. * lib/connect.c (rpl_connect): Likewise. * lib/getpeername.c (rpl_getpeername): Likewise. * lib/getsockname.c (rpl_getsockname): Likewise. * lib/getsockopt.c (rpl_getsockopt): Likewise. * lib/listen.c (rpl_listen): Likewise. * lib/recv.c (rpl_recv): Likewise. * lib/recvfrom.c (rpl_recvfrom): Likewise. * lib/send.c (rpl_send): Likewise. * lib/sendto.c (rpl_sendto): Likewise. * lib/setsockopt.c (rpl_setsockopt): Likewise. * lib/shutdown.c (rpl_shutdown): Likewise.
author Bruno Haible <bruno@clisp.org>
date Wed, 21 Sep 2011 00:20:59 +0200
parents 97fc9a21a8fb
children 8250f2777afc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13023
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Determine name of a terminal.
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2
14079
97fc9a21a8fb maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents: 13996
diff changeset
3 Copyright (C) 2010-2011 Free Software Foundation, Inc.
13023
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 (at your option) any later version.
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 /* Written by Bruno Haible <bruno@clisp.org>, 2010. */
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #include <config.h>
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include <unistd.h>
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <errno.h>
13266
ddd7bebf854c ttyname_r: Make it work on MacOS X 10.4 and Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13023
diff changeset
25 #include <limits.h>
13023
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <string.h>
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 int
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 ttyname_r (int fd, char *buf, size_t buflen)
13266
ddd7bebf854c ttyname_r: Make it work on MacOS X 10.4 and Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13023
diff changeset
30 #undef ttyname_r
13023
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 {
13268
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
32 /* When ttyname_r exists, use it. */
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
33 #if HAVE_TTYNAME_R
13266
ddd7bebf854c ttyname_r: Make it work on MacOS X 10.4 and Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13023
diff changeset
34 /* This code is multithread-safe. */
13996
2ff78debd520 ttyname_r: Work around bug on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents: 13268
diff changeset
35 /* On Solaris, ttyname_r always fails if buflen < 128. On OSF/1 5.1,
2ff78debd520 ttyname_r: Work around bug on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents: 13268
diff changeset
36 ttyname_r ignores the buffer size and assumes the buffer is large enough.
2ff78debd520 ttyname_r: Work around bug on OSF/1 5.1.
Bruno Haible <bruno@clisp.org>
parents: 13268
diff changeset
37 So provide a buffer that is large enough. */
13268
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
38 char largerbuf[512];
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
39 # if HAVE_POSIXDECL_TTYNAME_R
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
40 int err =
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
41 (buflen < sizeof (largerbuf)
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
42 ? ttyname_r (fd, largerbuf, sizeof (largerbuf))
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
43 : ttyname_r (fd, buf, buflen <= INT_MAX ? buflen : INT_MAX));
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
44 if (err != 0)
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
45 return err;
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
46 if (buflen < sizeof (largerbuf))
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
47 {
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
48 size_t namelen = strlen (largerbuf);
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
49 if (namelen > buflen)
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
50 return ERANGE;
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
51 memcpy (buf, largerbuf, namelen);
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
52 }
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
53 # else
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
54 char *name =
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
55 (buflen < sizeof (largerbuf)
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
56 ? ttyname_r (fd, largerbuf, sizeof (largerbuf))
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
57 : ttyname_r (fd, buf, buflen <= INT_MAX ? buflen : INT_MAX));
13266
ddd7bebf854c ttyname_r: Make it work on MacOS X 10.4 and Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13023
diff changeset
58 if (name == NULL)
ddd7bebf854c ttyname_r: Make it work on MacOS X 10.4 and Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13023
diff changeset
59 return errno;
ddd7bebf854c ttyname_r: Make it work on MacOS X 10.4 and Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13023
diff changeset
60 if (name != buf)
ddd7bebf854c ttyname_r: Make it work on MacOS X 10.4 and Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13023
diff changeset
61 {
ddd7bebf854c ttyname_r: Make it work on MacOS X 10.4 and Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13023
diff changeset
62 size_t namelen = strlen (name) + 1;
ddd7bebf854c ttyname_r: Make it work on MacOS X 10.4 and Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13023
diff changeset
63 if (namelen > buflen)
ddd7bebf854c ttyname_r: Make it work on MacOS X 10.4 and Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13023
diff changeset
64 return ERANGE;
ddd7bebf854c ttyname_r: Make it work on MacOS X 10.4 and Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13023
diff changeset
65 memmove (buf, name, namelen);
ddd7bebf854c ttyname_r: Make it work on MacOS X 10.4 and Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13023
diff changeset
66 }
13268
7287ecc3411c ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13266
diff changeset
67 # endif
13266
ddd7bebf854c ttyname_r: Make it work on MacOS X 10.4 and Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13023
diff changeset
68 return 0;
ddd7bebf854c ttyname_r: Make it work on MacOS X 10.4 and Solaris 10.
Bruno Haible <bruno@clisp.org>
parents: 13023
diff changeset
69 #elif HAVE_TTYNAME
13023
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 /* Note: This is not multithread-safe. */
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 char *name;
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 size_t namelen;
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 name = ttyname (fd);
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 if (name == NULL)
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 return errno;
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 namelen = strlen (name) + 1;
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 if (namelen > buflen)
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 return ERANGE;
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 memcpy (buf, name, namelen);
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 return 0;
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 #else
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 /* Platforms like mingw: no ttys exist at all. */
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 return ENOTTY;
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 #endif
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 }