Mercurial > gnulib
annotate lib/ptsname_r.c @ 16940:f0c9b2dc0596
ptsname_r: Fix test failures on IRIX, Solaris.
* m4/ptsname_r.m4 (gl_PREREQ_PTSNAME_R): Test whether isatty sets
errno when it fails. Define ISATTY_FAILS_WITHOUT_SETTING_ERRNO
accordingly.
* lib/ptsname_r.c: Include <fcntl.h>.
(__ptsname_r): When isatty returned false, then on IRIX, Solaris
set errno if fd is invalid.
* tests/test-isatty.c (main): Update comments.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sun, 24 Jun 2012 17:05:25 +0200 |
parents | 8250f2777afc |
children | 17843e284264 |
rev | line source |
---|---|
16067 | 1 /* Determine name of the slave side of a pseudo-terminal. |
16201
8250f2777afc
maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents:
16067
diff
changeset
|
2 Copyright (C) 1998, 2002, 2010-2012 Free Software Foundation, Inc. |
16067 | 3 |
4 This program is free software: you can redistribute it and/or modify | |
5 it under the terms of the GNU General Public License as published by | |
6 the Free Software Foundation; either version 3 of the License, or | |
7 (at your option) any later version. | |
8 | |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
16 | |
17 #include <config.h> | |
18 | |
19 #include <stdlib.h> | |
20 | |
21 #include <errno.h> | |
16940
f0c9b2dc0596
ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
22 #include <fcntl.h> |
16067 | 23 #include <string.h> |
24 #include <sys/stat.h> | |
25 #include <unistd.h> | |
26 | |
27 #ifdef _LIBC | |
28 # include <paths.h> | |
29 #else | |
30 # ifndef _PATH_TTY | |
31 # define _PATH_TTY "/dev/tty" | |
32 # endif | |
33 # ifndef _PATH_DEV | |
34 # define _PATH_DEV "/dev/" | |
35 # endif | |
36 | |
37 # define __set_errno(e) errno = (e) | |
38 # define __isatty isatty | |
39 # define __stat stat | |
40 # define __ttyname_r ttyname_r | |
41 # define __ptsname_r ptsname_r | |
42 | |
43 #endif | |
44 | |
45 | |
46 /* Store at most BUFLEN characters of the pathname of the slave pseudo | |
47 terminal associated with the master FD is open on in BUF. | |
48 Return 0 on success, otherwise an error number. */ | |
49 int | |
50 __ptsname_r (int fd, char *buf, size_t buflen) | |
51 { | |
52 int save_errno = errno; | |
53 int err; | |
54 struct stat st; | |
55 | |
56 if (buf == NULL) | |
57 { | |
58 __set_errno (EINVAL); | |
59 return EINVAL; | |
60 } | |
61 | |
62 if (!__isatty (fd)) | |
16940
f0c9b2dc0596
ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
63 { |
f0c9b2dc0596
ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
64 #if ISATTY_FAILS_WITHOUT_SETTING_ERRNO && defined F_GETFL /* IRIX, Solaris */ |
f0c9b2dc0596
ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
65 /* Set errno. */ |
f0c9b2dc0596
ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
66 if (fcntl (fd, F_GETFL) != -1) |
f0c9b2dc0596
ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
67 errno = ENOTTY; |
f0c9b2dc0596
ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
68 #else |
f0c9b2dc0596
ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
69 /* We rely on isatty to set errno properly (i.e. EBADF or ENOTTY). */ |
f0c9b2dc0596
ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
70 #endif |
f0c9b2dc0596
ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
71 return errno; |
f0c9b2dc0596
ptsname_r: Fix test failures on IRIX, Solaris.
Bruno Haible <bruno@clisp.org>
parents:
16201
diff
changeset
|
72 } |
16067 | 73 |
74 if (buflen < strlen (_PATH_TTY) + 3) | |
75 { | |
76 __set_errno (ERANGE); | |
77 return ERANGE; | |
78 } | |
79 | |
80 err = __ttyname_r (fd, buf, buflen); | |
81 if (err != 0) | |
82 { | |
83 __set_errno (err); | |
84 return errno; | |
85 } | |
86 | |
87 buf[sizeof (_PATH_DEV) - 1] = 't'; | |
88 | |
89 if (__stat (buf, &st) < 0) | |
90 return errno; | |
91 | |
92 __set_errno (save_errno); | |
93 return 0; | |
94 } |