| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | /* general.c -- Stuff that is used by all files. */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | /* Copyright (C) 1987-2004 Free Software Foundation, Inc.
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    This file is part of GNU Bash, the Bourne Again SHell. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Bash is free software; you can redistribute it and/or modify it under | 
					
						
							|  |  |  |    the terms of the GNU General Public License as published by the Free | 
					
						
							|  |  |  |    Software Foundation; either version 2, or (at your option) any later | 
					
						
							|  |  |  |    version. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Bash is distributed in the hope that it will be useful, but WITHOUT ANY | 
					
						
							|  |  |  |    WARRANTY; without even the implied warranty of MERCHANTABILITY or | 
					
						
							|  |  |  |    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License | 
					
						
							|  |  |  |    for more details. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    You should have received a copy of the GNU General Public License along | 
					
						
							|  |  |  |    with Bash; see the file COPYING.  If not, write to the Free Software | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |    Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #include "config.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | #include "bashtypes.h"
 | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  | #ifndef _MINIX
 | 
					
						
							|  |  |  | #  include <sys/param.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #include "posixstat.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (HAVE_UNISTD_H)
 | 
					
						
							|  |  |  | #  include <unistd.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | #include "filecntl.h"
 | 
					
						
							|  |  |  | #include "bashansi.h"
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | #include "chartypes.h"
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #include "bashintl.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | #include "shell.h"
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | #include "test.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | #include <tilde/tilde.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if !defined (errno)
 | 
					
						
							|  |  |  | extern int errno; | 
					
						
							|  |  |  | #endif /* !errno */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | extern int expand_aliases; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | extern int interactive_comments; | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | extern int check_hashed_filenames; | 
					
						
							|  |  |  | extern int source_uses_path; | 
					
						
							|  |  |  | extern int source_searches_cwd; | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | static char *bash_special_tilde_expansions __P((char *)); | 
					
						
							|  |  |  | static int unquoted_tilde_word __P((const char *)); | 
					
						
							|  |  |  | static void initialize_group_array __P((void)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  | /* A standard error message to use when getcwd() returns NULL. */ | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | char *bash_getcwd_errstr = N_("getcwd: cannot access parent directories"); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | /* Do whatever is necessary to initialize `Posix mode'. */ | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | posix_initialize (on) | 
					
						
							|  |  |  |      int on; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   /* Things that should be turned on when posix mode is enabled. */ | 
					
						
							|  |  |  |   if (on != 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       interactive_comments = source_uses_path = expand_aliases = 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Things that should be turned on when posix mode is disabled. */ | 
					
						
							|  |  |  |   if (on == 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       source_searches_cwd = 1; | 
					
						
							|  |  |  |       expand_aliases = interactive_shell; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | /* **************************************************************** */ | 
					
						
							|  |  |  | /*								    */ | 
					
						
							|  |  |  | /*  Functions to convert to and from and display non-standard types */ | 
					
						
							|  |  |  | /*								    */ | 
					
						
							|  |  |  | /* **************************************************************** */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | #if defined (RLIMTYPE)
 | 
					
						
							|  |  |  | RLIMTYPE | 
					
						
							|  |  |  | string_to_rlimtype (s) | 
					
						
							|  |  |  |      char *s; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  |   RLIMTYPE ret; | 
					
						
							|  |  |  |   int neg; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  |   ret = 0; | 
					
						
							|  |  |  |   neg = 0; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |   while (s && *s && whitespace (*s)) | 
					
						
							|  |  |  |     s++; | 
					
						
							|  |  |  |   if (*s == '-' || *s == '+') | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       neg = *s == '-'; | 
					
						
							|  |  |  |       s++; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   for ( ; s && *s && DIGIT (*s); s++) | 
					
						
							|  |  |  |     ret = (ret * 10) + TODIGIT (*s); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |   return (neg ? -ret : ret); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | print_rlimtype (n, addnl) | 
					
						
							|  |  |  |      RLIMTYPE n; | 
					
						
							|  |  |  |      int addnl; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   char s[INT_STRLEN_BOUND (RLIMTYPE) + 1], *p; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   p = s + sizeof(s); | 
					
						
							|  |  |  |   *--p = '\0'; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (n < 0) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       do | 
					
						
							|  |  |  | 	*--p = '0' - n % 10; | 
					
						
							|  |  |  |       while ((n /= 10) != 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       *--p = '-'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       do | 
					
						
							|  |  |  | 	*--p = '0' + n % 10; | 
					
						
							|  |  |  |       while ((n /= 10) != 0); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   printf ("%s%s", p, addnl ? "\n" : ""); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | #endif /* RLIMTYPE */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | /* **************************************************************** */ | 
					
						
							|  |  |  | /*								    */ | 
					
						
							|  |  |  | /*		       Input Validation Functions		    */ | 
					
						
							|  |  |  | /*								    */ | 
					
						
							|  |  |  | /* **************************************************************** */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Return non-zero if all of the characters in STRING are digits. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | all_digits (string) | 
					
						
							|  |  |  |      char *string; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   register char *s; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (s = string; *s; s++) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |     if (DIGIT (*s) == 0) | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |       return (0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   return (1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Return non-zero if the characters pointed to by STRING constitute a
 | 
					
						
							|  |  |  |    valid number.  Stuff the converted number into RESULT if RESULT is | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |    not null. */ | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | int | 
					
						
							|  |  |  | legal_number (string, result) | 
					
						
							|  |  |  |      char *string; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |      intmax_t *result; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   intmax_t value; | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  |   char *ep; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (result) | 
					
						
							|  |  |  |     *result = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   errno = 0; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   value = strtoimax (string, &ep, 10); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   if (errno) | 
					
						
							|  |  |  |     return 0;	/* errno is set on overflow or underflow */ | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   /* Skip any trailing whitespace, since strtoimax does not. */ | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   while (whitespace (*ep)) | 
					
						
							|  |  |  |     ep++; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  |   /* If *string is not '\0' but *ep is '\0' on return, the entire string
 | 
					
						
							|  |  |  |      is valid. */ | 
					
						
							|  |  |  |   if (string && *string && *ep == '\0') | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       if (result) | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  | 	*result = value; | 
					
						
							|  |  |  |       /* The SunOS4 implementation of strtol() will happily ignore
 | 
					
						
							|  |  |  | 	 overflow conditions, so this cannot do overflow correctly | 
					
						
							|  |  |  | 	 on those systems. */ | 
					
						
							|  |  |  |       return 1; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  |      | 
					
						
							|  |  |  |   return (0); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | /* Return 1 if this token is a legal shell `identifier'; that is, it consists
 | 
					
						
							|  |  |  |    solely of letters, digits, and underscores, and does not begin with a | 
					
						
							|  |  |  |    digit. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | legal_identifier (name) | 
					
						
							|  |  |  |      char *name; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   register char *s; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   unsigned char c; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   if (!name || !(c = *name) || (legal_variable_starter (c) == 0)) | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     return (0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   for (s = name + 1; (c = *s) != 0; s++) | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       if (legal_variable_char (c) == 0) | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | 	return (0); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |   return (1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Make sure that WORD is a valid shell identifier, i.e.
 | 
					
						
							|  |  |  |    does not contain a dollar sign, nor is quoted in any way.  Nor | 
					
						
							|  |  |  |    does it consist of all digits.  If CHECK_WORD is non-zero, | 
					
						
							|  |  |  |    the word is checked to ensure that it consists of only letters, | 
					
						
							|  |  |  |    digits, and underscores. */ | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | check_identifier (word, check_word) | 
					
						
							|  |  |  |      WORD_DESC *word; | 
					
						
							|  |  |  |      int check_word; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   if ((word->flags & (W_HASDOLLAR|W_QUOTED)) || all_digits (word->word)) | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |       internal_error (_("`%s': not a valid identifier"), word->word); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |       return (0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else if (check_word && legal_identifier (word->word) == 0) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |       internal_error (_("`%s': not a valid identifier"), word->word); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |       return (0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     return (1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | /* Return 1 if STRING comprises a valid alias name.  The shell accepts
 | 
					
						
							|  |  |  |    essentially all characters except those which must be quoted to the | 
					
						
							|  |  |  |    parser (which disqualifies them from alias expansion anyway) and `/'. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | legal_alias_name (string, flags) | 
					
						
							|  |  |  |      char *string; | 
					
						
							|  |  |  |      int flags; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   register char *s; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (s = string; *s; s++) | 
					
						
							|  |  |  |     if (shellbreak (*s) || shellxquote (*s) || shellexp (*s) || (*s == '/')) | 
					
						
							|  |  |  |       return 0; | 
					
						
							|  |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | /* Returns non-zero if STRING is an assignment statement.  The returned value
 | 
					
						
							|  |  |  |    is the index of the `=' sign. */ | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | assignment (string, flags) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |      const char *string; | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  |      int flags; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   register unsigned char c; | 
					
						
							|  |  |  |   register int newi, indx; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   c = string[indx = 0]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #if defined (ARRAY_VARS)
 | 
					
						
							| 
									
										
										
										
											2004-11-09 21:37:25 +00:00
										 |  |  |   if ((legal_variable_starter (c) == 0) && (flags == 0 || c != '[')) /* ] */ | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   if (legal_variable_starter (c) == 0) | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |     return (0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   while (c = string[indx]) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       /* The following is safe.  Note that '=' at the start of a word
 | 
					
						
							|  |  |  | 	 is not an assignment statement. */ | 
					
						
							|  |  |  |       if (c == '=') | 
					
						
							|  |  |  | 	return (indx); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (ARRAY_VARS)
 | 
					
						
							|  |  |  |       if (c == '[') | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  newi = skipsubscript (string, indx); | 
					
						
							|  |  |  | 	  if (string[newi++] != ']') | 
					
						
							|  |  |  | 	    return (0); | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | 	  if (string[newi] == '+' && string[newi+1] == '=') | 
					
						
							|  |  |  | 	    return (newi + 1); | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | 	  return ((string[newi] == '=') ? newi : 0); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif /* ARRAY_VARS */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |       /* Check for `+=' */ | 
					
						
							|  |  |  |       if (c == '+' && string[indx+1] == '=') | 
					
						
							|  |  |  | 	return (indx + 1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |       /* Variable names in assignment statements may contain only letters,
 | 
					
						
							|  |  |  | 	 digits, and `_'. */ | 
					
						
							|  |  |  |       if (legal_variable_char (c) == 0) | 
					
						
							|  |  |  | 	return (0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       indx++; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   return (0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | /* **************************************************************** */ | 
					
						
							|  |  |  | /*								    */ | 
					
						
							|  |  |  | /*	     Functions to manage files and file descriptors	    */ | 
					
						
							|  |  |  | /*								    */ | 
					
						
							|  |  |  | /* **************************************************************** */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | /* A function to unset no-delay mode on a file descriptor.  Used in shell.c
 | 
					
						
							|  |  |  |    to unset it on the fd passed as stdin.  Should be called on stdin if | 
					
						
							|  |  |  |    readline gets an EAGAIN or EWOULDBLOCK when trying to read input. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if !defined (O_NDELAY)
 | 
					
						
							|  |  |  | #  if defined (FNDELAY)
 | 
					
						
							|  |  |  | #    define O_NDELAY FNDELAY
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							|  |  |  | #endif /* O_NDELAY */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Make sure no-delay mode is not set on file descriptor FD. */ | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | sh_unset_nodelay_mode (fd) | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |      int fd; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |   int flags, bflags; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if ((flags = fcntl (fd, F_GETFL, 0)) < 0) | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |   bflags = 0; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* This is defined to O_NDELAY in filecntl.h if O_NONBLOCK is not present
 | 
					
						
							|  |  |  |      and O_NDELAY is defined. */ | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  | #ifdef O_NONBLOCK
 | 
					
						
							|  |  |  |   bflags |= O_NONBLOCK; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef O_NDELAY
 | 
					
						
							|  |  |  |   bflags |= O_NDELAY; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (flags & bflags) | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |       flags &= ~bflags; | 
					
						
							|  |  |  |       return (fcntl (fd, F_SETFL, flags)); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | /* Return 1 if file descriptor FD is valid; 0 otherwise. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | sh_validfd (fd) | 
					
						
							|  |  |  |      int fd; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return (fcntl (fd, F_GETFD, 0) >= 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-02-19 17:11:39 +00:00
										 |  |  | /* There is a bug in the NeXT 2.1 rlogind that causes opens
 | 
					
						
							|  |  |  |    of /dev/tty to fail. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (__BEOS__)
 | 
					
						
							|  |  |  | /* On BeOS, opening in non-blocking mode exposes a bug in BeOS, so turn it
 | 
					
						
							|  |  |  |    into a no-op.  This should probably go away in the future. */ | 
					
						
							|  |  |  | #  undef O_NONBLOCK
 | 
					
						
							|  |  |  | #  define O_NONBLOCK 0
 | 
					
						
							|  |  |  | #endif /* __BEOS__ */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | check_dev_tty () | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   int tty_fd; | 
					
						
							|  |  |  |   char *tty; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  |   tty_fd = open ("/dev/tty", O_RDWR|O_NONBLOCK); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   if (tty_fd < 0) | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |       tty = (char *)ttyname (fileno (stdin)); | 
					
						
							|  |  |  |       if (tty == 0) | 
					
						
							|  |  |  | 	return; | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  |       tty_fd = open (tty, O_RDWR|O_NONBLOCK); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   close (tty_fd); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | /* Return 1 if PATH1 and PATH2 are the same file.  This is kind of
 | 
					
						
							|  |  |  |    expensive.  If non-NULL STP1 and STP2 point to stat structures | 
					
						
							|  |  |  |    corresponding to PATH1 and PATH2, respectively. */ | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | same_file (path1, path2, stp1, stp2) | 
					
						
							|  |  |  |      char *path1, *path2; | 
					
						
							|  |  |  |      struct stat *stp1, *stp2; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   struct stat st1, st2; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   if (stp1 == NULL) | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |       if (stat (path1, &st1) != 0) | 
					
						
							|  |  |  | 	return (0); | 
					
						
							|  |  |  |       stp1 = &st1; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   if (stp2 == NULL) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (stat (path2, &st2) != 0) | 
					
						
							|  |  |  | 	return (0); | 
					
						
							|  |  |  |       stp2 = &st2; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   return ((stp1->st_dev == stp2->st_dev) && (stp1->st_ino == stp2->st_ino)); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | /* Move FD to a number close to the maximum number of file descriptors
 | 
					
						
							|  |  |  |    allowed in the shell process, to avoid the user stepping on it with | 
					
						
							|  |  |  |    redirection and causing us extra work.  If CHECK_NEW is non-zero, | 
					
						
							|  |  |  |    we check whether or not the file descriptors are in use before | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  |    duplicating FD onto them.  MAXFD says where to start checking the | 
					
						
							|  |  |  |    file descriptors.  If it's less than 20, we get the maximum value | 
					
						
							|  |  |  |    available from getdtablesize(2). */ | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | move_to_high_fd (fd, check_new, maxfd) | 
					
						
							|  |  |  |      int fd, check_new, maxfd; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   int script_fd, nfds, ignore; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  |   if (maxfd < 20) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       nfds = getdtablesize (); | 
					
						
							|  |  |  |       if (nfds <= 0) | 
					
						
							|  |  |  | 	nfds = 20; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |       if (nfds > HIGH_FD_MAX) | 
					
						
							|  |  |  | 	nfds = HIGH_FD_MAX;		/* reasonable maximum */ | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     nfds = maxfd; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   for (nfds--; check_new && nfds > 3; nfds--) | 
					
						
							|  |  |  |     if (fcntl (nfds, F_GETFD, &ignore) == -1) | 
					
						
							|  |  |  |       break; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   if (nfds > 3 && fd != nfds && (script_fd = dup2 (fd, nfds)) != -1) | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |       if (check_new == 0 || fd != fileno (stderr))	/* don't close stderr */ | 
					
						
							|  |  |  | 	close (fd); | 
					
						
							|  |  |  |       return (script_fd); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   /* OK, we didn't find one less than our artificial maximum; return the
 | 
					
						
							|  |  |  |      original file descriptor. */ | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   return (fd); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  | /* Return non-zero if the characters from SAMPLE are not all valid
 | 
					
						
							|  |  |  |    characters to be found in the first line of a shell script.  We | 
					
						
							|  |  |  |    check up to the first newline, or SAMPLE_LEN, whichever comes first. | 
					
						
							|  |  |  |    All of the characters must be printable or whitespace. */ | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | check_binary_file (sample, sample_len) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |      char *sample; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |      int sample_len; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   register int i; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   unsigned char c; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   for (i = 0; i < sample_len; i++) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       c = sample[i]; | 
					
						
							|  |  |  |       if (c == '\n') | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	return (0); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  | #if 0
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       if (ISSPACE (c) == 0 && ISPRINT (c) == 0) | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  |       if (c == '\0') | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 	return (1); | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  |        | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   return (0); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | /* **************************************************************** */ | 
					
						
							|  |  |  | /*								    */ | 
					
						
							|  |  |  | /*		    Functions to inspect pathnames		    */ | 
					
						
							|  |  |  | /*								    */ | 
					
						
							|  |  |  | /* **************************************************************** */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | file_isdir (fn) | 
					
						
							|  |  |  |      char *fn; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   struct stat sb; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ((stat (fn, &sb) == 0) && S_ISDIR (sb.st_mode)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | file_iswdir (fn) | 
					
						
							|  |  |  |      char *fn; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2006-10-10 14:15:34 +00:00
										 |  |  |   return (file_isdir (fn) && sh_eaccess (fn, W_OK) == 0); | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | /* Return 1 if STRING contains an absolute pathname, else 0.  Used by `cd'
 | 
					
						
							|  |  |  |    to decide whether or not to look up a directory name in $CDPATH. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | absolute_pathname (string) | 
					
						
							|  |  |  |      const char *string; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   if (string == 0 || *string == '\0') | 
					
						
							|  |  |  |     return (0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (ABSPATH(string)) | 
					
						
							|  |  |  |     return (1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (string[0] == '.' && PATHSEP(string[1]))	/* . and ./ */ | 
					
						
							|  |  |  |     return (1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (string[0] == '.' && string[1] == '.' && PATHSEP(string[2]))	/* .. and ../ */ | 
					
						
							|  |  |  |     return (1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Return 1 if STRING is an absolute program name; it is absolute if it
 | 
					
						
							|  |  |  |    contains any slashes.  This is used to decide whether or not to look | 
					
						
							|  |  |  |    up through $PATH. */ | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | absolute_program (string) | 
					
						
							|  |  |  |      const char *string; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return ((char *)xstrchr (string, '/') != (char *)NULL); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | /* **************************************************************** */ | 
					
						
							|  |  |  | /*								    */ | 
					
						
							|  |  |  | /*		    Functions to manipulate pathnames		    */ | 
					
						
							|  |  |  | /*								    */ | 
					
						
							|  |  |  | /* **************************************************************** */ | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Turn STRING (a pathname) into an absolute pathname, assuming that
 | 
					
						
							|  |  |  |    DOT_PATH contains the symbolic location of `.'.  This always | 
					
						
							|  |  |  |    returns a new string, even if STRING was an absolute pathname to | 
					
						
							|  |  |  |    begin with. */ | 
					
						
							|  |  |  | char * | 
					
						
							|  |  |  | make_absolute (string, dot_path) | 
					
						
							|  |  |  |      char *string, *dot_path; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char *result; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   if (dot_path == 0 || ABSPATH(string)) | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #ifdef __CYGWIN__
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       char pathbuf[PATH_MAX + 1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       cygwin_conv_to_full_posix_path (string, pathbuf); | 
					
						
							|  |  |  |       result = savestring (pathbuf); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     result = savestring (string); | 
					
						
							| 
									
										
										
										
											2004-07-27 13:29:18 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |     result = sh_makepath (dot_path, string, 0); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return (result); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Return the `basename' of the pathname in STRING (the stuff after the
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |    last '/').  If STRING is `/', just return it. */ | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | char * | 
					
						
							|  |  |  | base_pathname (string) | 
					
						
							|  |  |  |      char *string; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   char *p; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | #if 0
 | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   if (absolute_pathname (string) == 0) | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     return (string); | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (string[0] == '/' && string[1] == 0) | 
					
						
							|  |  |  |     return (string); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   p = (char *)strrchr (string, '/'); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   return (p ? ++p : string); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Return the full pathname of FILE.  Easy.  Filenames that begin
 | 
					
						
							|  |  |  |    with a '/' are returned as themselves.  Other filenames have | 
					
						
							|  |  |  |    the current working directory prepended.  A new string is | 
					
						
							|  |  |  |    returned in either case. */ | 
					
						
							|  |  |  | char * | 
					
						
							|  |  |  | full_pathname (file) | 
					
						
							|  |  |  |      char *file; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |   char *ret; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   file = (*file == '~') ? bash_tilde_expand (file, 0) : savestring (file); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   if (ABSPATH(file)) | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     return (file); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |   ret = sh_makepath ((char *)NULL, file, (MP_DOCWD|MP_RMDOT)); | 
					
						
							|  |  |  |   free (file); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |   return (ret); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* A slightly related function.  Get the prettiest name of this
 | 
					
						
							|  |  |  |    directory possible. */ | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | static char tdir[PATH_MAX]; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Return a pretty pathname.  If the first part of the pathname is
 | 
					
						
							|  |  |  |    the same as $HOME, then replace that with `~'.  */ | 
					
						
							|  |  |  | char * | 
					
						
							|  |  |  | polite_directory_format (name) | 
					
						
							|  |  |  |      char *name; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   char *home; | 
					
						
							|  |  |  |   int l; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   home = get_string_value ("HOME"); | 
					
						
							|  |  |  |   l = home ? strlen (home) : 0; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |   if (l > 1 && strncmp (home, name, l) == 0 && (!name[l] || name[l] == '/')) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											1997-09-22 20:22:27 +00:00
										 |  |  |       strncpy (tdir + 1, name + l, sizeof(tdir) - 2); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |       tdir[0] = '~'; | 
					
						
							| 
									
										
										
										
											1997-09-22 20:22:27 +00:00
										 |  |  |       tdir[sizeof(tdir) - 1] = '\0'; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |       return (tdir); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     return (name); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | /* Given a string containing units of information separated by colons,
 | 
					
						
							|  |  |  |    return the next one pointed to by (P_INDEX), or NULL if there are no more. | 
					
						
							|  |  |  |    Advance (P_INDEX) to the character after the colon. */ | 
					
						
							|  |  |  | char * | 
					
						
							|  |  |  | extract_colon_unit (string, p_index) | 
					
						
							|  |  |  |      char *string; | 
					
						
							|  |  |  |      int *p_index; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   int i, start, len; | 
					
						
							|  |  |  |   char *value; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   if (string == 0) | 
					
						
							|  |  |  |     return (string); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   len = strlen (string); | 
					
						
							|  |  |  |   if (*p_index >= len) | 
					
						
							|  |  |  |     return ((char *)NULL); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   i = *p_index; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   /* Each call to this routine leaves the index pointing at a colon if
 | 
					
						
							|  |  |  |      there is more to the path.  If I is > 0, then increment past the | 
					
						
							|  |  |  |      `:'.  If I is 0, then the path has a leading colon.  Trailing colons | 
					
						
							|  |  |  |      are handled OK by the `else' part of the if statement; an empty | 
					
						
							|  |  |  |      string is returned in that case. */ | 
					
						
							|  |  |  |   if (i && string[i] == ':') | 
					
						
							|  |  |  |     i++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (start = i; string[i] && string[i] != ':'; i++) | 
					
						
							|  |  |  |     ; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   *p_index = i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (i == start) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (string[i]) | 
					
						
							|  |  |  | 	(*p_index)++; | 
					
						
							|  |  |  |       /* Return "" in the case of a trailing `:'. */ | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       value = (char *)xmalloc (1); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |       value[0] = '\0'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else | 
					
						
							| 
									
										
										
										
											2000-03-17 21:46:59 +00:00
										 |  |  |     value = substring (string, start, i); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return (value); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* **************************************************************** */ | 
					
						
							|  |  |  | /*								    */ | 
					
						
							|  |  |  | /*		    Tilde Initialization and Expansion		    */ | 
					
						
							|  |  |  | /*								    */ | 
					
						
							|  |  |  | /* **************************************************************** */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  | #if defined (PUSHD_AND_POPD)
 | 
					
						
							|  |  |  | extern char *get_dirstack_from_string __P((char *)); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | static char **bash_tilde_prefixes; | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | static char **bash_tilde_prefixes2; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | static char **bash_tilde_suffixes; | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | static char **bash_tilde_suffixes2; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | /* If tilde_expand hasn't been able to expand the text, perhaps it
 | 
					
						
							|  |  |  |    is a special shell expansion.  This function is installed as the | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  |    tilde_expansion_preexpansion_hook.  It knows how to expand ~- and ~+. | 
					
						
							|  |  |  |    If PUSHD_AND_POPD is defined, ~[+-]N expands to directories from the | 
					
						
							|  |  |  |    directory stack. */ | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | static char * | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | bash_special_tilde_expansions (text) | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |      char *text; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   char *result; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   result = (char *)NULL; | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (text[0] == '+' && text[1] == '\0') | 
					
						
							|  |  |  |     result = get_string_value ("PWD"); | 
					
						
							|  |  |  |   else if (text[0] == '-' && text[1] == '\0') | 
					
						
							|  |  |  |     result = get_string_value ("OLDPWD"); | 
					
						
							|  |  |  | #if defined (PUSHD_AND_POPD)
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   else if (DIGIT (*text) || ((*text == '+' || *text == '-') && DIGIT (text[1]))) | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  |     result = get_dirstack_from_string (text); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   return (result ? savestring (result) : (char *)NULL); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Initialize the tilde expander.  In Bash, we handle `~-' and `~+', as
 | 
					
						
							|  |  |  |    well as handling special tilde prefixes; `:~" and `=~' are indications | 
					
						
							|  |  |  |    that we should do tilde expansion. */ | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | tilde_initialize () | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   static int times_called = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  |   /* Tell the tilde expander that we want a crack first. */ | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   tilde_expansion_preexpansion_hook = bash_special_tilde_expansions; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* Tell the tilde expander about special strings which start a tilde
 | 
					
						
							|  |  |  |      expansion, and the special strings that end one.  Only do this once. | 
					
						
							|  |  |  |      tilde_initialize () is called from within bashline_reinitialize (). */ | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  |   if (times_called++ == 0) | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |       bash_tilde_prefixes = strvec_create (3); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       bash_tilde_prefixes[0] = "=~"; | 
					
						
							|  |  |  |       bash_tilde_prefixes[1] = ":~"; | 
					
						
							|  |  |  |       bash_tilde_prefixes[2] = (char *)NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |       bash_tilde_prefixes2 = strvec_create (2); | 
					
						
							|  |  |  |       bash_tilde_prefixes2[0] = ":~"; | 
					
						
							|  |  |  |       bash_tilde_prefixes2[1] = (char *)NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       tilde_additional_prefixes = bash_tilde_prefixes; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |       bash_tilde_suffixes = strvec_create (3); | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       bash_tilde_suffixes[0] = ":"; | 
					
						
							|  |  |  |       bash_tilde_suffixes[1] = "=~";	/* XXX - ?? */ | 
					
						
							|  |  |  |       bash_tilde_suffixes[2] = (char *)NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       tilde_additional_suffixes = bash_tilde_suffixes; | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       bash_tilde_suffixes2 = strvec_create (2); | 
					
						
							|  |  |  |       bash_tilde_suffixes2[0] = ":"; | 
					
						
							|  |  |  |       bash_tilde_suffixes2[1] = (char *)NULL; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | /* POSIX.2, 3.6.1:  A tilde-prefix consists of an unquoted tilde character
 | 
					
						
							|  |  |  |    at the beginning of the word, followed by all of the characters preceding | 
					
						
							|  |  |  |    the first unquoted slash in the word, or all the characters in the word | 
					
						
							|  |  |  |    if there is no slash...If none of the characters in the tilde-prefix are | 
					
						
							|  |  |  |    quoted, the characters in the tilde-prefix following the tilde shell be | 
					
						
							|  |  |  |    treated as a possible login name. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define TILDE_END(c)	((c) == '\0' || (c) == '/' || (c) == ':')
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | unquoted_tilde_word (s) | 
					
						
							|  |  |  |      const char *s; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   const char *r; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (r = s; TILDE_END(*r) == 0; r++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       switch (*r) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	case '\\': | 
					
						
							|  |  |  | 	case '\'': | 
					
						
							|  |  |  | 	case '"': | 
					
						
							|  |  |  | 	  return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | /* Find the end of the tilde-prefix starting at S, and return the tilde
 | 
					
						
							|  |  |  |    prefix in newly-allocated memory.  Return the length of the string in | 
					
						
							|  |  |  |    *LENP.  FLAGS tells whether or not we're in an assignment context -- | 
					
						
							|  |  |  |    if so, `:' delimits the end of the tilde prefix as well. */ | 
					
						
							|  |  |  | char * | 
					
						
							|  |  |  | bash_tilde_find_word (s, flags, lenp) | 
					
						
							|  |  |  |      const char *s; | 
					
						
							|  |  |  |      int flags, *lenp; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   const char *r; | 
					
						
							|  |  |  |   char *ret; | 
					
						
							|  |  |  |   int l; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (r = s; *r && *r != '/'; r++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       /* Short-circuit immediately if we see a quote character.  Even though
 | 
					
						
							|  |  |  | 	 POSIX says that `the first unquoted slash' (or `:') terminates the | 
					
						
							|  |  |  | 	 tilde-prefix, in practice, any quoted portion of the tilde prefix | 
					
						
							|  |  |  | 	 will cause it to not be expanded. */ | 
					
						
							|  |  |  |       if (*r == '\\' || *r == '\'' || *r == '"')   | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  ret = savestring (s); | 
					
						
							|  |  |  | 	  if (lenp) | 
					
						
							|  |  |  | 	    *lenp = 0; | 
					
						
							|  |  |  | 	  return ret; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |       else if (flags && *r == ':') | 
					
						
							|  |  |  | 	break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   l = r - s; | 
					
						
							|  |  |  |   ret = xmalloc (l + 1); | 
					
						
							|  |  |  |   strncpy (ret, s, l); | 
					
						
							|  |  |  |   ret[l] = '\0'; | 
					
						
							|  |  |  |   if (lenp) | 
					
						
							|  |  |  |     *lenp = l; | 
					
						
							|  |  |  |   return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | /* Tilde-expand S by running it through the tilde expansion library.
 | 
					
						
							|  |  |  |    ASSIGN_P is 1 if this is a variable assignment, so the alternate | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  |    tilde prefixes should be enabled (`=~' and `:~', see above).  If | 
					
						
							|  |  |  |    ASSIGN_P is 2, we are expanding the rhs of an assignment statement, | 
					
						
							|  |  |  |    so `=~' is not valid. */ | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | char * | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  | bash_tilde_expand (s, assign_p) | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |      const char *s; | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |      int assign_p; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   int old_immed, r; | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   char *ret; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   old_immed = interrupt_immediately; | 
					
						
							|  |  |  |   interrupt_immediately = 1; | 
					
						
							| 
									
										
										
										
											2005-12-07 14:08:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   tilde_additional_prefixes = assign_p == 0 ? (char **)0 | 
					
						
							|  |  |  |   					    : (assign_p == 2 ? bash_tilde_prefixes2 : bash_tilde_prefixes); | 
					
						
							|  |  |  |   if (assign_p == 2) | 
					
						
							|  |  |  |     tilde_additional_suffixes = bash_tilde_suffixes2; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   r = (*s == '~') ? unquoted_tilde_word (s) : 1; | 
					
						
							|  |  |  |   ret = r ? tilde_expand (s) : savestring (s); | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  |   interrupt_immediately = old_immed; | 
					
						
							|  |  |  |   return (ret); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* **************************************************************** */ | 
					
						
							|  |  |  | /*								    */ | 
					
						
							|  |  |  | /*	  Functions to manipulate and search the group list	    */ | 
					
						
							|  |  |  | /*								    */ | 
					
						
							|  |  |  | /* **************************************************************** */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int ngroups, maxgroups; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* The set of groups that this user is a member of. */ | 
					
						
							|  |  |  | static GETGROUPS_T *group_array = (GETGROUPS_T *)NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if !defined (NOGROUP)
 | 
					
						
							|  |  |  | #  define NOGROUP (gid_t) -1
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | initialize_group_array () | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   register int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (maxgroups == 0) | 
					
						
							|  |  |  |     maxgroups = getmaxgroups (); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   ngroups = 0; | 
					
						
							|  |  |  |   group_array = (GETGROUPS_T *)xrealloc (group_array, maxgroups * sizeof (GETGROUPS_T)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (HAVE_GETGROUPS)
 | 
					
						
							|  |  |  |   ngroups = getgroups (maxgroups, group_array); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* If getgroups returns nothing, or the OS does not support getgroups(),
 | 
					
						
							|  |  |  |      make sure the groups array includes at least the current gid. */ | 
					
						
							|  |  |  |   if (ngroups == 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       group_array[0] = current_user.gid; | 
					
						
							|  |  |  |       ngroups = 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* If the primary group is not in the groups array, add it as group_array[0]
 | 
					
						
							|  |  |  |      and shuffle everything else up 1, if there's room. */ | 
					
						
							|  |  |  |   for (i = 0; i < ngroups; i++) | 
					
						
							|  |  |  |     if (current_user.gid == (gid_t)group_array[i]) | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |   if (i == ngroups && ngroups < maxgroups) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       for (i = ngroups; i > 0; i--) | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | 	group_array[i] = group_array[i - 1]; | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  |       group_array[0] = current_user.gid; | 
					
						
							|  |  |  |       ngroups++; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* If the primary group is not group_array[0], swap group_array[0] and
 | 
					
						
							|  |  |  |      whatever the current group is.  The vast majority of systems should | 
					
						
							|  |  |  |      not need this; a notable exception is Linux. */ | 
					
						
							|  |  |  |   if (group_array[0] != current_user.gid) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       for (i = 0; i < ngroups; i++) | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | 	if (group_array[i] == current_user.gid) | 
					
						
							|  |  |  | 	  break; | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  |       if (i < ngroups) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  group_array[i] = group_array[0]; | 
					
						
							|  |  |  | 	  group_array[0] = current_user.gid; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Return non-zero if GID is one that we have in our groups list. */ | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  | #if defined (__STDC__) || defined ( _MINIX)
 | 
					
						
							|  |  |  | group_member (gid_t gid) | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | group_member (gid) | 
					
						
							|  |  |  |      gid_t gid; | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  | #endif /* !__STDC__ && !_MINIX */
 | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | { | 
					
						
							|  |  |  | #if defined (HAVE_GETGROUPS)
 | 
					
						
							|  |  |  |   register int i; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Short-circuit if possible, maybe saving a call to getgroups(). */ | 
					
						
							|  |  |  |   if (gid == current_user.gid || gid == current_user.egid) | 
					
						
							|  |  |  |     return (1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (HAVE_GETGROUPS)
 | 
					
						
							|  |  |  |   if (ngroups == 0) | 
					
						
							|  |  |  |     initialize_group_array (); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* In case of error, the user loses. */ | 
					
						
							|  |  |  |   if (ngroups <= 0) | 
					
						
							|  |  |  |     return (0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Search through the list looking for GID. */ | 
					
						
							|  |  |  |   for (i = 0; i < ngroups; i++) | 
					
						
							|  |  |  |     if (gid == (gid_t)group_array[i]) | 
					
						
							|  |  |  |       return (1); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | char ** | 
					
						
							|  |  |  | get_group_list (ngp) | 
					
						
							|  |  |  |      int *ngp; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   static char **group_vector = (char **)NULL; | 
					
						
							|  |  |  |   register int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (group_vector) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (ngp) | 
					
						
							|  |  |  | 	*ngp = ngroups; | 
					
						
							|  |  |  |       return group_vector; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (ngroups == 0) | 
					
						
							|  |  |  |     initialize_group_array (); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (ngroups <= 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (ngp) | 
					
						
							|  |  |  | 	*ngp = 0; | 
					
						
							|  |  |  |       return (char **)NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |   group_vector = strvec_create (ngroups); | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  |   for (i = 0; i < ngroups; i++) | 
					
						
							| 
									
										
										
										
											2002-07-17 14:10:11 +00:00
										 |  |  |     group_vector[i] = itos (group_array[i]); | 
					
						
							| 
									
										
										
										
											1999-02-19 17:11:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  |   if (ngp) | 
					
						
							|  |  |  |     *ngp = ngroups; | 
					
						
							|  |  |  |   return group_vector; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1999-02-19 17:11:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | int * | 
					
						
							|  |  |  | get_group_array (ngp) | 
					
						
							|  |  |  |      int *ngp; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   int i; | 
					
						
							|  |  |  |   static int *group_iarray = (int *)NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (group_iarray) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (ngp) | 
					
						
							|  |  |  | 	*ngp = ngroups; | 
					
						
							|  |  |  |       return (group_iarray); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (ngroups == 0) | 
					
						
							|  |  |  |     initialize_group_array ();     | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (ngroups <= 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (ngp) | 
					
						
							|  |  |  | 	*ngp = 0; | 
					
						
							|  |  |  |       return (int *)NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   group_iarray = (int *)xmalloc (ngroups * sizeof (int)); | 
					
						
							|  |  |  |   for (i = 0; i < ngroups; i++) | 
					
						
							|  |  |  |     group_iarray[i] = (int)group_array[i]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (ngp) | 
					
						
							|  |  |  |     *ngp = ngroups; | 
					
						
							|  |  |  |   return group_iarray; | 
					
						
							|  |  |  | } |