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