Mercurial > hg > octave-shane > gnulib-hg
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 |
rev | line source |
---|---|
13023 | 1 /* Determine name of a terminal. |
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 | 4 |
5 This program is free software: you can redistribute it and/or modify | |
6 it under the terms of the GNU General Public License as published by | |
7 the Free Software Foundation; either version 3 of the License, or | |
8 (at your option) any later version. | |
9 | |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
17 | |
18 /* Written by Bruno Haible <bruno@clisp.org>, 2010. */ | |
19 | |
20 #include <config.h> | |
21 | |
22 #include <unistd.h> | |
23 | |
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 | 26 #include <string.h> |
27 | |
28 int | |
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 | 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 | 70 /* Note: This is not multithread-safe. */ |
71 char *name; | |
72 size_t namelen; | |
73 | |
74 name = ttyname (fd); | |
75 if (name == NULL) | |
76 return errno; | |
77 namelen = strlen (name) + 1; | |
78 if (namelen > buflen) | |
79 return ERANGE; | |
80 memcpy (buf, name, namelen); | |
81 return 0; | |
82 #else | |
83 /* Platforms like mingw: no ttys exist at all. */ | |
84 return ENOTTY; | |
85 #endif | |
86 } |