]>
Commit | Line | Data |
---|---|---|
726f6388 JA |
1 | /* general.c -- Stuff that is used by all files. */ |
2 | ||
4931aca6 | 3 | /* Copyright (C) 1987-2004 Free Software Foundation, Inc. |
726f6388 JA |
4 | |
5 | This file is part of GNU Bash, the Bourne Again SHell. | |
6 | ||
7 | Bash is free software; you can redistribute it and/or modify it under | |
8 | the terms of the GNU General Public License as published by the Free | |
9 | Software Foundation; either version 2, or (at your option) any later | |
10 | version. | |
11 | ||
12 | Bash is distributed in the hope that it will be useful, but WITHOUT ANY | |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License along | |
18 | with Bash; see the file COPYING. If not, write to the Free Software | |
bb70624e | 19 | Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ |
726f6388 | 20 | |
ccc6cda3 JA |
21 | #include "config.h" |
22 | ||
726f6388 | 23 | #include "bashtypes.h" |
cce855bc JA |
24 | #ifndef _MINIX |
25 | # include <sys/param.h> | |
26 | #endif | |
ccc6cda3 JA |
27 | #include "posixstat.h" |
28 | ||
29 | #if defined (HAVE_UNISTD_H) | |
30 | # include <unistd.h> | |
31 | #endif | |
32 | ||
726f6388 JA |
33 | #include "filecntl.h" |
34 | #include "bashansi.h" | |
ccc6cda3 | 35 | #include <stdio.h> |
f73dda09 | 36 | #include "chartypes.h" |
ccc6cda3 JA |
37 | #include <errno.h> |
38 | ||
5e13499c CR |
39 | #include "bashintl.h" |
40 | ||
726f6388 | 41 | #include "shell.h" |
e6e3b444 CR |
42 | #include "test.h" |
43 | ||
726f6388 JA |
44 | #include <tilde/tilde.h> |
45 | ||
726f6388 JA |
46 | #if !defined (errno) |
47 | extern int errno; | |
48 | #endif /* !errno */ | |
49 | ||
f73dda09 | 50 | extern int expand_aliases; |
ccc6cda3 | 51 | extern int interactive_comments; |
28ef6c31 JA |
52 | extern int check_hashed_filenames; |
53 | extern int source_uses_path; | |
54 | extern int source_searches_cwd; | |
cce855bc | 55 | |
7117c2d2 JA |
56 | static char *bash_special_tilde_expansions __P((char *)); |
57 | static int unquoted_tilde_word __P((const char *)); | |
58 | static void initialize_group_array __P((void)); | |
59 | ||
cce855bc | 60 | /* A standard error message to use when getcwd() returns NULL. */ |
f37c27ea | 61 | const char * const bash_getcwd_errstr = N_("getcwd: cannot access parent directories"); |
726f6388 | 62 | |
ccc6cda3 | 63 | /* Do whatever is necessary to initialize `Posix mode'. */ |
726f6388 | 64 | void |
ccc6cda3 JA |
65 | posix_initialize (on) |
66 | int on; | |
726f6388 | 67 | { |
28ef6c31 JA |
68 | /* Things that should be turned on when posix mode is enabled. */ |
69 | if (on != 0) | |
70 | { | |
71 | interactive_comments = source_uses_path = expand_aliases = 1; | |
72 | } | |
73 | ||
74 | /* Things that should be turned on when posix mode is disabled. */ | |
75 | if (on == 0) | |
76 | { | |
77 | source_searches_cwd = 1; | |
78 | expand_aliases = interactive_shell; | |
79 | } | |
726f6388 JA |
80 | } |
81 | ||
ccc6cda3 JA |
82 | /* **************************************************************** */ |
83 | /* */ | |
84 | /* Functions to convert to and from and display non-standard types */ | |
85 | /* */ | |
86 | /* **************************************************************** */ | |
87 | ||
726f6388 JA |
88 | #if defined (RLIMTYPE) |
89 | RLIMTYPE | |
90 | string_to_rlimtype (s) | |
91 | char *s; | |
92 | { | |
cce855bc JA |
93 | RLIMTYPE ret; |
94 | int neg; | |
726f6388 | 95 | |
cce855bc JA |
96 | ret = 0; |
97 | neg = 0; | |
726f6388 JA |
98 | while (s && *s && whitespace (*s)) |
99 | s++; | |
100 | if (*s == '-' || *s == '+') | |
101 | { | |
102 | neg = *s == '-'; | |
103 | s++; | |
104 | } | |
f73dda09 JA |
105 | for ( ; s && *s && DIGIT (*s); s++) |
106 | ret = (ret * 10) + TODIGIT (*s); | |
726f6388 JA |
107 | return (neg ? -ret : ret); |
108 | } | |
109 | ||
110 | void | |
111 | print_rlimtype (n, addnl) | |
112 | RLIMTYPE n; | |
113 | int addnl; | |
114 | { | |
f73dda09 | 115 | char s[INT_STRLEN_BOUND (RLIMTYPE) + 1], *p; |
726f6388 | 116 | |
f73dda09 JA |
117 | p = s + sizeof(s); |
118 | *--p = '\0'; | |
726f6388 JA |
119 | |
120 | if (n < 0) | |
121 | { | |
f73dda09 JA |
122 | do |
123 | *--p = '0' - n % 10; | |
124 | while ((n /= 10) != 0); | |
125 | ||
126 | *--p = '-'; | |
127 | } | |
128 | else | |
129 | { | |
130 | do | |
131 | *--p = '0' + n % 10; | |
132 | while ((n /= 10) != 0); | |
726f6388 JA |
133 | } |
134 | ||
f73dda09 | 135 | printf ("%s%s", p, addnl ? "\n" : ""); |
726f6388 JA |
136 | } |
137 | #endif /* RLIMTYPE */ | |
138 | ||
ccc6cda3 JA |
139 | /* **************************************************************** */ |
140 | /* */ | |
141 | /* Input Validation Functions */ | |
142 | /* */ | |
143 | /* **************************************************************** */ | |
144 | ||
145 | /* Return non-zero if all of the characters in STRING are digits. */ | |
146 | int | |
147 | all_digits (string) | |
148 | char *string; | |
149 | { | |
28ef6c31 JA |
150 | register char *s; |
151 | ||
152 | for (s = string; *s; s++) | |
f73dda09 | 153 | if (DIGIT (*s) == 0) |
28ef6c31 JA |
154 | return (0); |
155 | ||
ccc6cda3 JA |
156 | return (1); |
157 | } | |
158 | ||
159 | /* Return non-zero if the characters pointed to by STRING constitute a | |
160 | valid number. Stuff the converted number into RESULT if RESULT is | |
f73dda09 | 161 | not null. */ |
ccc6cda3 JA |
162 | int |
163 | legal_number (string, result) | |
906833f0 | 164 | const char *string; |
7117c2d2 | 165 | intmax_t *result; |
ccc6cda3 | 166 | { |
7117c2d2 | 167 | intmax_t value; |
cce855bc | 168 | char *ep; |
ccc6cda3 JA |
169 | |
170 | if (result) | |
171 | *result = 0; | |
172 | ||
f73dda09 | 173 | errno = 0; |
7117c2d2 | 174 | value = strtoimax (string, &ep, 10); |
f73dda09 JA |
175 | if (errno) |
176 | return 0; /* errno is set on overflow or underflow */ | |
ccc6cda3 | 177 | |
7117c2d2 | 178 | /* Skip any trailing whitespace, since strtoimax does not. */ |
28ef6c31 JA |
179 | while (whitespace (*ep)) |
180 | ep++; | |
181 | ||
cce855bc JA |
182 | /* If *string is not '\0' but *ep is '\0' on return, the entire string |
183 | is valid. */ | |
184 | if (string && *string && *ep == '\0') | |
ccc6cda3 JA |
185 | { |
186 | if (result) | |
cce855bc JA |
187 | *result = value; |
188 | /* The SunOS4 implementation of strtol() will happily ignore | |
189 | overflow conditions, so this cannot do overflow correctly | |
190 | on those systems. */ | |
191 | return 1; | |
ccc6cda3 | 192 | } |
cce855bc JA |
193 | |
194 | return (0); | |
ccc6cda3 JA |
195 | } |
196 | ||
726f6388 JA |
197 | /* Return 1 if this token is a legal shell `identifier'; that is, it consists |
198 | solely of letters, digits, and underscores, and does not begin with a | |
199 | digit. */ | |
200 | int | |
201 | legal_identifier (name) | |
202 | char *name; | |
203 | { | |
204 | register char *s; | |
f73dda09 | 205 | unsigned char c; |
726f6388 | 206 | |
f73dda09 | 207 | if (!name || !(c = *name) || (legal_variable_starter (c) == 0)) |
726f6388 JA |
208 | return (0); |
209 | ||
f73dda09 | 210 | for (s = name + 1; (c = *s) != 0; s++) |
726f6388 | 211 | { |
f73dda09 | 212 | if (legal_variable_char (c) == 0) |
28ef6c31 | 213 | return (0); |
726f6388 JA |
214 | } |
215 | return (1); | |
216 | } | |
217 | ||
218 | /* Make sure that WORD is a valid shell identifier, i.e. | |
219 | does not contain a dollar sign, nor is quoted in any way. Nor | |
220 | does it consist of all digits. If CHECK_WORD is non-zero, | |
221 | the word is checked to ensure that it consists of only letters, | |
222 | digits, and underscores. */ | |
ccc6cda3 | 223 | int |
726f6388 JA |
224 | check_identifier (word, check_word) |
225 | WORD_DESC *word; | |
226 | int check_word; | |
227 | { | |
ccc6cda3 | 228 | if ((word->flags & (W_HASDOLLAR|W_QUOTED)) || all_digits (word->word)) |
726f6388 | 229 | { |
5e13499c | 230 | internal_error (_("`%s': not a valid identifier"), word->word); |
726f6388 JA |
231 | return (0); |
232 | } | |
233 | else if (check_word && legal_identifier (word->word) == 0) | |
234 | { | |
5e13499c | 235 | internal_error (_("`%s': not a valid identifier"), word->word); |
726f6388 JA |
236 | return (0); |
237 | } | |
238 | else | |
239 | return (1); | |
240 | } | |
241 | ||
1d7ecd77 CR |
242 | /* Return 1 if STRING comprises a valid alias name. The shell accepts |
243 | essentially all characters except those which must be quoted to the | |
244 | parser (which disqualifies them from alias expansion anyway) and `/'. */ | |
245 | int | |
246 | legal_alias_name (string, flags) | |
247 | char *string; | |
248 | int flags; | |
249 | { | |
250 | register char *s; | |
251 | ||
252 | for (s = string; *s; s++) | |
253 | if (shellbreak (*s) || shellxquote (*s) || shellexp (*s) || (*s == '/')) | |
254 | return 0; | |
255 | return 1; | |
256 | } | |
257 | ||
7117c2d2 JA |
258 | /* Returns non-zero if STRING is an assignment statement. The returned value |
259 | is the index of the `=' sign. */ | |
260 | int | |
5e13499c | 261 | assignment (string, flags) |
7117c2d2 | 262 | const char *string; |
5e13499c | 263 | int flags; |
7117c2d2 JA |
264 | { |
265 | register unsigned char c; | |
266 | register int newi, indx; | |
267 | ||
268 | c = string[indx = 0]; | |
269 | ||
5e13499c | 270 | #if defined (ARRAY_VARS) |
c3271763 | 271 | if ((legal_variable_starter (c) == 0) && (flags == 0 || c != '[')) /* ] */ |
5e13499c | 272 | #else |
7117c2d2 | 273 | if (legal_variable_starter (c) == 0) |
5e13499c | 274 | #endif |
7117c2d2 JA |
275 | return (0); |
276 | ||
277 | while (c = string[indx]) | |
278 | { | |
279 | /* The following is safe. Note that '=' at the start of a word | |
280 | is not an assignment statement. */ | |
281 | if (c == '=') | |
282 | return (indx); | |
283 | ||
284 | #if defined (ARRAY_VARS) | |
285 | if (c == '[') | |
286 | { | |
287 | newi = skipsubscript (string, indx); | |
288 | if (string[newi++] != ']') | |
289 | return (0); | |
d11b8b46 CR |
290 | if (string[newi] == '+' && string[newi+1] == '=') |
291 | return (newi + 1); | |
7117c2d2 JA |
292 | return ((string[newi] == '=') ? newi : 0); |
293 | } | |
294 | #endif /* ARRAY_VARS */ | |
295 | ||
d11b8b46 CR |
296 | /* Check for `+=' */ |
297 | if (c == '+' && string[indx+1] == '=') | |
298 | return (indx + 1); | |
299 | ||
7117c2d2 JA |
300 | /* Variable names in assignment statements may contain only letters, |
301 | digits, and `_'. */ | |
302 | if (legal_variable_char (c) == 0) | |
303 | return (0); | |
304 | ||
305 | indx++; | |
306 | } | |
307 | return (0); | |
308 | } | |
309 | ||
ccc6cda3 JA |
310 | /* **************************************************************** */ |
311 | /* */ | |
312 | /* Functions to manage files and file descriptors */ | |
313 | /* */ | |
314 | /* **************************************************************** */ | |
315 | ||
726f6388 JA |
316 | /* A function to unset no-delay mode on a file descriptor. Used in shell.c |
317 | to unset it on the fd passed as stdin. Should be called on stdin if | |
318 | readline gets an EAGAIN or EWOULDBLOCK when trying to read input. */ | |
319 | ||
320 | #if !defined (O_NDELAY) | |
321 | # if defined (FNDELAY) | |
322 | # define O_NDELAY FNDELAY | |
323 | # endif | |
324 | #endif /* O_NDELAY */ | |
325 | ||
326 | /* Make sure no-delay mode is not set on file descriptor FD. */ | |
bb70624e | 327 | int |
28ef6c31 | 328 | sh_unset_nodelay_mode (fd) |
726f6388 JA |
329 | int fd; |
330 | { | |
bb70624e | 331 | int flags, bflags; |
726f6388 JA |
332 | |
333 | if ((flags = fcntl (fd, F_GETFL, 0)) < 0) | |
bb70624e | 334 | return -1; |
726f6388 | 335 | |
bb70624e | 336 | bflags = 0; |
ccc6cda3 JA |
337 | |
338 | /* This is defined to O_NDELAY in filecntl.h if O_NONBLOCK is not present | |
339 | and O_NDELAY is defined. */ | |
bb70624e JA |
340 | #ifdef O_NONBLOCK |
341 | bflags |= O_NONBLOCK; | |
342 | #endif | |
343 | ||
344 | #ifdef O_NDELAY | |
345 | bflags |= O_NDELAY; | |
346 | #endif | |
347 | ||
348 | if (flags & bflags) | |
726f6388 | 349 | { |
bb70624e JA |
350 | flags &= ~bflags; |
351 | return (fcntl (fd, F_SETFL, flags)); | |
726f6388 | 352 | } |
726f6388 | 353 | |
bb70624e | 354 | return 0; |
726f6388 JA |
355 | } |
356 | ||
7117c2d2 JA |
357 | /* Return 1 if file descriptor FD is valid; 0 otherwise. */ |
358 | int | |
359 | sh_validfd (fd) | |
360 | int fd; | |
361 | { | |
362 | return (fcntl (fd, F_GETFD, 0) >= 0); | |
363 | } | |
364 | ||
b72432fd JA |
365 | /* There is a bug in the NeXT 2.1 rlogind that causes opens |
366 | of /dev/tty to fail. */ | |
367 | ||
368 | #if defined (__BEOS__) | |
369 | /* On BeOS, opening in non-blocking mode exposes a bug in BeOS, so turn it | |
370 | into a no-op. This should probably go away in the future. */ | |
371 | # undef O_NONBLOCK | |
372 | # define O_NONBLOCK 0 | |
373 | #endif /* __BEOS__ */ | |
374 | ||
726f6388 | 375 | void |
ccc6cda3 | 376 | check_dev_tty () |
726f6388 | 377 | { |
ccc6cda3 JA |
378 | int tty_fd; |
379 | char *tty; | |
726f6388 | 380 | |
d166f048 | 381 | tty_fd = open ("/dev/tty", O_RDWR|O_NONBLOCK); |
726f6388 | 382 | |
ccc6cda3 | 383 | if (tty_fd < 0) |
726f6388 | 384 | { |
ccc6cda3 JA |
385 | tty = (char *)ttyname (fileno (stdin)); |
386 | if (tty == 0) | |
387 | return; | |
d166f048 | 388 | tty_fd = open (tty, O_RDWR|O_NONBLOCK); |
726f6388 | 389 | } |
ccc6cda3 | 390 | close (tty_fd); |
726f6388 JA |
391 | } |
392 | ||
ccc6cda3 JA |
393 | /* Return 1 if PATH1 and PATH2 are the same file. This is kind of |
394 | expensive. If non-NULL STP1 and STP2 point to stat structures | |
395 | corresponding to PATH1 and PATH2, respectively. */ | |
726f6388 | 396 | int |
ccc6cda3 JA |
397 | same_file (path1, path2, stp1, stp2) |
398 | char *path1, *path2; | |
399 | struct stat *stp1, *stp2; | |
726f6388 | 400 | { |
ccc6cda3 | 401 | struct stat st1, st2; |
726f6388 | 402 | |
ccc6cda3 | 403 | if (stp1 == NULL) |
726f6388 | 404 | { |
ccc6cda3 JA |
405 | if (stat (path1, &st1) != 0) |
406 | return (0); | |
407 | stp1 = &st1; | |
726f6388 | 408 | } |
726f6388 | 409 | |
ccc6cda3 JA |
410 | if (stp2 == NULL) |
411 | { | |
412 | if (stat (path2, &st2) != 0) | |
413 | return (0); | |
414 | stp2 = &st2; | |
415 | } | |
726f6388 | 416 | |
ccc6cda3 | 417 | return ((stp1->st_dev == stp2->st_dev) && (stp1->st_ino == stp2->st_ino)); |
726f6388 JA |
418 | } |
419 | ||
ccc6cda3 JA |
420 | /* Move FD to a number close to the maximum number of file descriptors |
421 | allowed in the shell process, to avoid the user stepping on it with | |
422 | redirection and causing us extra work. If CHECK_NEW is non-zero, | |
423 | we check whether or not the file descriptors are in use before | |
d166f048 JA |
424 | duplicating FD onto them. MAXFD says where to start checking the |
425 | file descriptors. If it's less than 20, we get the maximum value | |
426 | available from getdtablesize(2). */ | |
726f6388 | 427 | int |
d166f048 JA |
428 | move_to_high_fd (fd, check_new, maxfd) |
429 | int fd, check_new, maxfd; | |
726f6388 | 430 | { |
ccc6cda3 | 431 | int script_fd, nfds, ignore; |
726f6388 | 432 | |
d166f048 JA |
433 | if (maxfd < 20) |
434 | { | |
435 | nfds = getdtablesize (); | |
436 | if (nfds <= 0) | |
437 | nfds = 20; | |
7117c2d2 JA |
438 | if (nfds > HIGH_FD_MAX) |
439 | nfds = HIGH_FD_MAX; /* reasonable maximum */ | |
d166f048 JA |
440 | } |
441 | else | |
442 | nfds = maxfd; | |
726f6388 | 443 | |
ccc6cda3 JA |
444 | for (nfds--; check_new && nfds > 3; nfds--) |
445 | if (fcntl (nfds, F_GETFD, &ignore) == -1) | |
446 | break; | |
726f6388 | 447 | |
7117c2d2 | 448 | if (nfds > 3 && fd != nfds && (script_fd = dup2 (fd, nfds)) != -1) |
ccc6cda3 JA |
449 | { |
450 | if (check_new == 0 || fd != fileno (stderr)) /* don't close stderr */ | |
451 | close (fd); | |
452 | return (script_fd); | |
453 | } | |
726f6388 | 454 | |
7117c2d2 JA |
455 | /* OK, we didn't find one less than our artificial maximum; return the |
456 | original file descriptor. */ | |
ccc6cda3 JA |
457 | return (fd); |
458 | } | |
459 | ||
460 | /* Return non-zero if the characters from SAMPLE are not all valid | |
461 | characters to be found in the first line of a shell script. We | |
462 | check up to the first newline, or SAMPLE_LEN, whichever comes first. | |
463 | All of the characters must be printable or whitespace. */ | |
726f6388 | 464 | |
726f6388 | 465 | int |
ccc6cda3 | 466 | check_binary_file (sample, sample_len) |
f73dda09 | 467 | char *sample; |
ccc6cda3 | 468 | int sample_len; |
726f6388 | 469 | { |
ccc6cda3 | 470 | register int i; |
f73dda09 | 471 | unsigned char c; |
726f6388 | 472 | |
ccc6cda3 JA |
473 | for (i = 0; i < sample_len; i++) |
474 | { | |
f73dda09 JA |
475 | c = sample[i]; |
476 | if (c == '\n') | |
ccc6cda3 | 477 | return (0); |
726f6388 | 478 | |
74d9692b | 479 | #if 0 |
f73dda09 | 480 | if (ISSPACE (c) == 0 && ISPRINT (c) == 0) |
74d9692b CR |
481 | #else |
482 | if (c == '\0') | |
483 | #endif | |
ccc6cda3 | 484 | return (1); |
74d9692b | 485 | |
ccc6cda3 | 486 | } |
726f6388 | 487 | |
ccc6cda3 | 488 | return (0); |
726f6388 JA |
489 | } |
490 | ||
fb65be05 CR |
491 | /* **************************************************************** */ |
492 | /* */ | |
493 | /* Functions to inspect pathnames */ | |
494 | /* */ | |
495 | /* **************************************************************** */ | |
496 | ||
497 | int | |
498 | file_isdir (fn) | |
499 | char *fn; | |
500 | { | |
501 | struct stat sb; | |
502 | ||
503 | return ((stat (fn, &sb) == 0) && S_ISDIR (sb.st_mode)); | |
504 | } | |
505 | ||
506 | int | |
507 | file_iswdir (fn) | |
508 | char *fn; | |
509 | { | |
ac18b312 | 510 | return (file_isdir (fn) && sh_eaccess (fn, W_OK) == 0); |
fb65be05 CR |
511 | } |
512 | ||
ff247e74 CR |
513 | /* Return 1 if STRING contains an absolute pathname, else 0. Used by `cd' |
514 | to decide whether or not to look up a directory name in $CDPATH. */ | |
515 | int | |
516 | absolute_pathname (string) | |
517 | const char *string; | |
518 | { | |
519 | if (string == 0 || *string == '\0') | |
520 | return (0); | |
521 | ||
522 | if (ABSPATH(string)) | |
523 | return (1); | |
524 | ||
525 | if (string[0] == '.' && PATHSEP(string[1])) /* . and ./ */ | |
526 | return (1); | |
527 | ||
528 | if (string[0] == '.' && string[1] == '.' && PATHSEP(string[2])) /* .. and ../ */ | |
529 | return (1); | |
530 | ||
531 | return (0); | |
532 | } | |
533 | ||
534 | /* Return 1 if STRING is an absolute program name; it is absolute if it | |
535 | contains any slashes. This is used to decide whether or not to look | |
536 | up through $PATH. */ | |
537 | int | |
538 | absolute_program (string) | |
539 | const char *string; | |
540 | { | |
541 | return ((char *)xstrchr (string, '/') != (char *)NULL); | |
542 | } | |
fb65be05 | 543 | |
ccc6cda3 JA |
544 | /* **************************************************************** */ |
545 | /* */ | |
546 | /* Functions to manipulate pathnames */ | |
547 | /* */ | |
548 | /* **************************************************************** */ | |
726f6388 | 549 | |
726f6388 JA |
550 | /* Turn STRING (a pathname) into an absolute pathname, assuming that |
551 | DOT_PATH contains the symbolic location of `.'. This always | |
552 | returns a new string, even if STRING was an absolute pathname to | |
553 | begin with. */ | |
554 | char * | |
555 | make_absolute (string, dot_path) | |
556 | char *string, *dot_path; | |
557 | { | |
558 | char *result; | |
ccc6cda3 | 559 | |
28ef6c31 | 560 | if (dot_path == 0 || ABSPATH(string)) |
d3a24ed2 CR |
561 | #ifdef __CYGWIN__ |
562 | { | |
563 | char pathbuf[PATH_MAX + 1]; | |
564 | ||
565 | cygwin_conv_to_full_posix_path (string, pathbuf); | |
566 | result = savestring (pathbuf); | |
567 | } | |
568 | #else | |
726f6388 | 569 | result = savestring (string); |
d3a24ed2 | 570 | #endif |
726f6388 | 571 | else |
bb70624e | 572 | result = sh_makepath (dot_path, string, 0); |
726f6388 JA |
573 | |
574 | return (result); | |
575 | } | |
576 | ||
726f6388 | 577 | /* Return the `basename' of the pathname in STRING (the stuff after the |
898cc92e | 578 | last '/'). If STRING is `/', just return it. */ |
726f6388 JA |
579 | char * |
580 | base_pathname (string) | |
581 | char *string; | |
582 | { | |
583 | char *p; | |
584 | ||
898cc92e | 585 | #if 0 |
28ef6c31 | 586 | if (absolute_pathname (string) == 0) |
726f6388 | 587 | return (string); |
898cc92e CR |
588 | #endif |
589 | ||
590 | if (string[0] == '/' && string[1] == 0) | |
591 | return (string); | |
726f6388 JA |
592 | |
593 | p = (char *)strrchr (string, '/'); | |
ccc6cda3 | 594 | return (p ? ++p : string); |
726f6388 JA |
595 | } |
596 | ||
597 | /* Return the full pathname of FILE. Easy. Filenames that begin | |
598 | with a '/' are returned as themselves. Other filenames have | |
599 | the current working directory prepended. A new string is | |
600 | returned in either case. */ | |
601 | char * | |
602 | full_pathname (file) | |
603 | char *file; | |
604 | { | |
bb70624e | 605 | char *ret; |
726f6388 | 606 | |
7117c2d2 | 607 | file = (*file == '~') ? bash_tilde_expand (file, 0) : savestring (file); |
726f6388 | 608 | |
28ef6c31 | 609 | if (ABSPATH(file)) |
726f6388 JA |
610 | return (file); |
611 | ||
bb70624e JA |
612 | ret = sh_makepath ((char *)NULL, file, (MP_DOCWD|MP_RMDOT)); |
613 | free (file); | |
726f6388 | 614 | |
bb70624e | 615 | return (ret); |
726f6388 | 616 | } |
726f6388 JA |
617 | |
618 | /* A slightly related function. Get the prettiest name of this | |
619 | directory possible. */ | |
ccc6cda3 | 620 | static char tdir[PATH_MAX]; |
726f6388 JA |
621 | |
622 | /* Return a pretty pathname. If the first part of the pathname is | |
623 | the same as $HOME, then replace that with `~'. */ | |
624 | char * | |
625 | polite_directory_format (name) | |
626 | char *name; | |
627 | { | |
ccc6cda3 JA |
628 | char *home; |
629 | int l; | |
726f6388 | 630 | |
ccc6cda3 JA |
631 | home = get_string_value ("HOME"); |
632 | l = home ? strlen (home) : 0; | |
726f6388 JA |
633 | if (l > 1 && strncmp (home, name, l) == 0 && (!name[l] || name[l] == '/')) |
634 | { | |
e8ce775d | 635 | strncpy (tdir + 1, name + l, sizeof(tdir) - 2); |
726f6388 | 636 | tdir[0] = '~'; |
e8ce775d | 637 | tdir[sizeof(tdir) - 1] = '\0'; |
726f6388 JA |
638 | return (tdir); |
639 | } | |
640 | else | |
641 | return (name); | |
642 | } | |
643 | ||
ccc6cda3 JA |
644 | /* Given a string containing units of information separated by colons, |
645 | return the next one pointed to by (P_INDEX), or NULL if there are no more. | |
646 | Advance (P_INDEX) to the character after the colon. */ | |
647 | char * | |
648 | extract_colon_unit (string, p_index) | |
649 | char *string; | |
650 | int *p_index; | |
726f6388 | 651 | { |
ccc6cda3 JA |
652 | int i, start, len; |
653 | char *value; | |
726f6388 | 654 | |
ccc6cda3 JA |
655 | if (string == 0) |
656 | return (string); | |
726f6388 | 657 | |
ccc6cda3 JA |
658 | len = strlen (string); |
659 | if (*p_index >= len) | |
660 | return ((char *)NULL); | |
726f6388 | 661 | |
ccc6cda3 | 662 | i = *p_index; |
726f6388 | 663 | |
ccc6cda3 JA |
664 | /* Each call to this routine leaves the index pointing at a colon if |
665 | there is more to the path. If I is > 0, then increment past the | |
666 | `:'. If I is 0, then the path has a leading colon. Trailing colons | |
667 | are handled OK by the `else' part of the if statement; an empty | |
668 | string is returned in that case. */ | |
669 | if (i && string[i] == ':') | |
670 | i++; | |
671 | ||
672 | for (start = i; string[i] && string[i] != ':'; i++) | |
673 | ; | |
674 | ||
675 | *p_index = i; | |
676 | ||
677 | if (i == start) | |
678 | { | |
679 | if (string[i]) | |
680 | (*p_index)++; | |
681 | /* Return "" in the case of a trailing `:'. */ | |
f73dda09 | 682 | value = (char *)xmalloc (1); |
ccc6cda3 JA |
683 | value[0] = '\0'; |
684 | } | |
685 | else | |
bb70624e | 686 | value = substring (string, start, i); |
ccc6cda3 JA |
687 | |
688 | return (value); | |
726f6388 | 689 | } |
726f6388 JA |
690 | |
691 | /* **************************************************************** */ | |
692 | /* */ | |
693 | /* Tilde Initialization and Expansion */ | |
694 | /* */ | |
695 | /* **************************************************************** */ | |
696 | ||
cce855bc JA |
697 | #if defined (PUSHD_AND_POPD) |
698 | extern char *get_dirstack_from_string __P((char *)); | |
699 | #endif | |
700 | ||
f73dda09 | 701 | static char **bash_tilde_prefixes; |
22e63b05 | 702 | static char **bash_tilde_prefixes2; |
f73dda09 | 703 | static char **bash_tilde_suffixes; |
22e63b05 | 704 | static char **bash_tilde_suffixes2; |
f73dda09 | 705 | |
726f6388 JA |
706 | /* If tilde_expand hasn't been able to expand the text, perhaps it |
707 | is a special shell expansion. This function is installed as the | |
cce855bc JA |
708 | tilde_expansion_preexpansion_hook. It knows how to expand ~- and ~+. |
709 | If PUSHD_AND_POPD is defined, ~[+-]N expands to directories from the | |
710 | directory stack. */ | |
726f6388 | 711 | static char * |
d166f048 | 712 | bash_special_tilde_expansions (text) |
726f6388 JA |
713 | char *text; |
714 | { | |
ccc6cda3 | 715 | char *result; |
726f6388 | 716 | |
ccc6cda3 | 717 | result = (char *)NULL; |
cce855bc JA |
718 | |
719 | if (text[0] == '+' && text[1] == '\0') | |
720 | result = get_string_value ("PWD"); | |
721 | else if (text[0] == '-' && text[1] == '\0') | |
722 | result = get_string_value ("OLDPWD"); | |
723 | #if defined (PUSHD_AND_POPD) | |
f73dda09 | 724 | else if (DIGIT (*text) || ((*text == '+' || *text == '-') && DIGIT (text[1]))) |
cce855bc JA |
725 | result = get_dirstack_from_string (text); |
726 | #endif | |
726f6388 | 727 | |
ccc6cda3 | 728 | return (result ? savestring (result) : (char *)NULL); |
726f6388 JA |
729 | } |
730 | ||
731 | /* Initialize the tilde expander. In Bash, we handle `~-' and `~+', as | |
732 | well as handling special tilde prefixes; `:~" and `=~' are indications | |
733 | that we should do tilde expansion. */ | |
734 | void | |
735 | tilde_initialize () | |
736 | { | |
737 | static int times_called = 0; | |
738 | ||
d166f048 | 739 | /* Tell the tilde expander that we want a crack first. */ |
f73dda09 | 740 | tilde_expansion_preexpansion_hook = bash_special_tilde_expansions; |
726f6388 JA |
741 | |
742 | /* Tell the tilde expander about special strings which start a tilde | |
743 | expansion, and the special strings that end one. Only do this once. | |
744 | tilde_initialize () is called from within bashline_reinitialize (). */ | |
cce855bc | 745 | if (times_called++ == 0) |
726f6388 | 746 | { |
7117c2d2 | 747 | bash_tilde_prefixes = strvec_create (3); |
f73dda09 JA |
748 | bash_tilde_prefixes[0] = "=~"; |
749 | bash_tilde_prefixes[1] = ":~"; | |
750 | bash_tilde_prefixes[2] = (char *)NULL; | |
751 | ||
22e63b05 CR |
752 | bash_tilde_prefixes2 = strvec_create (2); |
753 | bash_tilde_prefixes2[0] = ":~"; | |
754 | bash_tilde_prefixes2[1] = (char *)NULL; | |
755 | ||
f73dda09 JA |
756 | tilde_additional_prefixes = bash_tilde_prefixes; |
757 | ||
7117c2d2 | 758 | bash_tilde_suffixes = strvec_create (3); |
f73dda09 JA |
759 | bash_tilde_suffixes[0] = ":"; |
760 | bash_tilde_suffixes[1] = "=~"; /* XXX - ?? */ | |
761 | bash_tilde_suffixes[2] = (char *)NULL; | |
762 | ||
763 | tilde_additional_suffixes = bash_tilde_suffixes; | |
22e63b05 CR |
764 | |
765 | bash_tilde_suffixes2 = strvec_create (2); | |
766 | bash_tilde_suffixes2[0] = ":"; | |
767 | bash_tilde_suffixes2[1] = (char *)NULL; | |
726f6388 | 768 | } |
726f6388 JA |
769 | } |
770 | ||
f73dda09 JA |
771 | /* POSIX.2, 3.6.1: A tilde-prefix consists of an unquoted tilde character |
772 | at the beginning of the word, followed by all of the characters preceding | |
773 | the first unquoted slash in the word, or all the characters in the word | |
774 | if there is no slash...If none of the characters in the tilde-prefix are | |
775 | quoted, the characters in the tilde-prefix following the tilde shell be | |
776 | treated as a possible login name. */ | |
777 | ||
778 | #define TILDE_END(c) ((c) == '\0' || (c) == '/' || (c) == ':') | |
779 | ||
780 | static int | |
781 | unquoted_tilde_word (s) | |
782 | const char *s; | |
783 | { | |
784 | const char *r; | |
785 | ||
786 | for (r = s; TILDE_END(*r) == 0; r++) | |
787 | { | |
788 | switch (*r) | |
789 | { | |
790 | case '\\': | |
791 | case '\'': | |
792 | case '"': | |
793 | return 0; | |
794 | } | |
795 | } | |
796 | return 1; | |
797 | } | |
798 | ||
22e63b05 CR |
799 | /* Find the end of the tilde-prefix starting at S, and return the tilde |
800 | prefix in newly-allocated memory. Return the length of the string in | |
801 | *LENP. FLAGS tells whether or not we're in an assignment context -- | |
802 | if so, `:' delimits the end of the tilde prefix as well. */ | |
803 | char * | |
804 | bash_tilde_find_word (s, flags, lenp) | |
805 | const char *s; | |
806 | int flags, *lenp; | |
807 | { | |
808 | const char *r; | |
809 | char *ret; | |
810 | int l; | |
811 | ||
812 | for (r = s; *r && *r != '/'; r++) | |
813 | { | |
814 | /* Short-circuit immediately if we see a quote character. Even though | |
815 | POSIX says that `the first unquoted slash' (or `:') terminates the | |
816 | tilde-prefix, in practice, any quoted portion of the tilde prefix | |
817 | will cause it to not be expanded. */ | |
818 | if (*r == '\\' || *r == '\'' || *r == '"') | |
819 | { | |
820 | ret = savestring (s); | |
821 | if (lenp) | |
822 | *lenp = 0; | |
823 | return ret; | |
824 | } | |
825 | else if (flags && *r == ':') | |
826 | break; | |
827 | } | |
828 | l = r - s; | |
829 | ret = xmalloc (l + 1); | |
830 | strncpy (ret, s, l); | |
831 | ret[l] = '\0'; | |
832 | if (lenp) | |
833 | *lenp = l; | |
834 | return ret; | |
835 | } | |
836 | ||
7117c2d2 JA |
837 | /* Tilde-expand S by running it through the tilde expansion library. |
838 | ASSIGN_P is 1 if this is a variable assignment, so the alternate | |
22e63b05 CR |
839 | tilde prefixes should be enabled (`=~' and `:~', see above). If |
840 | ASSIGN_P is 2, we are expanding the rhs of an assignment statement, | |
841 | so `=~' is not valid. */ | |
ccc6cda3 | 842 | char * |
7117c2d2 | 843 | bash_tilde_expand (s, assign_p) |
f73dda09 | 844 | const char *s; |
7117c2d2 | 845 | int assign_p; |
726f6388 | 846 | { |
f73dda09 | 847 | int old_immed, r; |
ccc6cda3 | 848 | char *ret; |
726f6388 | 849 | |
ccc6cda3 JA |
850 | old_immed = interrupt_immediately; |
851 | interrupt_immediately = 1; | |
22e63b05 CR |
852 | |
853 | tilde_additional_prefixes = assign_p == 0 ? (char **)0 | |
854 | : (assign_p == 2 ? bash_tilde_prefixes2 : bash_tilde_prefixes); | |
855 | if (assign_p == 2) | |
856 | tilde_additional_suffixes = bash_tilde_suffixes2; | |
857 | ||
f73dda09 JA |
858 | r = (*s == '~') ? unquoted_tilde_word (s) : 1; |
859 | ret = r ? tilde_expand (s) : savestring (s); | |
ccc6cda3 JA |
860 | interrupt_immediately = old_immed; |
861 | return (ret); | |
726f6388 | 862 | } |
d166f048 JA |
863 | |
864 | /* **************************************************************** */ | |
865 | /* */ | |
866 | /* Functions to manipulate and search the group list */ | |
867 | /* */ | |
868 | /* **************************************************************** */ | |
869 | ||
870 | static int ngroups, maxgroups; | |
871 | ||
872 | /* The set of groups that this user is a member of. */ | |
873 | static GETGROUPS_T *group_array = (GETGROUPS_T *)NULL; | |
874 | ||
875 | #if !defined (NOGROUP) | |
876 | # define NOGROUP (gid_t) -1 | |
877 | #endif | |
878 | ||
d166f048 JA |
879 | static void |
880 | initialize_group_array () | |
881 | { | |
882 | register int i; | |
883 | ||
884 | if (maxgroups == 0) | |
885 | maxgroups = getmaxgroups (); | |
886 | ||
887 | ngroups = 0; | |
888 | group_array = (GETGROUPS_T *)xrealloc (group_array, maxgroups * sizeof (GETGROUPS_T)); | |
889 | ||
890 | #if defined (HAVE_GETGROUPS) | |
891 | ngroups = getgroups (maxgroups, group_array); | |
892 | #endif | |
893 | ||
894 | /* If getgroups returns nothing, or the OS does not support getgroups(), | |
895 | make sure the groups array includes at least the current gid. */ | |
896 | if (ngroups == 0) | |
897 | { | |
898 | group_array[0] = current_user.gid; | |
899 | ngroups = 1; | |
900 | } | |
901 | ||
902 | /* If the primary group is not in the groups array, add it as group_array[0] | |
903 | and shuffle everything else up 1, if there's room. */ | |
904 | for (i = 0; i < ngroups; i++) | |
905 | if (current_user.gid == (gid_t)group_array[i]) | |
906 | break; | |
907 | if (i == ngroups && ngroups < maxgroups) | |
908 | { | |
909 | for (i = ngroups; i > 0; i--) | |
28ef6c31 | 910 | group_array[i] = group_array[i - 1]; |
d166f048 JA |
911 | group_array[0] = current_user.gid; |
912 | ngroups++; | |
913 | } | |
cce855bc JA |
914 | |
915 | /* If the primary group is not group_array[0], swap group_array[0] and | |
916 | whatever the current group is. The vast majority of systems should | |
917 | not need this; a notable exception is Linux. */ | |
918 | if (group_array[0] != current_user.gid) | |
919 | { | |
920 | for (i = 0; i < ngroups; i++) | |
28ef6c31 JA |
921 | if (group_array[i] == current_user.gid) |
922 | break; | |
cce855bc JA |
923 | if (i < ngroups) |
924 | { | |
925 | group_array[i] = group_array[0]; | |
926 | group_array[0] = current_user.gid; | |
927 | } | |
928 | } | |
d166f048 JA |
929 | } |
930 | ||
931 | /* Return non-zero if GID is one that we have in our groups list. */ | |
932 | int | |
cce855bc JA |
933 | #if defined (__STDC__) || defined ( _MINIX) |
934 | group_member (gid_t gid) | |
935 | #else | |
d166f048 JA |
936 | group_member (gid) |
937 | gid_t gid; | |
cce855bc | 938 | #endif /* !__STDC__ && !_MINIX */ |
d166f048 JA |
939 | { |
940 | #if defined (HAVE_GETGROUPS) | |
941 | register int i; | |
942 | #endif | |
943 | ||
944 | /* Short-circuit if possible, maybe saving a call to getgroups(). */ | |
945 | if (gid == current_user.gid || gid == current_user.egid) | |
946 | return (1); | |
947 | ||
948 | #if defined (HAVE_GETGROUPS) | |
949 | if (ngroups == 0) | |
950 | initialize_group_array (); | |
951 | ||
952 | /* In case of error, the user loses. */ | |
953 | if (ngroups <= 0) | |
954 | return (0); | |
955 | ||
956 | /* Search through the list looking for GID. */ | |
957 | for (i = 0; i < ngroups; i++) | |
958 | if (gid == (gid_t)group_array[i]) | |
959 | return (1); | |
960 | #endif | |
961 | ||
962 | return (0); | |
963 | } | |
964 | ||
965 | char ** | |
966 | get_group_list (ngp) | |
967 | int *ngp; | |
968 | { | |
969 | static char **group_vector = (char **)NULL; | |
970 | register int i; | |
d166f048 JA |
971 | |
972 | if (group_vector) | |
973 | { | |
974 | if (ngp) | |
975 | *ngp = ngroups; | |
976 | return group_vector; | |
977 | } | |
978 | ||
979 | if (ngroups == 0) | |
980 | initialize_group_array (); | |
981 | ||
982 | if (ngroups <= 0) | |
983 | { | |
984 | if (ngp) | |
985 | *ngp = 0; | |
986 | return (char **)NULL; | |
987 | } | |
988 | ||
7117c2d2 | 989 | group_vector = strvec_create (ngroups); |
d166f048 | 990 | for (i = 0; i < ngroups; i++) |
7117c2d2 | 991 | group_vector[i] = itos (group_array[i]); |
b72432fd | 992 | |
d166f048 JA |
993 | if (ngp) |
994 | *ngp = ngroups; | |
995 | return group_vector; | |
996 | } | |
b72432fd JA |
997 | |
998 | int * | |
999 | get_group_array (ngp) | |
1000 | int *ngp; | |
1001 | { | |
1002 | int i; | |
1003 | static int *group_iarray = (int *)NULL; | |
1004 | ||
1005 | if (group_iarray) | |
1006 | { | |
1007 | if (ngp) | |
1008 | *ngp = ngroups; | |
1009 | return (group_iarray); | |
1010 | } | |
1011 | ||
1012 | if (ngroups == 0) | |
1013 | initialize_group_array (); | |
1014 | ||
1015 | if (ngroups <= 0) | |
1016 | { | |
1017 | if (ngp) | |
1018 | *ngp = 0; | |
1019 | return (int *)NULL; | |
1020 | } | |
1021 | ||
1022 | group_iarray = (int *)xmalloc (ngroups * sizeof (int)); | |
1023 | for (i = 0; i < ngroups; i++) | |
1024 | group_iarray[i] = (int)group_array[i]; | |
1025 | ||
1026 | if (ngp) | |
1027 | *ngp = ngroups; | |
1028 | return group_iarray; | |
1029 | } |