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