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