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