source: vendor/bash/3.1-p17/general.c@ 3274

Last change on this file since 3274 was 3232, checked in by bird, 18 years ago

Applied Brendan's patches.

  • Property svn:eol-style set to native
File size: 25.0 KB
Line 
1/* general.c -- Stuff that is used by all files. */
2
3/* Copyright (C) 1987-2004 Free Software Foundation, Inc.
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
19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
20
21#include "config.h"
22
23#include "bashtypes.h"
24#ifndef _MINIX
25# include <sys/param.h>
26#endif
27#include "posixstat.h"
28
29#if defined (HAVE_UNISTD_H)
30# include <unistd.h>
31#endif
32
33#include "filecntl.h"
34#include "bashansi.h"
35#include <stdio.h>
36#include "chartypes.h"
37#include <errno.h>
38
39#include "bashintl.h"
40
41#include "shell.h"
42#include "test.h"
43
44#include <tilde/tilde.h>
45
46#if !defined (errno)
47extern int errno;
48#endif /* !errno */
49
50extern int expand_aliases;
51extern int interrupt_immediately;
52extern int interactive_comments;
53extern int check_hashed_filenames;
54extern int source_uses_path;
55extern int source_searches_cwd;
56
57static char *bash_special_tilde_expansions __P((char *));
58static int unquoted_tilde_word __P((const char *));
59static void initialize_group_array __P((void));
60
61/* A standard error message to use when getcwd() returns NULL. */
62char *bash_getcwd_errstr = N_("getcwd: cannot access parent directories");
63
64/* Do whatever is necessary to initialize `Posix mode'. */
65void
66posix_initialize (on)
67 int on;
68{
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;
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 }
81}
82
83/* **************************************************************** */
84/* */
85/* Functions to convert to and from and display non-standard types */
86/* */
87/* **************************************************************** */
88
89#if defined (RLIMTYPE)
90RLIMTYPE
91string_to_rlimtype (s)
92 char *s;
93{
94 RLIMTYPE ret;
95 int neg;
96
97 ret = 0;
98 neg = 0;
99 while (s && *s && whitespace (*s))
100 s++;
101 if (*s == '-' || *s == '+')
102 {
103 neg = *s == '-';
104 s++;
105 }
106 for ( ; s && *s && DIGIT (*s); s++)
107 ret = (ret * 10) + TODIGIT (*s);
108 return (neg ? -ret : ret);
109}
110
111void
112print_rlimtype (n, addnl)
113 RLIMTYPE n;
114 int addnl;
115{
116 char s[INT_STRLEN_BOUND (RLIMTYPE) + 1], *p;
117
118 p = s + sizeof(s);
119 *--p = '\0';
120
121 if (n < 0)
122 {
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);
134 }
135
136 printf ("%s%s", p, addnl ? "\n" : "");
137}
138#endif /* RLIMTYPE */
139
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{
151 register char *s;
152
153 for (s = string; *s; s++)
154 if (DIGIT (*s) == 0)
155 return (0);
156
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
162 not null. */
163int
164legal_number (string, result)
165 char *string;
166 intmax_t *result;
167{
168 intmax_t value;
169 char *ep;
170
171 if (result)
172 *result = 0;
173
174 errno = 0;
175 value = strtoimax (string, &ep, 10);
176 if (errno)
177 return 0; /* errno is set on overflow or underflow */
178
179 /* Skip any trailing whitespace, since strtoimax does not. */
180 while (whitespace (*ep))
181 ep++;
182
183 /* If *string is not '\0' but *ep is '\0' on return, the entire string
184 is valid. */
185 if (string && *string && *ep == '\0')
186 {
187 if (result)
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;
193 }
194
195 return (0);
196}
197
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;
206 unsigned char c;
207
208 if (!name || !(c = *name) || (legal_variable_starter (c) == 0))
209 return (0);
210
211 for (s = name + 1; (c = *s) != 0; s++)
212 {
213 if (legal_variable_char (c) == 0)
214 return (0);
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. */
224int
225check_identifier (word, check_word)
226 WORD_DESC *word;
227 int check_word;
228{
229 if ((word->flags & (W_HASDOLLAR|W_QUOTED)) || all_digits (word->word))
230 {
231 internal_error (_("`%s': not a valid identifier"), word->word);
232 return (0);
233 }
234 else if (check_word && legal_identifier (word->word) == 0)
235 {
236 internal_error (_("`%s': not a valid identifier"), word->word);
237 return (0);
238 }
239 else
240 return (1);
241}
242
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
259/* Returns non-zero if STRING is an assignment statement. The returned value
260 is the index of the `=' sign. */
261int
262assignment (string, flags)
263 const char *string;
264 int flags;
265{
266 register unsigned char c;
267 register int newi, indx;
268
269 c = string[indx = 0];
270
271#if defined (ARRAY_VARS)
272 if ((legal_variable_starter (c) == 0) && (flags == 0 || c != '[')) /* ] */
273#else
274 if (legal_variable_starter (c) == 0)
275#endif
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);
291 if (string[newi] == '+' && string[newi+1] == '=')
292 return (newi + 1);
293 return ((string[newi] == '=') ? newi : 0);
294 }
295#endif /* ARRAY_VARS */
296
297 /* Check for `+=' */
298 if (c == '+' && string[indx+1] == '=')
299 return (indx + 1);
300
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
311/* **************************************************************** */
312/* */
313/* Functions to manage files and file descriptors */
314/* */
315/* **************************************************************** */
316
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. */
328int
329sh_unset_nodelay_mode (fd)
330 int fd;
331{
332 int flags, bflags;
333
334 if ((flags = fcntl (fd, F_GETFL, 0)) < 0)
335 return -1;
336
337 bflags = 0;
338
339 /* This is defined to O_NDELAY in filecntl.h if O_NONBLOCK is not present
340 and O_NDELAY is defined. */
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)
350 {
351 flags &= ~bflags;
352 return (fcntl (fd, F_SETFL, flags));
353 }
354
355 return 0;
356}
357
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
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
376void
377check_dev_tty ()
378{
379 int tty_fd;
380 char *tty;
381
382 tty_fd = open ("/dev/tty", O_RDWR|O_NONBLOCK);
383
384 if (tty_fd < 0)
385 {
386 tty = (char *)ttyname (fileno (stdin));
387 if (tty == 0)
388 return;
389 tty_fd = open (tty, O_RDWR|O_NONBLOCK);
390 }
391 close (tty_fd);
392}
393
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. */
397int
398same_file (path1, path2, stp1, stp2)
399 char *path1, *path2;
400 struct stat *stp1, *stp2;
401{
402 struct stat st1, st2;
403
404 if (stp1 == NULL)
405 {
406 if (stat (path1, &st1) != 0)
407 return (0);
408 stp1 = &st1;
409 }
410
411 if (stp2 == NULL)
412 {
413 if (stat (path2, &st2) != 0)
414 return (0);
415 stp2 = &st2;
416 }
417
418 return ((stp1->st_dev == stp2->st_dev) && (stp1->st_ino == stp2->st_ino));
419}
420
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
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). */
428int
429move_to_high_fd (fd, check_new, maxfd)
430 int fd, check_new, maxfd;
431{
432 int script_fd, nfds, ignore;
433
434 if (maxfd < 20)
435 {
436 nfds = getdtablesize ();
437 if (nfds <= 0)
438 nfds = 20;
439 if (nfds > HIGH_FD_MAX)
440 nfds = HIGH_FD_MAX; /* reasonable maximum */
441 }
442 else
443 nfds = maxfd;
444
445 for (nfds--; check_new && nfds > 3; nfds--)
446 if (fcntl (nfds, F_GETFD, &ignore) == -1)
447 break;
448
449 if (nfds > 3 && fd != nfds && (script_fd = dup2 (fd, nfds)) != -1)
450 {
451 if (check_new == 0 || fd != fileno (stderr)) /* don't close stderr */
452 close (fd);
453 return (script_fd);
454 }
455
456 /* OK, we didn't find one less than our artificial maximum; return the
457 original file descriptor. */
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. */
465
466int
467check_binary_file (sample, sample_len)
468 char *sample;
469 int sample_len;
470{
471 register int i;
472 unsigned char c;
473
474 for (i = 0; i < sample_len; i++)
475 {
476 c = sample[i];
477 if (c == '\n')
478 return (0);
479
480 if (ISSPACE (c) == 0 && ISPRINT (c) == 0)
481 return (1);
482 }
483
484 return (0);
485}
486
487/* **************************************************************** */
488/* */
489/* Functions to inspect pathnames */
490/* */
491/* **************************************************************** */
492
493int
494file_isdir (fn)
495 char *fn;
496{
497 struct stat sb;
498
499 return ((stat (fn, &sb) == 0) && S_ISDIR (sb.st_mode));
500}
501
502int
503file_iswdir (fn)
504 char *fn;
505{
506 return (file_isdir (fn) && test_eaccess (fn, W_OK) == 0);
507}
508
509/* Return 1 if STRING contains an absolute pathname, else 0. Used by `cd'
510 to decide whether or not to look up a directory name in $CDPATH. */
511int
512absolute_pathname (string)
513 const char *string;
514{
515 if (string == 0 || *string == '\0')
516 return (0);
517
518 if (ABSPATH(string))
519 return (1);
520
521 if (string[0] == '.' && PATHSEP(string[1])) /* . and ./ */
522 return (1);
523
524 if (string[0] == '.' && string[1] == '.' && PATHSEP(string[2])) /* .. and ../ */
525 return (1);
526
527 return (0);
528}
529
530/* Return 1 if STRING is an absolute program name; it is absolute if it
531 contains any slashes. This is used to decide whether or not to look
532 up through $PATH. */
533int
534absolute_program (string)
535 const char *string;
536{
537 return ((char *)xstrchr (string, '/') != (char *)NULL);
538}
539
540/* **************************************************************** */
541/* */
542/* Functions to manipulate pathnames */
543/* */
544/* **************************************************************** */
545
546/* Turn STRING (a pathname) into an absolute pathname, assuming that
547 DOT_PATH contains the symbolic location of `.'. This always
548 returns a new string, even if STRING was an absolute pathname to
549 begin with. */
550char *
551make_absolute (string, dot_path)
552 char *string, *dot_path;
553{
554 char *result;
555
556 if (dot_path == 0 || ABSPATH(string))
557#ifdef __CYGWIN__
558 {
559 char pathbuf[PATH_MAX + 1];
560
561 cygwin_conv_to_full_posix_path (string, pathbuf);
562 result = savestring (pathbuf);
563 }
564#else
565 result = savestring (string);
566#endif
567 else
568 result = sh_makepath (dot_path, string, 0);
569
570 return (result);
571}
572
573/* Return the `basename' of the pathname in STRING (the stuff after the
574 last '/'). If STRING is `/', just return it. */
575char *
576base_pathname (string)
577 char *string;
578{
579 char *p;
580
581#if 0
582 if (absolute_pathname (string) == 0)
583 return (string);
584#endif
585
586 if (string[0] == '/' && string[1] == 0)
587 return (string);
588
589 p = (char *)strrchr (string, '/');
590 return (p ? ++p : string);
591}
592
593/* Return the full pathname of FILE. Easy. Filenames that begin
594 with a '/' are returned as themselves. Other filenames have
595 the current working directory prepended. A new string is
596 returned in either case. */
597char *
598full_pathname (file)
599 char *file;
600{
601 char *ret;
602
603 file = (*file == '~') ? bash_tilde_expand (file, 0) : savestring (file);
604
605 if (ABSPATH(file))
606 return (file);
607
608 ret = sh_makepath ((char *)NULL, file, (MP_DOCWD|MP_RMDOT));
609 free (file);
610
611 return (ret);
612}
613
614/* A slightly related function. Get the prettiest name of this
615 directory possible. */
616static char tdir[PATH_MAX];
617
618/* Return a pretty pathname. If the first part of the pathname is
619 the same as $HOME, then replace that with `~'. */
620char *
621polite_directory_format (name)
622 char *name;
623{
624 char *home;
625 int l;
626
627 home = get_string_value ("HOME");
628 l = home ? strlen (home) : 0;
629 if (l > 1 && strncmp (home, name, l) == 0 && (!name[l] || name[l] == '/'))
630 {
631 strncpy (tdir + 1, name + l, sizeof(tdir) - 2);
632 tdir[0] = '~';
633 tdir[sizeof(tdir) - 1] = '\0';
634 return (tdir);
635 }
636 else
637 return (name);
638}
639
640/* Given a string containing units of information separated by colons,
641 return the next one pointed to by (P_INDEX), or NULL if there are no more.
642 Advance (P_INDEX) to the character after the colon. */
643char *
644extract_colon_unit (string, p_index)
645 char *string;
646 int *p_index;
647{
648 int i, start, len;
649 char *value;
650
651 if (string == 0)
652 return (string);
653
654 len = strlen (string);
655 if (*p_index >= len)
656 return ((char *)NULL);
657
658 i = *p_index;
659
660 /* Each call to this routine leaves the index pointing at a colon if
661 there is more to the path. If I is > 0, then increment past the
662 `:'. If I is 0, then the path has a leading colon. Trailing colons
663 are handled OK by the `else' part of the if statement; an empty
664 string is returned in that case. */
665#ifndef __OS2__
666 if (i && string[i] == ':')
667 i++;
668
669 for (start = i; string[i] && string[i] != ':'; i++)
670 ;
671
672#else /* __OS2__ */
673 if (i && (string[i] == ':' || string[i] ==';'))
674 i++;
675
676 /* In OS/2, interpret either `:' or `;' as separator, except when
677 the `:' looks like part of a drive letter. */
678 for (start = i;; i++)
679 {
680 if (!string[i])
681 break;
682 if (string[i] == ';')
683 break;
684 if (string[i] != ':')
685 continue;
686 if (i - start != 1)
687 break;
688 if (!isalpha (string[i - 1]))
689 break;
690 }
691#endif /* __OS2__ */
692
693 *p_index = i;
694
695 if (i == start)
696 {
697 if (string[i])
698 (*p_index)++;
699 /* Return "" in the case of a trailing `:'. */
700 value = (char *)xmalloc (1);
701 value[0] = '\0';
702 }
703 else
704 value = substring (string, start, i);
705
706 return (value);
707}
708
709/* **************************************************************** */
710/* */
711/* Tilde Initialization and Expansion */
712/* */
713/* **************************************************************** */
714
715#if defined (PUSHD_AND_POPD)
716extern char *get_dirstack_from_string __P((char *));
717#endif
718
719static char **bash_tilde_prefixes;
720static char **bash_tilde_prefixes2;
721static char **bash_tilde_suffixes;
722static char **bash_tilde_suffixes2;
723
724/* If tilde_expand hasn't been able to expand the text, perhaps it
725 is a special shell expansion. This function is installed as the
726 tilde_expansion_preexpansion_hook. It knows how to expand ~- and ~+.
727 If PUSHD_AND_POPD is defined, ~[+-]N expands to directories from the
728 directory stack. */
729static char *
730bash_special_tilde_expansions (text)
731 char *text;
732{
733 char *result;
734
735 result = (char *)NULL;
736
737 if (text[0] == '+' && text[1] == '\0')
738 result = get_string_value ("PWD");
739 else if (text[0] == '-' && text[1] == '\0')
740 result = get_string_value ("OLDPWD");
741#if defined (PUSHD_AND_POPD)
742 else if (DIGIT (*text) || ((*text == '+' || *text == '-') && DIGIT (text[1])))
743 result = get_dirstack_from_string (text);
744#endif
745
746 return (result ? savestring (result) : (char *)NULL);
747}
748
749/* Initialize the tilde expander. In Bash, we handle `~-' and `~+', as
750 well as handling special tilde prefixes; `:~" and `=~' are indications
751 that we should do tilde expansion. */
752void
753tilde_initialize ()
754{
755 static int times_called = 0;
756
757 /* Tell the tilde expander that we want a crack first. */
758 tilde_expansion_preexpansion_hook = bash_special_tilde_expansions;
759
760 /* Tell the tilde expander about special strings which start a tilde
761 expansion, and the special strings that end one. Only do this once.
762 tilde_initialize () is called from within bashline_reinitialize (). */
763 if (times_called++ == 0)
764 {
765 bash_tilde_prefixes = strvec_create (3);
766 bash_tilde_prefixes[0] = "=~";
767 bash_tilde_prefixes[1] = ":~";
768 bash_tilde_prefixes[2] = (char *)NULL;
769
770 bash_tilde_prefixes2 = strvec_create (2);
771 bash_tilde_prefixes2[0] = ":~";
772 bash_tilde_prefixes2[1] = (char *)NULL;
773
774 tilde_additional_prefixes = bash_tilde_prefixes;
775
776 bash_tilde_suffixes = strvec_create (3);
777 bash_tilde_suffixes[0] = ":";
778 bash_tilde_suffixes[1] = "=~"; /* XXX - ?? */
779 bash_tilde_suffixes[2] = (char *)NULL;
780
781 tilde_additional_suffixes = bash_tilde_suffixes;
782
783 bash_tilde_suffixes2 = strvec_create (2);
784 bash_tilde_suffixes2[0] = ":";
785 bash_tilde_suffixes2[1] = (char *)NULL;
786 }
787}
788
789/* POSIX.2, 3.6.1: A tilde-prefix consists of an unquoted tilde character
790 at the beginning of the word, followed by all of the characters preceding
791 the first unquoted slash in the word, or all the characters in the word
792 if there is no slash...If none of the characters in the tilde-prefix are
793 quoted, the characters in the tilde-prefix following the tilde shell be
794 treated as a possible login name. */
795
796#define TILDE_END(c) ((c) == '\0' || (c) == '/' || (c) == ':')
797
798static int
799unquoted_tilde_word (s)
800 const char *s;
801{
802 const char *r;
803
804 for (r = s; TILDE_END(*r) == 0; r++)
805 {
806 switch (*r)
807 {
808 case '\\':
809 case '\'':
810 case '"':
811 return 0;
812 }
813 }
814 return 1;
815}
816
817/* Find the end of the tilde-prefix starting at S, and return the tilde
818 prefix in newly-allocated memory. Return the length of the string in
819 *LENP. FLAGS tells whether or not we're in an assignment context --
820 if so, `:' delimits the end of the tilde prefix as well. */
821char *
822bash_tilde_find_word (s, flags, lenp)
823 const char *s;
824 int flags, *lenp;
825{
826 const char *r;
827 char *ret;
828 int l;
829
830 for (r = s; *r && *r != '/'; r++)
831 {
832 /* Short-circuit immediately if we see a quote character. Even though
833 POSIX says that `the first unquoted slash' (or `:') terminates the
834 tilde-prefix, in practice, any quoted portion of the tilde prefix
835 will cause it to not be expanded. */
836 if (*r == '\\' || *r == '\'' || *r == '"')
837 {
838 ret = savestring (s);
839 if (lenp)
840 *lenp = 0;
841 return ret;
842 }
843 else if (flags && *r == ':')
844 break;
845 }
846 l = r - s;
847 ret = xmalloc (l + 1);
848 strncpy (ret, s, l);
849 ret[l] = '\0';
850 if (lenp)
851 *lenp = l;
852 return ret;
853}
854
855/* Tilde-expand S by running it through the tilde expansion library.
856 ASSIGN_P is 1 if this is a variable assignment, so the alternate
857 tilde prefixes should be enabled (`=~' and `:~', see above). If
858 ASSIGN_P is 2, we are expanding the rhs of an assignment statement,
859 so `=~' is not valid. */
860char *
861bash_tilde_expand (s, assign_p)
862 const char *s;
863 int assign_p;
864{
865 int old_immed, r;
866 char *ret;
867
868 old_immed = interrupt_immediately;
869 interrupt_immediately = 1;
870
871 tilde_additional_prefixes = assign_p == 0 ? (char **)0
872 : (assign_p == 2 ? bash_tilde_prefixes2 : bash_tilde_prefixes);
873 if (assign_p == 2)
874 tilde_additional_suffixes = bash_tilde_suffixes2;
875
876 r = (*s == '~') ? unquoted_tilde_word (s) : 1;
877 ret = r ? tilde_expand (s) : savestring (s);
878 interrupt_immediately = old_immed;
879 return (ret);
880}
881
882/* **************************************************************** */
883/* */
884/* Functions to manipulate and search the group list */
885/* */
886/* **************************************************************** */
887
888static int ngroups, maxgroups;
889
890/* The set of groups that this user is a member of. */
891static GETGROUPS_T *group_array = (GETGROUPS_T *)NULL;
892
893#if !defined (NOGROUP)
894# define NOGROUP (gid_t) -1
895#endif
896
897static void
898initialize_group_array ()
899{
900 register int i;
901
902 if (maxgroups == 0)
903 maxgroups = getmaxgroups ();
904
905 ngroups = 0;
906 group_array = (GETGROUPS_T *)xrealloc (group_array, maxgroups * sizeof (GETGROUPS_T));
907
908#if defined (HAVE_GETGROUPS)
909 ngroups = getgroups (maxgroups, group_array);
910#endif
911
912 /* If getgroups returns nothing, or the OS does not support getgroups(),
913 make sure the groups array includes at least the current gid. */
914 if (ngroups == 0)
915 {
916 group_array[0] = current_user.gid;
917 ngroups = 1;
918 }
919
920 /* If the primary group is not in the groups array, add it as group_array[0]
921 and shuffle everything else up 1, if there's room. */
922 for (i = 0; i < ngroups; i++)
923 if (current_user.gid == (gid_t)group_array[i])
924 break;
925 if (i == ngroups && ngroups < maxgroups)
926 {
927 for (i = ngroups; i > 0; i--)
928 group_array[i] = group_array[i - 1];
929 group_array[0] = current_user.gid;
930 ngroups++;
931 }
932
933 /* If the primary group is not group_array[0], swap group_array[0] and
934 whatever the current group is. The vast majority of systems should
935 not need this; a notable exception is Linux. */
936 if (group_array[0] != current_user.gid)
937 {
938 for (i = 0; i < ngroups; i++)
939 if (group_array[i] == current_user.gid)
940 break;
941 if (i < ngroups)
942 {
943 group_array[i] = group_array[0];
944 group_array[0] = current_user.gid;
945 }
946 }
947}
948
949/* Return non-zero if GID is one that we have in our groups list. */
950int
951#if defined (__STDC__) || defined ( _MINIX)
952group_member (gid_t gid)
953#else
954group_member (gid)
955 gid_t gid;
956#endif /* !__STDC__ && !_MINIX */
957{
958#if defined (HAVE_GETGROUPS)
959 register int i;
960#endif
961
962 /* Short-circuit if possible, maybe saving a call to getgroups(). */
963 if (gid == current_user.gid || gid == current_user.egid)
964 return (1);
965
966#if defined (HAVE_GETGROUPS)
967 if (ngroups == 0)
968 initialize_group_array ();
969
970 /* In case of error, the user loses. */
971 if (ngroups <= 0)
972 return (0);
973
974 /* Search through the list looking for GID. */
975 for (i = 0; i < ngroups; i++)
976 if (gid == (gid_t)group_array[i])
977 return (1);
978#endif
979
980 return (0);
981}
982
983char **
984get_group_list (ngp)
985 int *ngp;
986{
987 static char **group_vector = (char **)NULL;
988 register int i;
989
990 if (group_vector)
991 {
992 if (ngp)
993 *ngp = ngroups;
994 return group_vector;
995 }
996
997 if (ngroups == 0)
998 initialize_group_array ();
999
1000 if (ngroups <= 0)
1001 {
1002 if (ngp)
1003 *ngp = 0;
1004 return (char **)NULL;
1005 }
1006
1007 group_vector = strvec_create (ngroups);
1008 for (i = 0; i < ngroups; i++)
1009 group_vector[i] = itos (group_array[i]);
1010
1011 if (ngp)
1012 *ngp = ngroups;
1013 return group_vector;
1014}
1015
1016int *
1017get_group_array (ngp)
1018 int *ngp;
1019{
1020 int i;
1021 static int *group_iarray = (int *)NULL;
1022
1023 if (group_iarray)
1024 {
1025 if (ngp)
1026 *ngp = ngroups;
1027 return (group_iarray);
1028 }
1029
1030 if (ngroups == 0)
1031 initialize_group_array ();
1032
1033 if (ngroups <= 0)
1034 {
1035 if (ngp)
1036 *ngp = 0;
1037 return (int *)NULL;
1038 }
1039
1040 group_iarray = (int *)xmalloc (ngroups * sizeof (int));
1041 for (i = 0; i < ngroups; i++)
1042 group_iarray[i] = (int)group_array[i];
1043
1044 if (ngp)
1045 *ngp = ngroups;
1046 return group_iarray;
1047}
Note: See TracBrowser for help on using the repository browser.