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
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
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Copyright (C) 2010 Free Software Foundation, Inc.
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. */
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
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 /* Note: This is not multithread-safe. */
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 char *name;
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 size_t namelen;
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 name = ttyname (fd);
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 if (name == NULL)
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 return errno;
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 namelen = strlen (name) + 1;
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 if (namelen > buflen)
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 return ERANGE;
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 memcpy (buf, name, namelen);
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 return 0;
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 #else
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 /* Platforms like mingw: no ttys exist at all. */
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 return ENOTTY;
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 #endif
953c5d14818b New module 'ttyname_r'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 }