Mercurial > gnulib
annotate lib/savewd.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 |
---|---|
7315 | 1 /* Save and restore the working directory, possibly using a child process. |
2 | |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
3 Copyright (C) 2006-2007, 2009-2019 Free Software Foundation, Inc. |
7315 | 4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9216
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
7315 | 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:
9216
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:
9216
diff
changeset
|
8 (at your option) any later version. |
7315 | 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/>. */ |
7315 | 17 |
18 /* Written by Paul Eggert. */ | |
19 | |
20 #include <config.h> | |
21 | |
17181
a80e4b259d9b
chdir-long, cycle-check, savewd: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
22 #define SAVEWD_INLINE _GL_EXTERN_INLINE |
a80e4b259d9b
chdir-long, cycle-check, savewd: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
23 |
7315 | 24 #include "savewd.h" |
25 | |
26 #include <errno.h> | |
27 #include <fcntl.h> | |
7375 | 28 #include <signal.h> |
7315 | 29 #include <stdbool.h> |
8196
5166dec344af
exit.h is replaced with stdlib.h.
Bruno Haible <bruno@clisp.org>
parents:
7408
diff
changeset
|
30 #include <stdlib.h> |
7315 | 31 #include <sys/types.h> |
32 #include <sys/wait.h> | |
33 #include <unistd.h> | |
34 | |
17840 | 35 #include "assure.h" |
14393
9f47f8c334f2
dirname: move m4/dos.m4 functionality into lib/dosname.h
Paul Eggert <eggert@cs.ucla.edu>
parents:
14079
diff
changeset
|
36 #include "dosname.h" |
7315 | 37 #include "fcntl-safer.h" |
38 | |
18956
f2019f19d268
maint: update to work with GCC7's -Werror=implicit-fallthrough=
Jim Meyering <meyering@fb.com>
parents:
18939
diff
changeset
|
39 #ifndef FALLTHROUGH |
f2019f19d268
maint: update to work with GCC7's -Werror=implicit-fallthrough=
Jim Meyering <meyering@fb.com>
parents:
18939
diff
changeset
|
40 # if __GNUC__ < 7 |
f2019f19d268
maint: update to work with GCC7's -Werror=implicit-fallthrough=
Jim Meyering <meyering@fb.com>
parents:
18939
diff
changeset
|
41 # define FALLTHROUGH ((void) 0) |
f2019f19d268
maint: update to work with GCC7's -Werror=implicit-fallthrough=
Jim Meyering <meyering@fb.com>
parents:
18939
diff
changeset
|
42 # else |
f2019f19d268
maint: update to work with GCC7's -Werror=implicit-fallthrough=
Jim Meyering <meyering@fb.com>
parents:
18939
diff
changeset
|
43 # define FALLTHROUGH __attribute__ ((__fallthrough__)) |
f2019f19d268
maint: update to work with GCC7's -Werror=implicit-fallthrough=
Jim Meyering <meyering@fb.com>
parents:
18939
diff
changeset
|
44 # endif |
f2019f19d268
maint: update to work with GCC7's -Werror=implicit-fallthrough=
Jim Meyering <meyering@fb.com>
parents:
18939
diff
changeset
|
45 #endif |
f2019f19d268
maint: update to work with GCC7's -Werror=implicit-fallthrough=
Jim Meyering <meyering@fb.com>
parents:
18939
diff
changeset
|
46 |
7315 | 47 /* Save the working directory into *WD, if it hasn't been saved |
48 already. Return true if a child has been forked to do the real | |
49 work. */ | |
50 static bool | |
51 savewd_save (struct savewd *wd) | |
52 { | |
53 switch (wd->state) | |
54 { | |
55 case INITIAL_STATE: | |
56 /* Save the working directory, or prepare to fall back if possible. */ | |
57 { | |
13616
acc972b5da60
fcntl-h, etc.: prefer O_SEARCH to O_RDONLY when applicable
Paul Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
58 int fd = open_safer (".", O_SEARCH); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
59 if (0 <= fd) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
60 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
61 wd->state = FD_STATE; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
62 wd->val.fd = fd; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
63 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
64 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
65 if (errno != EACCES && errno != ESTALE) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
66 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
67 wd->state = ERROR_STATE; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
68 wd->val.errnum = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
69 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
70 } |
7315 | 71 } |
72 wd->state = FORKING_STATE; | |
73 wd->val.child = -1; | |
18956
f2019f19d268
maint: update to work with GCC7's -Werror=implicit-fallthrough=
Jim Meyering <meyering@fb.com>
parents:
18939
diff
changeset
|
74 FALLTHROUGH; |
7315 | 75 case FORKING_STATE: |
76 if (wd->val.child < 0) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
77 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
78 /* "Save" the initial working directory by forking a new |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
79 subprocess that will attempt all the work from the chdir |
18939
778d3f273443
Remove repeated words in comments.
Bjarni Ingi Gislason <bjarniig@rhi.hi.is>
parents:
18626
diff
changeset
|
80 until the next savewd_restore. */ |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
81 wd->val.child = fork (); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
82 if (wd->val.child != 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
83 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
84 if (0 < wd->val.child) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
85 return true; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
86 wd->state = ERROR_STATE; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
87 wd->val.errnum = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
88 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
89 } |
7315 | 90 break; |
91 | |
92 case FD_STATE: | |
93 case FD_POST_CHDIR_STATE: | |
94 case ERROR_STATE: | |
95 case FINAL_STATE: | |
96 break; | |
97 | |
98 default: | |
17840 | 99 assure (false); |
7315 | 100 } |
101 | |
102 return false; | |
103 } | |
104 | |
105 int | |
106 savewd_chdir (struct savewd *wd, char const *dir, int options, | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
107 int open_result[2]) |
7315 | 108 { |
109 int fd = -1; | |
110 int result = 0; | |
111 | |
112 /* Open the directory if requested, or if avoiding a race condition | |
113 is requested and possible. */ | |
7408 | 114 if (open_result |
115 || (options & (HAVE_WORKING_O_NOFOLLOW ? SAVEWD_CHDIR_NOFOLLOW : 0))) | |
7315 | 116 { |
117 fd = open (dir, | |
13616
acc972b5da60
fcntl-h, etc.: prefer O_SEARCH to O_RDONLY when applicable
Paul Eggert <eggert@cs.ucla.edu>
parents:
12559
diff
changeset
|
118 (O_SEARCH | O_DIRECTORY | O_NOCTTY | O_NONBLOCK |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
119 | (options & SAVEWD_CHDIR_NOFOLLOW ? O_NOFOLLOW : 0))); |
7315 | 120 |
121 if (open_result) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
122 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
123 open_result[0] = fd; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
124 open_result[1] = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
125 } |
7315 | 126 |
18104
76e10610d806
savewd: remove SAVEWD_CHDIR_READABLE
Paul Eggert <eggert@cs.ucla.edu>
parents:
17848
diff
changeset
|
127 if (fd < 0 && errno != EACCES) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
128 result = -1; |
7315 | 129 } |
130 | |
131 if (result == 0 && ! (0 <= fd && options & SAVEWD_CHDIR_SKIP_READABLE)) | |
132 { | |
133 if (savewd_save (wd)) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
134 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
135 open_result = NULL; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
136 result = -2; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
137 } |
7315 | 138 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
139 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
140 result = (fd < 0 ? chdir (dir) : fchdir (fd)); |
7315 | 141 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
142 if (result == 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
143 switch (wd->state) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
144 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
145 case FD_STATE: |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
146 wd->state = FD_POST_CHDIR_STATE; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
147 break; |
7315 | 148 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
149 case ERROR_STATE: |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
150 case FD_POST_CHDIR_STATE: |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
151 case FINAL_STATE: |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
152 break; |
7315 | 153 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
154 case FORKING_STATE: |
17840 | 155 assure (wd->val.child == 0); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
156 break; |
7315 | 157 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
158 default: |
17840 | 159 assure (false); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
160 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
161 } |
7315 | 162 } |
163 | |
164 if (0 <= fd && ! open_result) | |
165 { | |
166 int e = errno; | |
167 close (fd); | |
168 errno = e; | |
169 } | |
170 | |
171 return result; | |
172 } | |
173 | |
174 int | |
175 savewd_restore (struct savewd *wd, int status) | |
176 { | |
177 switch (wd->state) | |
178 { | |
179 case INITIAL_STATE: | |
180 case FD_STATE: | |
181 /* The working directory is the desired directory, so there's no | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
182 work to do. */ |
7315 | 183 break; |
184 | |
185 case FD_POST_CHDIR_STATE: | |
186 /* Restore the working directory using fchdir. */ | |
187 if (fchdir (wd->val.fd) == 0) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
188 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
189 wd->state = FD_STATE; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
190 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
191 } |
7315 | 192 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
193 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
194 int chdir_errno = errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
195 close (wd->val.fd); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
196 wd->state = ERROR_STATE; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
197 wd->val.errnum = chdir_errno; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
198 } |
18956
f2019f19d268
maint: update to work with GCC7's -Werror=implicit-fallthrough=
Jim Meyering <meyering@fb.com>
parents:
18939
diff
changeset
|
199 FALLTHROUGH; |
7315 | 200 case ERROR_STATE: |
201 /* Report an error if asked to restore the working directory. */ | |
202 errno = wd->val.errnum; | |
203 return -1; | |
204 | |
205 case FORKING_STATE: | |
206 /* "Restore" the working directory by waiting for the subprocess | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
207 to finish. */ |
7315 | 208 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
209 pid_t child = wd->val.child; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
210 if (child == 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
211 _exit (status); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
212 if (0 < child) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
213 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
214 int child_status; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
215 while (waitpid (child, &child_status, 0) < 0) |
17840 | 216 assure (errno == EINTR); |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
217 wd->val.child = -1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
218 if (! WIFEXITED (child_status)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
219 raise (WTERMSIG (child_status)); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
220 return WEXITSTATUS (child_status); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
221 } |
7315 | 222 } |
223 break; | |
224 | |
225 default: | |
17840 | 226 assure (false); |
7315 | 227 } |
228 | |
229 return 0; | |
230 } | |
231 | |
232 void | |
233 savewd_finish (struct savewd *wd) | |
234 { | |
235 switch (wd->state) | |
236 { | |
237 case INITIAL_STATE: | |
238 case ERROR_STATE: | |
239 break; | |
240 | |
241 case FD_STATE: | |
242 case FD_POST_CHDIR_STATE: | |
243 close (wd->val.fd); | |
244 break; | |
245 | |
246 case FORKING_STATE: | |
17840 | 247 assure (wd->val.child < 0); |
7315 | 248 break; |
249 | |
250 default: | |
17840 | 251 assure (false); |
7315 | 252 } |
253 | |
254 wd->state = FINAL_STATE; | |
255 } | |
256 | |
257 /* Return true if the actual work is currently being done by a | |
258 subprocess. | |
259 | |
260 A true return means that the caller and the subprocess should | |
261 resynchronize later with savewd_restore, using only their own | |
262 memory to decide when to resynchronize; they should not consult the | |
263 file system to decide, because that might lead to race conditions. | |
264 This is why savewd_chdir is broken out into another function; | |
265 savewd_chdir's callers _can_ inspect the file system to decide | |
266 whether to call savewd_chdir. */ | |
17181
a80e4b259d9b
chdir-long, cycle-check, savewd: better 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
267 static bool |
7315 | 268 savewd_delegating (struct savewd const *wd) |
269 { | |
270 return wd->state == FORKING_STATE && 0 < wd->val.child; | |
271 } | |
272 | |
273 int | |
274 savewd_process_files (int n_files, char **file, | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
275 int (*act) (char *, struct savewd *, void *), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
276 void *options) |
7315 | 277 { |
278 int i = 0; | |
279 int last_relative; | |
280 int exit_status = EXIT_SUCCESS; | |
281 struct savewd wd; | |
282 savewd_init (&wd); | |
283 | |
284 for (last_relative = n_files - 1; 0 <= last_relative; last_relative--) | |
285 if (! IS_ABSOLUTE_FILE_NAME (file[last_relative])) | |
286 break; | |
287 | |
288 for (; i < last_relative; i++) | |
289 { | |
290 if (! savewd_delegating (&wd)) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
291 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
292 int s = act (file[i], &wd, options); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
293 if (exit_status < s) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
294 exit_status = s; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
295 } |
7315 | 296 |
297 if (! IS_ABSOLUTE_FILE_NAME (file[i + 1])) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
298 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
299 int r = savewd_restore (&wd, exit_status); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
300 if (exit_status < r) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
301 exit_status = r; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
302 } |
7315 | 303 } |
304 | |
305 savewd_finish (&wd); | |
306 | |
307 for (; i < n_files; i++) | |
308 { | |
309 int s = act (file[i], &wd, options); | |
310 if (exit_status < s) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11941
diff
changeset
|
311 exit_status = s; |
7315 | 312 } |
313 | |
314 return exit_status; | |
315 } |