Mercurial > gnulib
annotate lib/openat-proc.c @ 40231:9b3c79fdfe0b
strtod: fix clash with strtold
Problem reported for RHEL 5 by Jesse Caldwell (Bug#34817).
* lib/strtod.c (compute_minus_zero, minus_zero):
Simplify by remving the macro / external variable,
and having just a function. User changed. This avoids
the need for an external variable that might clash.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Mon, 11 Mar 2019 16:40:29 -0700 |
parents | b06060465f09 |
children |
rev | line source |
---|---|
7389 | 1 /* Create /proc/self/fd-related names for subfiles of open directories. |
2 | |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
3 Copyright (C) 2006, 2009-2019 Free Software Foundation, Inc. |
7389 | 4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7863
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
7389 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7863
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7863
diff
changeset
|
8 (at your option) any later version. |
7389 | 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 | |
19190 | 16 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
7389 | 17 |
18 /* Written by Paul Eggert. */ | |
19 | |
20 #include <config.h> | |
21 | |
22 #include "openat-priv.h" | |
23 | |
24 #include <sys/types.h> | |
25 #include <sys/stat.h> | |
26 #include <fcntl.h> | |
27 | |
28 #include <stdio.h> | |
14167
7e8e0e534d32
openat, save-cwd: avoid xmalloc
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
29 #include <stdlib.h> |
7389 | 30 #include <string.h> |
12501 | 31 #include <unistd.h> |
7389 | 32 |
18212
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
33 #ifdef __KLIBC__ |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
34 # include <InnoTekLIBC/backend.h> |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
35 #endif |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
36 |
7389 | 37 #include "intprops.h" |
38 | |
18212
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
39 /* Set BUF to the name of the subfile of the directory identified by |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
40 FD, where the subfile is named FILE. If successful, return BUF if |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
41 the result fits in BUF, dynamically allocated memory otherwise. |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
42 Return NULL (setting errno) on error. */ |
7389 | 43 char * |
44 openat_proc_name (char buf[OPENAT_BUFFER_SIZE], int fd, char const *file) | |
45 { | |
18212
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
46 char *result = buf; |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
47 int dirlen; |
7389 | 48 |
11950
bc28631dbf94
openat: fail with ENOENT on empty name
Eric Blake <ebb9@byu.net>
parents:
11938
diff
changeset
|
49 /* Make sure the caller gets ENOENT when appropriate. */ |
bc28631dbf94
openat: fail with ENOENT on empty name
Eric Blake <ebb9@byu.net>
parents:
11938
diff
changeset
|
50 if (!*file) |
bc28631dbf94
openat: fail with ENOENT on empty name
Eric Blake <ebb9@byu.net>
parents:
11938
diff
changeset
|
51 { |
bc28631dbf94
openat: fail with ENOENT on empty name
Eric Blake <ebb9@byu.net>
parents:
11938
diff
changeset
|
52 buf[0] = '\0'; |
bc28631dbf94
openat: fail with ENOENT on empty name
Eric Blake <ebb9@byu.net>
parents:
11938
diff
changeset
|
53 return buf; |
bc28631dbf94
openat: fail with ENOENT on empty name
Eric Blake <ebb9@byu.net>
parents:
11938
diff
changeset
|
54 } |
bc28631dbf94
openat: fail with ENOENT on empty name
Eric Blake <ebb9@byu.net>
parents:
11938
diff
changeset
|
55 |
18212
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
56 #ifndef __KLIBC__ |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
57 # define PROC_SELF_FD_FORMAT "/proc/self/fd/%d/" |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
58 { |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
59 enum { |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
60 PROC_SELF_FD_DIR_SIZE_BOUND |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
61 = (sizeof PROC_SELF_FD_FORMAT - (sizeof "%d" - 1) |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
62 + INT_STRLEN_BOUND (int)) |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
63 }; |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
64 |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
65 static int proc_status = 0; |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
66 if (! proc_status) |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
67 { |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
68 /* Set PROC_STATUS to a positive value if /proc/self/fd is |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
69 reliable, and a negative value otherwise. Solaris 10 |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
70 /proc/self/fd mishandles "..", and any file name might expand |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
71 to ".." after symbolic link expansion, so avoid /proc/self/fd |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
72 if it mishandles "..". Solaris 10 has openat, but this |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
73 problem is exhibited on code that built on Solaris 8 and |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
74 running on Solaris 10. */ |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
75 |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
76 int proc_self_fd = open ("/proc/self/fd", |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
77 O_SEARCH | O_DIRECTORY | O_NOCTTY | O_NONBLOCK); |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
78 if (proc_self_fd < 0) |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
79 proc_status = -1; |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
80 else |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
81 { |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
82 /* Detect whether /proc/self/fd/%i/../fd exists, where %i is the |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
83 number of a file descriptor open on /proc/self/fd. On Linux, |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
84 that name resolves to /proc/self/fd, which was opened above. |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
85 However, on Solaris, it may resolve to /proc/self/fd/fd, which |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
86 cannot exist, since all names in /proc/self/fd are numeric. */ |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
87 char dotdot_buf[PROC_SELF_FD_DIR_SIZE_BOUND + sizeof "../fd" - 1]; |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
88 sprintf (dotdot_buf, PROC_SELF_FD_FORMAT "../fd", proc_self_fd); |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
89 proc_status = access (dotdot_buf, F_OK) ? -1 : 1; |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
90 close (proc_self_fd); |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
91 } |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
92 } |
7389 | 93 |
18212
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
94 if (proc_status < 0) |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
95 return NULL; |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
96 else |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
97 { |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
98 size_t bufsize = PROC_SELF_FD_DIR_SIZE_BOUND + strlen (file); |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
99 if (OPENAT_BUFFER_SIZE < bufsize) |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
100 { |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
101 result = malloc (bufsize); |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
102 if (! result) |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
103 return NULL; |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
104 } |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
105 |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
106 dirlen = sprintf (result, PROC_SELF_FD_FORMAT, fd); |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
107 } |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
108 } |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
109 #else |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
110 /* OS/2 kLIBC provides a function to retrieve a path from a fd. */ |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
111 { |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
112 char dir[_MAX_PATH]; |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
113 size_t bufsize; |
7389 | 114 |
18212
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
115 if (__libc_Back_ioFHToPath (fd, dir, sizeof dir)) |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
116 return NULL; |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
117 |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
118 dirlen = strlen (dir); |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
119 bufsize = dirlen + 1 + strlen (file) + 1; /* 1 for '/', 1 for null */ |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
120 if (OPENAT_BUFFER_SIZE < bufsize) |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
121 { |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
122 result = malloc (bufsize); |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
123 if (! result) |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
124 return NULL; |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
125 } |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
126 |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
127 strcpy (result, dir); |
18229
9010100a27dd
openat_proc_name: fix that last '/' is overwritten on OS/2 kLIBC
KO Myung-Hun <komh78@gmail.com>
parents:
18212
diff
changeset
|
128 result[dirlen++] = '/'; |
18212
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
129 } |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
130 #endif |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
131 |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
132 strcpy (result + dirlen, file); |
5926b8112df9
openat_proc_name: port to OS/2 kLIBC
Paul Eggert <eggert@cs.ucla.edu>
parents:
18189
diff
changeset
|
133 return result; |
7389 | 134 } |