clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name misc.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/bin/ksh/obj -resource-dir /usr/local/lib/clang/13.0.0 -D EMACS -D VI -I . -I /usr/src/bin/ksh -I /usr/src/bin/ksh/../../lib/libc/gen -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/bin/ksh/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/bin/ksh/misc.c
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | #include <ctype.h> |
| 8 | #include <errno.h> |
| 9 | #include <fcntl.h> |
| 10 | #include <limits.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <unistd.h> |
| 14 | |
| 15 | #include "sh.h" |
| 16 | #include "charclass.h" |
| 17 | |
| 18 | short ctypes [UCHAR_MAX+1]; |
| 19 | static int dropped_privileges; |
| 20 | |
| 21 | static int do_gmatch(const unsigned char *, const unsigned char *, |
| 22 | const unsigned char *, const unsigned char *); |
| 23 | static const unsigned char *cclass(const unsigned char *, int); |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | void |
| 29 | setctypes(const char *s, int t) |
| 30 | { |
| 31 | int i; |
| 32 | |
| 33 | if (t & C_IFS) { |
| 34 | for (i = 0; i < UCHAR_MAX+1; i++) |
| 35 | ctypes[i] &= ~C_IFS; |
| 36 | ctypes[0] |= C_IFS; |
| 37 | } |
| 38 | while (*s != 0) |
| 39 | ctypes[(unsigned char) *s++] |= t; |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | initctypes(void) |
| 44 | { |
| 45 | int c; |
| 46 | |
| 47 | for (c = 'a'; c <= 'z'; c++) |
| 48 | ctypes[c] |= C_ALPHA; |
| 49 | for (c = 'A'; c <= 'Z'; c++) |
| 50 | ctypes[c] |= C_ALPHA; |
| 51 | ctypes['_'] |= C_ALPHA; |
| 52 | setctypes(" \t\n|&;<>()", C_LEX1); |
| 53 | setctypes("*@#!$-?", C_VAR1); |
| 54 | setctypes(" \t\n", C_IFSWS); |
| 55 | setctypes("=-+?", C_SUBOP1); |
| 56 | setctypes("#%", C_SUBOP2); |
| 57 | setctypes(" \n\t\"#$&'()*;<>?[\\`|", C_QUOTE); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | |
| 62 | char * |
| 63 | u64ton(uint64_t n, int base) |
| 64 | { |
| 65 | char *p; |
| 66 | static char buf [20]; |
| 67 | |
| 68 | p = &buf[sizeof(buf)]; |
| 69 | *--p = '\0'; |
| 70 | do { |
| 71 | *--p = "0123456789ABCDEF"[n%base]; |
| 72 | n /= base; |
| 73 | } while (n != 0); |
| 74 | return p; |
| 75 | } |
| 76 | |
| 77 | char * |
| 78 | str_save(const char *s, Area *ap) |
| 79 | { |
| 80 | size_t len; |
| 81 | char *p; |
| 82 | |
| 83 | if (!s) |
| 84 | return NULL; |
| 85 | len = strlen(s)+1; |
| 86 | p = alloc(len, ap); |
| 87 | strlcpy(p, s, len); |
| 88 | return (p); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | char * |
| 96 | str_nsave(const char *s, int n, Area *ap) |
| 97 | { |
| 98 | char *ns; |
| 99 | |
| 100 | if (n < 0) |
| 101 | return 0; |
| 102 | ns = alloc(n + 1, ap); |
| 103 | ns[0] = '\0'; |
| 104 | return strncat(ns, s, n); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | char * |
| 109 | Xcheck_grow_(XString *xsp, char *xp, size_t more) |
| 110 | { |
| 111 | char *old_beg = xsp->beg; |
| 112 | |
| 113 | xsp->len += more > xsp->len ? more : xsp->len; |
| 114 | xsp->beg = aresize(xsp->beg, xsp->len + 8, xsp->areap); |
| 115 | xsp->end = xsp->beg + xsp->len; |
| 116 | return xsp->beg + (xp - old_beg); |
| 117 | } |
| 118 | |
| 119 | const struct option sh_options[] = { |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | { "allexport", 'a', OF_ANY }, |
| 125 | { "braceexpand", 0, OF_ANY }, |
| 126 | { "bgnice", 0, OF_ANY }, |
| 127 | { NULL, 'c', OF_CMDLINE }, |
| 128 | { "csh-history", 0, OF_ANY }, |
| 129 | #ifdef EMACS |
| 130 | { "emacs", 0, OF_ANY }, |
| 131 | #endif |
| 132 | { "errexit", 'e', OF_ANY }, |
| 133 | #ifdef EMACS |
| 134 | { "gmacs", 0, OF_ANY }, |
| 135 | #endif |
| 136 | { "ignoreeof", 0, OF_ANY }, |
| 137 | { "interactive",'i', OF_CMDLINE }, |
| 138 | { "keyword", 'k', OF_ANY }, |
| 139 | { "login", 'l', OF_CMDLINE }, |
| 140 | { "markdirs", 'X', OF_ANY }, |
| 141 | { "monitor", 'm', OF_ANY }, |
| 142 | { "noclobber", 'C', OF_ANY }, |
| 143 | { "noexec", 'n', OF_ANY }, |
| 144 | { "noglob", 'f', OF_ANY }, |
| 145 | { "nohup", 0, OF_ANY }, |
| 146 | { "nolog", 0, OF_ANY }, |
| 147 | { "notify", 'b', OF_ANY }, |
| 148 | { "nounset", 'u', OF_ANY }, |
| 149 | { "physical", 0, OF_ANY }, |
| 150 | { "pipefail", 0, OF_ANY }, |
| 151 | { "posix", 0, OF_ANY }, |
| 152 | { "privileged", 'p', OF_ANY }, |
| 153 | { "restricted", 'r', OF_CMDLINE }, |
| 154 | { "sh", 0, OF_ANY }, |
| 155 | { "stdin", 's', OF_CMDLINE }, |
| 156 | { "trackall", 'h', OF_ANY }, |
| 157 | { "verbose", 'v', OF_ANY }, |
| 158 | #ifdef VI |
| 159 | { "vi", 0, OF_ANY }, |
| 160 | { "viraw", 0, OF_ANY }, |
| 161 | { "vi-show8", 0, OF_ANY }, |
| 162 | { "vi-tabcomplete", 0, OF_ANY }, |
| 163 | { "vi-esccomplete", 0, OF_ANY }, |
| 164 | #endif |
| 165 | { "xtrace", 'x', OF_ANY }, |
| 166 | |
| 167 | |
| 168 | |
| 169 | { NULL, 0, OF_INTERNAL }, |
| 170 | }; |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | int |
| 176 | option(const char *n) |
| 177 | { |
| 178 | unsigned int ele; |
| 179 | |
| 180 | for (ele = 0; ele < NELEM(sh_options); ele++) |
| 181 | if (sh_options[ele].name && strcmp(sh_options[ele].name, n) == 0) |
| 182 | return ele; |
| 183 | |
| 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | struct options_info { |
| 188 | int opt_width; |
| 189 | struct { |
| 190 | const char *name; |
| 191 | int flag; |
| 192 | } opts[NELEM(sh_options)]; |
| 193 | }; |
| 194 | |
| 195 | static char *options_fmt_entry(void *arg, int i, char *buf, int buflen); |
| 196 | static void printoptions(int verbose); |
| 197 | |
| 198 | |
| 199 | static char * |
| 200 | options_fmt_entry(void *arg, int i, char *buf, int buflen) |
| 201 | { |
| 202 | struct options_info *oi = (struct options_info *) arg; |
| 203 | |
| 204 | shf_snprintf(buf, buflen, "%-*s %s", |
| 205 | oi->opt_width, oi->opts[i].name, |
| 206 | Flag(oi->opts[i].flag) ? "on" : "off"); |
| 207 | return buf; |
| 208 | } |
| 209 | |
| 210 | static void |
| 211 | printoptions(int verbose) |
| 212 | { |
| 213 | unsigned int ele; |
| 214 | |
| 215 | if (verbose) { |
| 216 | struct options_info oi; |
| 217 | unsigned int n; |
| 218 | int len; |
| 219 | |
| 220 | |
| 221 | shprintf("Current option settings\n"); |
| 222 | |
| 223 | for (ele = n = oi.opt_width = 0; ele < NELEM(sh_options); ele++) { |
| 224 | if (sh_options[ele].name) { |
| 225 | len = strlen(sh_options[ele].name); |
| 226 | oi.opts[n].name = sh_options[ele].name; |
| 227 | oi.opts[n++].flag = ele; |
| 228 | if (len > oi.opt_width) |
| 229 | oi.opt_width = len; |
| 230 | } |
| 231 | } |
| 232 | print_columns(shl_stdout, n, options_fmt_entry, &oi, |
| 233 | oi.opt_width + 5, 1); |
| 234 | } else { |
| 235 | |
| 236 | shprintf("set"); |
| 237 | for (ele = 0; ele < NELEM(sh_options); ele++) { |
| 238 | if (sh_options[ele].name) |
| 239 | shprintf(" %co %s", |
| 240 | Flag(ele) ? '-' : '+', |
| 241 | sh_options[ele].name); |
| 242 | } |
| 243 | shprintf("\n"); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | char * |
| 248 | getoptions(void) |
| 249 | { |
| 250 | unsigned int ele; |
| 251 | char m[(int) FNFLAGS + 1]; |
| 252 | char *cp = m; |
| 253 | |
| 254 | for (ele = 0; ele < NELEM(sh_options); ele++) |
| 255 | if (sh_options[ele].c && Flag(ele)) |
| 256 | *cp++ = sh_options[ele].c; |
| 257 | *cp = 0; |
| 258 | return str_save(m, ATEMP); |
| 259 | } |
| 260 | |
| 261 | |
| 262 | void |
| 263 | change_flag(enum sh_flag f, |
| 264 | int what, |
| 265 | int newval) |
| 266 | { |
| 267 | int oldval; |
| 268 | |
| 269 | oldval = Flag(f); |
| 270 | Flag(f) = newval; |
| 271 | if (f == FMONITOR) { |
| 272 | if (what != OF_CMDLINE && newval != oldval) |
| 273 | j_change(); |
| 274 | } else |
| 275 | if (0 |
| 276 | #ifdef VI |
| 277 | || f == FVI |
| 278 | #endif /* VI */ |
| 279 | #ifdef EMACS |
| 280 | || f == FEMACS || f == FGMACS |
| 281 | #endif /* EMACS */ |
| 282 | ) |
| 283 | { |
| 284 | if (newval) { |
| 285 | #ifdef VI |
| 286 | Flag(FVI) = 0; |
| 287 | #endif /* VI */ |
| 288 | #ifdef EMACS |
| 289 | Flag(FEMACS) = Flag(FGMACS) = 0; |
| 290 | #endif /* EMACS */ |
| 291 | Flag(f) = newval; |
| 292 | } |
| 293 | } else |
| 294 | |
| 295 | if (f == FPRIVILEGED && oldval && !newval && issetugid() && |
| 296 | !dropped_privileges) { |
| 297 | gid_t gid = getgid(); |
| 298 | |
| 299 | setresgid(gid, gid, gid); |
| 300 | setgroups(1, &gid); |
| 301 | setresuid(ksheuid, ksheuid, ksheuid); |
| 302 | |
| 303 | if (pledge("stdio rpath wpath cpath fattr flock getpw proc " |
| 304 | "exec tty", NULL) == -1) |
| 305 | bi_errorf("pledge fail"); |
| 306 | dropped_privileges = 1; |
| 307 | } else if (f == FPOSIX && newval) { |
| 308 | Flag(FBRACEEXPAND) = 0; |
| 309 | } |
| 310 | |
| 311 | if (f == FTALKING) { |
| 312 | if ((what == OF_CMDLINE || what == OF_SET) && procpid == kshpid) |
| 313 | Flag(FTALKING_I) = newval; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | int |
| 321 | parse_args(char **argv, |
| 322 | int what, |
| 323 | int *setargsp) |
| 324 | { |
| 325 | static char cmd_opts[NELEM(sh_options) + 3]; |
| 326 | static char set_opts[NELEM(sh_options) + 5]; |
| 327 | char *opts; |
| 328 | char *array = NULL; |
| 329 | Getopt go; |
| 330 | int i, optc, sortargs = 0, arrayset = 0; |
| 331 | unsigned int ele; |
| 332 | |
| 333 | |
| 334 | if (cmd_opts[0] == '\0') { |
| 1 | Assuming the condition is false | |
|
| |
| 335 | char *p, *q; |
| 336 | |
| 337 | |
| 338 | strlcpy(cmd_opts, "o:", sizeof cmd_opts); |
| 339 | p = cmd_opts + strlen(cmd_opts); |
| 340 | |
| 341 | strlcpy(set_opts, "A:o;s", sizeof set_opts); |
| 342 | q = set_opts + strlen(set_opts); |
| 343 | for (ele = 0; ele < NELEM(sh_options); ele++) { |
| 344 | if (sh_options[ele].c) { |
| 345 | if (sh_options[ele].flags & OF_CMDLINE) |
| 346 | *p++ = sh_options[ele].c; |
| 347 | if (sh_options[ele].flags & OF_SET) |
| 348 | *q++ = sh_options[ele].c; |
| 349 | } |
| 350 | } |
| 351 | *p = '\0'; |
| 352 | *q = '\0'; |
| 353 | } |
| 354 | |
| 355 | if (what == OF_CMDLINE) { |
| 3 | | Assuming 'what' is not equal to OF_CMDLINE | |
|
| |
| 356 | char *p; |
| 357 | |
| 358 | |
| 359 | |
| 360 | Flag(FLOGIN) = (argv[0][0] == '-' || |
| 361 | ((p = strrchr(argv[0], '/')) && *++p == '-')); |
| 362 | opts = cmd_opts; |
| 363 | } else |
| 364 | opts = set_opts; |
| 365 | ksh_getopt_reset(&go, GF_ERROR|GF_PLUSOPT); |
| 5 | | Calling 'ksh_getopt_reset' | |
|
| 7 | | Returning from 'ksh_getopt_reset' | |
|
| 366 | while ((optc = ksh_getopt(argv, &go, opts)) != -1) { |
| |
| 32 | | Returning from 'ksh_getopt' | |
|
| 33 | | Assuming the condition is true | |
|
| 34 | | Loop condition is true. Entering loop body | |
|
| 40 | | Loop condition is false. Execution continues on line 423 | |
|
| 367 | int set = (go.info & GI_PLUS) ? 0 : 1; |
| |
| 368 | switch (optc) { |
| 36 | | Control jumps to 'case 65:' at line 369 | |
|
| 369 | case 'A': |
| 370 | arrayset = set ? 1 : -1; |
| |
| 371 | array = go.optarg; |
| 38 | | Null pointer value stored to 'array' | |
|
| 372 | break; |
| 39 | | Execution continues on line 366 | |
|
| 373 | |
| 374 | case 'o': |
| 375 | if (go.optarg == NULL) { |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | printoptions(set); |
| 383 | break; |
| 384 | } |
| 385 | i = option(go.optarg); |
| 386 | if (i != -1 && set == Flag(i)) |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | ; |
| 393 | else if (i != -1 && (sh_options[i].flags & what)) |
| 394 | change_flag((enum sh_flag) i, what, set); |
| 395 | else { |
| 396 | bi_errorf("%s: bad option", go.optarg); |
| 397 | return -1; |
| 398 | } |
| 399 | break; |
| 400 | |
| 401 | case '?': |
| 402 | return -1; |
| 403 | |
| 404 | default: |
| 405 | |
| 406 | if (what == OF_SET && optc == 's') { |
| 407 | sortargs = 1; |
| 408 | break; |
| 409 | } |
| 410 | for (ele = 0; ele < NELEM(sh_options); ele++) |
| 411 | if (optc == sh_options[ele].c && |
| 412 | (what & sh_options[ele].flags)) { |
| 413 | change_flag((enum sh_flag) ele, what, |
| 414 | set); |
| 415 | break; |
| 416 | } |
| 417 | if (ele == NELEM(sh_options)) { |
| 418 | internal_errorf("%s: `%c'", __func__, optc); |
| 419 | return -1; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | if (!(go.info & GI_MINUSMINUS) && argv[go.optind] && |
| 424 | (argv[go.optind][0] == '-' || argv[go.optind][0] == '+') && |
| 425 | argv[go.optind][1] == '\0') { |
| 426 | |
| 427 | if (argv[go.optind][0] == '-' && !Flag(FPOSIX)) |
| 428 | Flag(FVERBOSE) = Flag(FXTRACE) = 0; |
| 429 | |
| 430 | go.optind++; |
| 431 | } |
| 432 | if (setargsp) |
| 41 | | Assuming 'setargsp' is null | |
|
| |
| 433 | |
| 434 | *setargsp = !arrayset && ((go.info & GI_MINUSMINUS) || |
| 435 | argv[go.optind]); |
| 436 | |
| 437 | if (arrayset && (!*array || *skip_varname(array, false))) { |
| 43 | | Dereference of null pointer (loaded from variable 'array') |
|
| 438 | bi_errorf("%s: is not an identifier", array); |
| 439 | return -1; |
| 440 | } |
| 441 | if (sortargs) { |
| 442 | for (i = go.optind; argv[i]; i++) |
| 443 | ; |
| 444 | qsortp((void **) &argv[go.optind], (size_t) (i - go.optind), |
| 445 | xstrcmp); |
| 446 | } |
| 447 | if (arrayset) { |
| 448 | set_array(array, arrayset, argv + go.optind); |
| 449 | for (; argv[go.optind]; go.optind++) |
| 450 | ; |
| 451 | } |
| 452 | |
| 453 | return go.optind; |
| 454 | } |
| 455 | |
| 456 | |
| 457 | int |
| 458 | getn(const char *as, int *ai) |
| 459 | { |
| 460 | char *p; |
| 461 | long n; |
| 462 | |
| 463 | n = strtol(as, &p, 10); |
| 464 | |
| 465 | if (!*as || *p || INT_MIN >= n || n >= INT_MAX) |
| 466 | return 0; |
| 467 | |
| 468 | *ai = (int)n; |
| 469 | return 1; |
| 470 | } |
| 471 | |
| 472 | |
| 473 | int |
| 474 | bi_getn(const char *as, int *ai) |
| 475 | { |
| 476 | int rv = getn(as, ai); |
| 477 | |
| 478 | if (!rv) |
| 479 | bi_errorf("%s: bad number", as); |
| 480 | return rv; |
| 481 | } |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | int |
| 494 | gmatch(const char *s, const char *p, int isfile) |
| 495 | { |
| 496 | const char *se, *pe; |
| 497 | |
| 498 | if (s == NULL || p == NULL) |
| 499 | return 0; |
| 500 | se = s + strlen(s); |
| 501 | pe = p + strlen(p); |
| 502 | |
| 503 | |
| 504 | |
| 505 | if (!isfile && !has_globbing(p, pe)) { |
| 506 | size_t len = pe - p + 1; |
| 507 | char tbuf[64]; |
| 508 | char *t = len <= sizeof(tbuf) ? tbuf : |
| 509 | alloc(len, ATEMP); |
| 510 | debunk(t, p, len); |
| 511 | return !strcmp(t, s); |
| 512 | } |
| 513 | return do_gmatch((const unsigned char *) s, (const unsigned char *) se, |
| 514 | (const unsigned char *) p, (const unsigned char *) pe); |
| 515 | } |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | int |
| 534 | has_globbing(const char *xp, const char *xpe) |
| 535 | { |
| 536 | const unsigned char *p = (const unsigned char *) xp; |
| 537 | const unsigned char *pe = (const unsigned char *) xpe; |
| 538 | int c; |
| 539 | int nest = 0, bnest = 0; |
| 540 | int saw_glob = 0; |
| 541 | int in_bracket = 0; |
| 542 | |
| 543 | for (; p < pe; p++) { |
| 544 | if (!ISMAGIC(*p)) |
| 545 | continue; |
| 546 | if ((c = *++p) == '*' || c == '?') |
| 547 | saw_glob = 1; |
| 548 | else if (c == '[') { |
| 549 | if (!in_bracket) { |
| 550 | saw_glob = 1; |
| 551 | in_bracket = 1; |
| 552 | if (ISMAGIC(p[1]) && p[2] == '!') |
| 553 | p += 2; |
| 554 | if (ISMAGIC(p[1]) && p[2] == ']') |
| 555 | p += 2; |
| 556 | } |
| 557 | |
| 558 | } else if (c == ']') { |
| 559 | if (in_bracket) { |
| 560 | if (bnest) |
| 561 | return 0; |
| 562 | in_bracket = 0; |
| 563 | } |
| 564 | } else if ((c & 0x80) && strchr("*+?@! ", c & 0x7f)) { |
| 565 | saw_glob = 1; |
| 566 | if (in_bracket) |
| 567 | bnest++; |
| 568 | else |
| 569 | nest++; |
| 570 | } else if (c == '|') { |
| 571 | if (in_bracket && !bnest) |
| 572 | return 0; |
| 573 | } else if (c == ')') { |
| 574 | if (in_bracket) { |
| 575 | if (!bnest--) |
| 576 | return 0; |
| 577 | } else if (nest) |
| 578 | nest--; |
| 579 | } |
| 580 | |
| 581 | |
| 582 | } |
| 583 | return saw_glob && !in_bracket && !nest; |
| 584 | } |
| 585 | |
| 586 | |
| 587 | static int |
| 588 | do_gmatch(const unsigned char *s, const unsigned char *se, |
| 589 | const unsigned char *p, const unsigned char *pe) |
| 590 | { |
| 591 | int sc, pc; |
| 592 | const unsigned char *prest, *psub, *pnext; |
| 593 | const unsigned char *srest; |
| 594 | |
| 595 | if (s == NULL || p == NULL) |
| 596 | return 0; |
| 597 | while (p < pe) { |
| 598 | pc = *p++; |
| 599 | sc = s < se ? *s : '\0'; |
| 600 | s++; |
| 601 | if (!ISMAGIC(pc)) { |
| 602 | if (sc != pc) |
| 603 | return 0; |
| 604 | continue; |
| 605 | } |
| 606 | switch (*p++) { |
| 607 | case '[': |
| 608 | if (sc == 0 || (p = cclass(p, sc)) == NULL) |
| 609 | return 0; |
| 610 | break; |
| 611 | |
| 612 | case '?': |
| 613 | if (sc == 0) |
| 614 | return 0; |
| 615 | break; |
| 616 | |
| 617 | case '*': |
| 618 | |
| 619 | while (ISMAGIC(p[0]) && p[1] == '*') |
| 620 | p += 2; |
| 621 | if (p == pe) |
| 622 | return 1; |
| 623 | s--; |
| 624 | do { |
| 625 | if (do_gmatch(s, se, p, pe)) |
| 626 | return 1; |
| 627 | } while (s++ < se); |
| 628 | return 0; |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | case 0x80|'+': |
| 636 | case 0x80|'*': |
| 637 | if (!(prest = pat_scan(p, pe, 0))) |
| 638 | return 0; |
| 639 | s--; |
| 640 | |
| 641 | if (p[-1] == (0x80 | '*') && |
| 642 | do_gmatch(s, se, prest, pe)) |
| 643 | return 1; |
| 644 | for (psub = p; ; psub = pnext) { |
| 645 | pnext = pat_scan(psub, pe, 1); |
| 646 | for (srest = s; srest <= se; srest++) { |
| 647 | if (do_gmatch(s, srest, psub, pnext - 2) && |
| 648 | (do_gmatch(srest, se, prest, pe) || |
| 649 | (s != srest && do_gmatch(srest, |
| 650 | se, p - 2, pe)))) |
| 651 | return 1; |
| 652 | } |
| 653 | if (pnext == prest) |
| 654 | break; |
| 655 | } |
| 656 | return 0; |
| 657 | |
| 658 | case 0x80|'?': |
| 659 | case 0x80|'@': |
| 660 | case 0x80|' ': |
| 661 | if (!(prest = pat_scan(p, pe, 0))) |
| 662 | return 0; |
| 663 | s--; |
| 664 | |
| 665 | if (p[-1] == (0x80 | '?') && |
| 666 | do_gmatch(s, se, prest, pe)) |
| 667 | return 1; |
| 668 | for (psub = p; ; psub = pnext) { |
| 669 | pnext = pat_scan(psub, pe, 1); |
| 670 | srest = prest == pe ? se : s; |
| 671 | for (; srest <= se; srest++) { |
| 672 | if (do_gmatch(s, srest, psub, pnext - 2) && |
| 673 | do_gmatch(srest, se, prest, pe)) |
| 674 | return 1; |
| 675 | } |
| 676 | if (pnext == prest) |
| 677 | break; |
| 678 | } |
| 679 | return 0; |
| 680 | |
| 681 | case 0x80|'!': |
| 682 | if (!(prest = pat_scan(p, pe, 0))) |
| 683 | return 0; |
| 684 | s--; |
| 685 | for (srest = s; srest <= se; srest++) { |
| 686 | int matched = 0; |
| 687 | |
| 688 | for (psub = p; ; psub = pnext) { |
| 689 | pnext = pat_scan(psub, pe, 1); |
| 690 | if (do_gmatch(s, srest, psub, |
| 691 | pnext - 2)) { |
| 692 | matched = 1; |
| 693 | break; |
| 694 | } |
| 695 | if (pnext == prest) |
| 696 | break; |
| 697 | } |
| 698 | if (!matched && |
| 699 | do_gmatch(srest, se, prest, pe)) |
| 700 | return 1; |
| 701 | } |
| 702 | return 0; |
| 703 | |
| 704 | default: |
| 705 | if (sc != p[-1]) |
| 706 | return 0; |
| 707 | break; |
| 708 | } |
| 709 | } |
| 710 | return s == se; |
| 711 | } |
| 712 | |
| 713 | static int |
| 714 | posix_cclass(const unsigned char *pattern, int test, const unsigned char **ep) |
| 715 | { |
| 716 | const struct cclass *cc; |
| 717 | const unsigned char *colon; |
| 718 | size_t len; |
| 719 | int rval = 0; |
| 720 | |
| 721 | if ((colon = strchr(pattern, ':')) == NULL || colon[1] != MAGIC) { |
| 722 | *ep = pattern - 2; |
| 723 | return -1; |
| 724 | } |
| 725 | *ep = colon + 3; |
| 726 | len = (size_t)(colon - pattern); |
| 727 | |
| 728 | for (cc = cclasses; cc->name != NULL; cc++) { |
| 729 | if (!strncmp(pattern, cc->name, len) && cc->name[len] == '\0') { |
| 730 | if (cc->isctype(test)) |
| 731 | rval = 1; |
| 732 | break; |
| 733 | } |
| 734 | } |
| 735 | if (cc->name == NULL) { |
| 736 | rval = -2; |
| 737 | } |
| 738 | return rval; |
| 739 | } |
| 740 | |
| 741 | static const unsigned char * |
| 742 | cclass(const unsigned char *p, int sub) |
| 743 | { |
| 744 | int c, d, rv, not, found = 0; |
| 745 | const unsigned char *orig_p = p; |
| 746 | |
| 747 | if ((not = (ISMAGIC(*p) && *++p == '!'))) |
| 748 | p++; |
| 749 | do { |
| 750 | |
| 751 | if ((p[0] == MAGIC && p[1] == '[' && p[2] == ':') || |
| 752 | (p[0] == '[' && p[1] == ':')) { |
| 753 | do { |
| 754 | const char *pp = p + (*p == MAGIC) + 2; |
| 755 | rv = posix_cclass(pp, sub, &p); |
| 756 | switch (rv) { |
| 757 | case 1: |
| 758 | found = 1; |
| 759 | break; |
| 760 | case -2: |
| 761 | return NULL; |
| 762 | } |
| 763 | } while (rv != -1 && p[0] == MAGIC && p[1] == '[' && p[2] == ':'); |
| 764 | if (p[0] == MAGIC && p[1] == ']') |
| 765 | break; |
| 766 | } |
| 767 | |
| 768 | c = *p++; |
| 769 | if (ISMAGIC(c)) { |
| 770 | c = *p++; |
| 771 | if ((c & 0x80) && !ISMAGIC(c)) { |
| 772 | c &= 0x7f; |
| 773 | |
| 774 | if (c == ' ') |
| 775 | c = '(' ; |
| 776 | } |
| 777 | } |
| 778 | if (c == '\0') |
| 779 | |
| 780 | return sub == '[' ? orig_p : NULL; |
| 781 | if (ISMAGIC(p[0]) && p[1] == '-' && |
| 782 | (!ISMAGIC(p[2]) || p[3] != ']')) { |
| 783 | p += 2; |
| 784 | d = *p++; |
| 785 | if (ISMAGIC(d)) { |
| 786 | d = *p++; |
| 787 | if ((d & 0x80) && !ISMAGIC(d)) |
| 788 | d &= 0x7f; |
| 789 | } |
| 790 | |
| 791 | if (c > d) |
| 792 | return NULL; |
| 793 | } else |
| 794 | d = c; |
| 795 | if (c == sub || (c <= sub && sub <= d)) |
| 796 | found = 1; |
| 797 | } while (!(ISMAGIC(p[0]) && p[1] == ']')); |
| 798 | |
| 799 | return (found != not) ? p+2 : NULL; |
| 800 | } |
| 801 | |
| 802 | |
| 803 | const unsigned char * |
| 804 | pat_scan(const unsigned char *p, const unsigned char *pe, int match_sep) |
| 805 | { |
| 806 | int nest = 0; |
| 807 | |
| 808 | for (; p < pe; p++) { |
| 809 | if (!ISMAGIC(*p)) |
| 810 | continue; |
| 811 | if ((*++p == ')' && nest-- == 0) || |
| 812 | (*p == '|' && match_sep && nest == 0)) |
| 813 | return ++p; |
| 814 | if ((*p & 0x80) && strchr("*+?@! ", *p & 0x7f)) |
| 815 | nest++; |
| 816 | } |
| 817 | return NULL; |
| 818 | } |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | void |
| 824 | qsortp(void **base, |
| 825 | size_t n, |
| 826 | int (*f) (const void *, const void *)) |
| 827 | { |
| 828 | qsort(base, n, sizeof(char *), f); |
| 829 | } |
| 830 | |
| 831 | int |
| 832 | xstrcmp(const void *p1, const void *p2) |
| 833 | { |
| 834 | return (strcmp(*(char **)p1, *(char **)p2)); |
| 835 | } |
| 836 | |
| 837 | |
| 838 | void |
| 839 | ksh_getopt_reset(Getopt *go, int flags) |
| 840 | { |
| 841 | go->optind = 1; |
| 842 | go->optarg = NULL; |
| 6 | | Null pointer value stored to 'go.optarg' | |
|
| 843 | go->p = 0; |
| 844 | go->flags = flags; |
| 845 | go->info = 0; |
| 846 | go->buf[1] = '\0'; |
| 847 | } |
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 | |
| 861 | |
| 862 | |
| 863 | |
| 864 | |
| 865 | |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | int |
| 875 | ksh_getopt(char **argv, Getopt *go, const char *options) |
| 876 | { |
| 877 | char c; |
| 878 | char *o; |
| 879 | |
| 880 | if (go->p == 0 || (c = argv[go->optind - 1][go->p]) == '\0') { |
| 881 | char *arg = argv[go->optind], flag = arg ? *arg : '\0'; |
| 9 | | Assuming 'arg' is non-null | |
|
| |
| 882 | |
| 883 | go->p = 1; |
| 884 | if (flag == '-' && arg[1] == '-' && arg[2] == '\0') { |
| 11 | | Assuming the condition is false | |
|
| 885 | go->optind++; |
| 886 | go->p = 0; |
| 887 | go->info |= GI_MINUSMINUS; |
| 888 | return -1; |
| 889 | } |
| 890 | if (arg == NULL || |
| |
| 891 | ((flag != '-' ) && |
| 892 | (!(go->flags & GF_PLUSOPT) || flag != '+')) || |
| 12 | | Assuming the condition is false | |
|
| 893 | (c = arg[1]) == '\0') { |
| 13 | | Assuming the condition is false | |
|
| 894 | go->p = 0; |
| 895 | return -1; |
| 896 | } |
| 897 | go->optind++; |
| 898 | go->info &= ~(GI_MINUS|GI_PLUS); |
| 899 | go->info |= flag == '-' ? GI_MINUS : GI_PLUS; |
| |
| 900 | } |
| 901 | go->p++; |
| 902 | if (c == '?' || c == ':' || c == ';' || c == ',' || c == '#' || |
| 16 | | Assuming the condition is false | |
|
| 17 | | Assuming the condition is false | |
|
| 18 | | Assuming the condition is false | |
|
| 19 | | Assuming the condition is false | |
|
| 20 | | Assuming the condition is false | |
|
| |
| 903 | !(o = strchr(options, c))) { |
| 21 | | Assuming 'o' is non-null | |
|
| 904 | if (options[0] == ':') { |
| 905 | go->buf[0] = c; |
| 906 | go->optarg = go->buf; |
| 907 | } else { |
| 908 | warningf(false, "%s%s-%c: unknown option", |
| 909 | (go->flags & GF_NONAME) ? "" : argv[0], |
| 910 | (go->flags & GF_NONAME) ? "" : ": ", c); |
| 911 | if (go->flags & GF_ERROR) |
| 912 | bi_errorf(NULL); |
| 913 | } |
| 914 | return '?'; |
| 915 | } |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | if (*++o == ':' || *o == ';') { |
| 23 | | Assuming the condition is false | |
|
| 24 | | Assuming the condition is false | |
|
| |
| 922 | if (argv[go->optind - 1][go->p]) |
| 923 | go->optarg = argv[go->optind - 1] + go->p; |
| 924 | else if (argv[go->optind]) |
| 925 | go->optarg = argv[go->optind++]; |
| 926 | else if (*o == ';') |
| 927 | go->optarg = NULL; |
| 928 | else { |
| 929 | if (options[0] == ':') { |
| 930 | go->buf[0] = c; |
| 931 | go->optarg = go->buf; |
| 932 | return ':'; |
| 933 | } |
| 934 | warningf(false, "%s%s-`%c' requires argument", |
| 935 | (go->flags & GF_NONAME) ? "" : argv[0], |
| 936 | (go->flags & GF_NONAME) ? "" : ": ", c); |
| 937 | if (go->flags & GF_ERROR) |
| 938 | bi_errorf(NULL); |
| 939 | return '?'; |
| 940 | } |
| 941 | go->p = 0; |
| 942 | } else if (*o == ',') { |
| 26 | | Assuming the condition is false | |
|
| |
| 943 | |
| 944 | go->optarg = argv[go->optind - 1] + go->p; |
| 945 | go->p = 0; |
| 946 | } else if (*o == '#') { |
| 28 | | Assuming the condition is false | |
|
| |
| 947 | |
| 948 | |
| 949 | |
| 950 | |
| 951 | if (argv[go->optind - 1][go->p]) { |
| 952 | if (digit(argv[go->optind - 1][go->p]) || |
| 953 | !strcmp(&argv[go->optind - 1][go->p], "unlimited")) { |
| 954 | go->optarg = argv[go->optind - 1] + go->p; |
| 955 | go->p = 0; |
| 956 | } else |
| 957 | go->optarg = NULL; |
| 958 | } else { |
| 959 | if (argv[go->optind] && (digit(argv[go->optind][0]) || |
| 960 | !strcmp(argv[go->optind], "unlimited"))) { |
| 961 | go->optarg = argv[go->optind++]; |
| 962 | go->p = 0; |
| 963 | } else |
| 964 | go->optarg = NULL; |
| 965 | } |
| 966 | } |
| 967 | return c; |
| 30 | | Returning without writing to 'go->optarg' | |
|
| 31 | | Returning value (loaded from 'c'), which participates in a condition later | |
|
| 968 | } |
| 969 | |
| 970 | |
| 971 | |
| 972 | |
| 973 | |
| 974 | void |
| 975 | print_value_quoted(const char *s) |
| 976 | { |
| 977 | const char *p; |
| 978 | int inquote = 0; |
| 979 | |
| 980 | |
| 981 | for (p = s; *p; p++) |
| 982 | if (ctype(*p, C_QUOTE)) |
| 983 | break; |
| 984 | if (!*p) { |
| 985 | shprintf("%s", s); |
| 986 | return; |
| 987 | } |
| 988 | for (p = s; *p; p++) { |
| 989 | if (*p == '\'') { |
| 990 | shprintf(inquote ? "'\\'" : "\\'"); |
| 991 | inquote = 0; |
| 992 | } else { |
| 993 | if (!inquote) { |
| 994 | shprintf("'"); |
| 995 | inquote = 1; |
| 996 | } |
| 997 | shf_putc(*p, shl_stdout); |
| 998 | } |
| 999 | } |
| 1000 | if (inquote) |
| 1001 | shprintf("'"); |
| 1002 | } |
| 1003 | |
| 1004 | |
| 1005 | |
| 1006 | |
| 1007 | void |
| 1008 | print_columns(struct shf *shf, int n, char *(*func) (void *, int, char *, int), |
| 1009 | void *arg, int max_width, int prefcol) |
| 1010 | { |
| 1011 | char *str = alloc(max_width + 1, ATEMP); |
| 1012 | int i; |
| 1013 | int r, c; |
| 1014 | int rows, cols; |
| 1015 | int nspace; |
| 1016 | int col_width; |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | |
| 1022 | cols = x_cols / (max_width + 1); |
| 1023 | if (!cols) |
| 1024 | cols = 1; |
| 1025 | rows = (n + cols - 1) / cols; |
| 1026 | if (prefcol && n && cols > rows) { |
| 1027 | int tmp = rows; |
| 1028 | |
| 1029 | rows = cols; |
| 1030 | cols = tmp; |
| 1031 | if (rows > n) |
| 1032 | rows = n; |
| 1033 | } |
| 1034 | |
| 1035 | col_width = max_width; |
| 1036 | if (cols == 1) |
| 1037 | col_width = 0; |
| 1038 | nspace = (x_cols - max_width * cols) / cols; |
| 1039 | if (nspace <= 0) |
| 1040 | nspace = 1; |
| 1041 | for (r = 0; r < rows; r++) { |
| 1042 | for (c = 0; c < cols; c++) { |
| 1043 | i = c * rows + r; |
| 1044 | if (i < n) { |
| 1045 | shf_fprintf(shf, "%-*s", |
| 1046 | col_width, |
| 1047 | (*func)(arg, i, str, max_width + 1)); |
| 1048 | if (c + 1 < cols) |
| 1049 | shf_fprintf(shf, "%*s", nspace, ""); |
| 1050 | } |
| 1051 | } |
| 1052 | shf_putchar('\n', shf); |
| 1053 | } |
| 1054 | afree(str, ATEMP); |
| 1055 | } |
| 1056 | |
| 1057 | |
| 1058 | int |
| 1059 | strip_nuls(char *buf, int nbytes) |
| 1060 | { |
| 1061 | char *dst; |
| 1062 | |
| 1063 | if ((dst = memchr(buf, '\0', nbytes))) { |
| 1064 | char *end = buf + nbytes; |
| 1065 | char *p, *q; |
| 1066 | |
| 1067 | for (p = dst; p < end; p = q) { |
| 1068 | |
| 1069 | while (++p < end && *p == '\0') |
| 1070 | ; |
| 1071 | |
| 1072 | if (!(q = memchr(p, '\0', end - p))) |
| 1073 | q = end; |
| 1074 | memmove(dst, p, q - p); |
| 1075 | dst += q - p; |
| 1076 | } |
| 1077 | *dst = '\0'; |
| 1078 | return dst - buf; |
| 1079 | } |
| 1080 | return nbytes; |
| 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | |
| 1085 | |
| 1086 | int |
| 1087 | blocking_read(int fd, char *buf, int nbytes) |
| 1088 | { |
| 1089 | int ret; |
| 1090 | int tried_reset = 0; |
| 1091 | |
| 1092 | while ((ret = read(fd, buf, nbytes)) == -1) { |
| 1093 | if (!tried_reset && errno == EAGAIN) { |
| 1094 | int oerrno = errno; |
| 1095 | if (reset_nonblock(fd) > 0) { |
| 1096 | tried_reset = 1; |
| 1097 | continue; |
| 1098 | } |
| 1099 | errno = oerrno; |
| 1100 | } |
| 1101 | break; |
| 1102 | } |
| 1103 | return ret; |
| 1104 | } |
| 1105 | |
| 1106 | |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | int |
| 1111 | reset_nonblock(int fd) |
| 1112 | { |
| 1113 | int flags; |
| 1114 | |
| 1115 | if ((flags = fcntl(fd, F_GETFL)) == -1) |
| 1116 | return -1; |
| 1117 | if (!(flags & O_NONBLOCK)) |
| 1118 | return 0; |
| 1119 | flags &= ~O_NONBLOCK; |
| 1120 | if (fcntl(fd, F_SETFL, flags) == -1) |
| 1121 | return -1; |
| 1122 | return 1; |
| 1123 | } |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | char * |
| 1128 | ksh_get_wd(char *buf, int bsize) |
| 1129 | { |
| 1130 | char *b; |
| 1131 | char *ret; |
| 1132 | |
| 1133 | |
| 1134 | |
| 1135 | |
| 1136 | if (!buf) { |
| 1137 | bsize = PATH_MAX; |
| 1138 | b = alloc(bsize, ATEMP); |
| 1139 | } else |
| 1140 | b = buf; |
| 1141 | |
| 1142 | ret = getcwd(b, bsize); |
| 1143 | |
| 1144 | if (!buf) { |
| 1145 | if (ret) |
| 1146 | ret = aresize(b, strlen(b) + 1, ATEMP); |
| 1147 | else |
| 1148 | afree(b, ATEMP); |
| 1149 | } |
| 1150 | |
| 1151 | return ret; |
| 1152 | } |