Mercurial > hg > octave-shane > gnulib-hg
annotate lib/ttyname_r.c @ 13268:7287ecc3411c
ttyname_r: Make it work on Solaris 10.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Mon, 26 Apr 2010 00:18:10 +0200 |
parents | ddd7bebf854c |
children | 2ff78debd520 |
rev | line source |
---|---|
13023 | 1 /* Determine name of a terminal. |
2 | |
3 Copyright (C) 2010 Free Software Foundation, Inc. | |
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. */ |
13268
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
35 /* On Solaris, ttyname_r always fails if buflen < 128. So provide a buffer |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
36 that is large enough. */ |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
37 char largerbuf[512]; |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
38 # if HAVE_POSIXDECL_TTYNAME_R |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
39 int err = |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
40 (buflen < sizeof (largerbuf) |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
41 ? ttyname_r (fd, largerbuf, sizeof (largerbuf)) |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
42 : 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
|
43 if (err != 0) |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
44 return err; |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
45 if (buflen < sizeof (largerbuf)) |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
46 { |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
47 size_t namelen = strlen (largerbuf); |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
48 if (namelen > buflen) |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
49 return ERANGE; |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
50 memcpy (buf, largerbuf, namelen); |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
51 } |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
52 # else |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
53 char *name = |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
54 (buflen < sizeof (largerbuf) |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
55 ? ttyname_r (fd, largerbuf, sizeof (largerbuf)) |
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
56 : 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
|
57 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
|
58 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
|
59 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
|
60 { |
ddd7bebf854c
ttyname_r: Make it work on MacOS X 10.4 and Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13023
diff
changeset
|
61 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
|
62 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
|
63 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
|
64 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
|
65 } |
13268
7287ecc3411c
ttyname_r: Make it work on Solaris 10.
Bruno Haible <bruno@clisp.org>
parents:
13266
diff
changeset
|
66 # 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
|
67 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
|
68 #elif HAVE_TTYNAME |
13023 | 69 /* Note: This is not multithread-safe. */ |
70 char *name; | |
71 size_t namelen; | |
72 | |
73 name = ttyname (fd); | |
74 if (name == NULL) | |
75 return errno; | |
76 namelen = strlen (name) + 1; | |
77 if (namelen > buflen) | |
78 return ERANGE; | |
79 memcpy (buf, name, namelen); | |
80 return 0; | |
81 #else | |
82 /* Platforms like mingw: no ttys exist at all. */ | |
83 return ENOTTY; | |
84 #endif | |
85 } |