clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name sem.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/csh/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/bin/csh -I . -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/bin/csh/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/csh/sem.c
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <errno.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | #include <unistd.h> |
| 40 | #include <limits.h> |
| 41 | #include <stdarg.h> |
| 42 | |
| 43 | #include "csh.h" |
| 44 | #include "proc.h" |
| 45 | #include "extern.h" |
| 46 | |
| 47 | static void vffree(int); |
| 48 | static Char *splicepipe(struct command *t, Char *); |
| 49 | static void doio(struct command *t, int *, int *); |
| 50 | static void chkclob(char *); |
| 51 | |
| 52 | void |
| 53 | execute(struct command *t, int wanttty, int *pipein, int *pipeout) |
| 54 | { |
| 55 | bool forked = 0; |
| 56 | struct biltins *bifunc; |
| 57 | int pid = 0; |
| 58 | int pv[2]; |
| 59 | sigset_t sigset; |
| 60 | |
| 61 | static sigset_t csigset; |
| 62 | |
| 63 | static sigset_t ocsigset; |
| 64 | static int onosigchld = 0; |
| 65 | static int nosigchld = 0; |
| 66 | |
| 67 | UNREGISTER(forked); |
| 68 | UNREGISTER(bifunc); |
| 69 | UNREGISTER(wanttty); |
| 70 | |
| 71 | if (t == 0) |
| 1 | Assuming 't' is not equal to null | |
|
| |
| 72 | return; |
| 73 | |
| 74 | if (t->t_dflg & F_AMPERSAND) |
| 3 | | Assuming the condition is false | |
|
| |
| 75 | wanttty = 0; |
| 76 | switch (t->t_dtyp) { |
| 5 | | Control jumps to 'case 2:' at line 88 | |
|
| 77 | |
| 78 | case NODE_COMMAND: |
| 79 | if ((t->t_dcom[0][0] & (QUOTE | TRIM)) == QUOTE) |
| 80 | (void) memmove(t->t_dcom[0], t->t_dcom[0] + 1, |
| 81 | (Strlen(t->t_dcom[0] + 1) + 1) * sizeof(Char)); |
| 82 | if ((t->t_dflg & F_REPEAT) == 0) |
| 83 | Dfix(t); |
| 84 | if (t->t_dcom[0] == 0) |
| 85 | return; |
| 86 | |
| 87 | |
| 88 | case NODE_PAREN: |
| 89 | if (t->t_dflg & F_PIPEOUT) |
| 6 | | Assuming the condition is false | |
|
| |
| 90 | mypipe(pipeout); |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | if (t->t_dflg & F_READ) { |
| 8 | | Assuming the condition is true | |
|
| |
| 96 | (void) close(0); |
| 97 | heredoc(t->t_dlef); |
| 98 | if (noexec) |
| |
| |
| 99 | (void) close(0); |
| 100 | } |
| 101 | |
| 102 | set(STRstatus, Strsave(STR0)); |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | while (t->t_dtyp == NODE_COMMAND) |
| 12 | | Loop condition is false. Execution continues on line 148 | |
|
| 111 | if (eq(t->t_dcom[0], STRnice)) |
| 112 | if (t->t_dcom[1]) |
| 113 | if (strchr("+-", t->t_dcom[1][0])) |
| 114 | if (t->t_dcom[2]) { |
| 115 | setname("nice"); |
| 116 | t->t_nice = |
| 117 | getn(t->t_dcom[1]); |
| 118 | lshift(t->t_dcom, 2); |
| 119 | t->t_dflg |= F_NICE; |
| 120 | } |
| 121 | else |
| 122 | break; |
| 123 | else { |
| 124 | t->t_nice = 4; |
| 125 | lshift(t->t_dcom, 1); |
| 126 | t->t_dflg |= F_NICE; |
| 127 | } |
| 128 | else |
| 129 | break; |
| 130 | else if (eq(t->t_dcom[0], STRnohup)) |
| 131 | if (t->t_dcom[1]) { |
| 132 | t->t_dflg |= F_NOHUP; |
| 133 | lshift(t->t_dcom, 1); |
| 134 | } |
| 135 | else |
| 136 | break; |
| 137 | else if (eq(t->t_dcom[0], STRtime)) |
| 138 | if (t->t_dcom[1]) { |
| 139 | t->t_dflg |= F_TIME; |
| 140 | lshift(t->t_dcom, 1); |
| 141 | } |
| 142 | else |
| 143 | break; |
| 144 | else |
| 145 | break; |
| 146 | |
| 147 | |
| 148 | if (t->t_dtyp == NODE_COMMAND) { |
| |
| 149 | |
| 150 | |
| 151 | |
| 152 | bifunc = isbfunc(t); |
| 153 | if (noexec) { |
| 154 | |
| 155 | |
| 156 | |
| 157 | if (bifunc && |
| 158 | bifunc->bfunct != dobreak && bifunc->bfunct != docontin && |
| 159 | bifunc->bfunct != doelse && bifunc->bfunct != doend && |
| 160 | bifunc->bfunct != doforeach && bifunc->bfunct != dogoto && |
| 161 | bifunc->bfunct != doif && bifunc->bfunct != dorepeat && |
| 162 | bifunc->bfunct != doswbrk && bifunc->bfunct != doswitch && |
| 163 | bifunc->bfunct != dowhile && bifunc->bfunct != dozip) |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | else { |
| 168 | bifunc = NULL; |
| 169 | if (noexec) |
| |
| |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | if (bifunc && (bifunc->bfunct == dochngd || |
| 184 | bifunc->bfunct == dopushd || |
| 185 | bifunc->bfunct == dopopd)) |
| 186 | t->t_dflg &= ~(F_NICE); |
| 187 | if (((t->t_dflg & F_TIME) || ((t->t_dflg & F_NOFORK) == 0 && |
| 16 | | Assuming the condition is false | |
|
| 17 | | Assuming the condition is false | |
|
| 188 | (!bifunc || t->t_dflg & |
| 189 | (F_PIPEOUT | F_AMPERSAND | F_NICE | F_NOHUP)))) || |
| 190 | |
| 191 | |
| 192 | |
| 193 | (bifunc && (t->t_dflg & (F_PIPEIN | F_PIPEOUT)) != 0 && |
| 194 | bifunc->bfunct == doeval)) { |
| 195 | if (t->t_dtyp == NODE_PAREN || |
| 196 | t->t_dflg & (F_REPEAT | F_AMPERSAND) || bifunc) { |
| 197 | forked++; |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | if (wanttty >= 0 && !nosigchld) { |
| 203 | sigemptyset(&sigset); |
| 204 | sigaddset(&sigset, SIGCHLD); |
| 205 | sigprocmask(SIG_BLOCK, &sigset, &csigset); |
| 206 | nosigchld = 1; |
| 207 | } |
| 208 | |
| 209 | pid = pfork(t, wanttty); |
| 210 | if (pid == 0 && nosigchld) { |
| 211 | sigprocmask(SIG_SETMASK, &csigset, NULL); |
| 212 | nosigchld = 0; |
| 213 | } |
| 214 | else if (pid != 0 && (t->t_dflg & F_AMPERSAND)) |
| 215 | backpid = pid; |
| 216 | |
| 217 | } |
| 218 | else { |
| 219 | int ochild, osetintr, ohaderr, odidfds; |
| 220 | int oSHIN, oSHOUT, oSHERR, oOLDSTD, otpgrp; |
| 221 | sigset_t osigset; |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | if (wanttty >= 0 && !nosigchld && !noexec) { |
| 232 | sigemptyset(&sigset); |
| 233 | sigaddset(&sigset, SIGCHLD); |
| 234 | sigprocmask(SIG_BLOCK, &sigset, &csigset); |
| 235 | nosigchld = 1; |
| 236 | } |
| 237 | sigemptyset(&sigset); |
| 238 | sigaddset(&sigset, SIGCHLD); |
| 239 | sigaddset(&sigset, SIGINT); |
| 240 | sigprocmask(SIG_BLOCK, &sigset, &osigset); |
| 241 | ochild = child; |
| 242 | osetintr = setintr; |
| 243 | ohaderr = haderr; |
| 244 | odidfds = didfds; |
| 245 | oSHIN = SHIN; |
| 246 | oSHOUT = SHOUT; |
| 247 | oSHERR = SHERR; |
| 248 | oOLDSTD = OLDSTD; |
| 249 | otpgrp = tpgrp; |
| 250 | ocsigset = csigset; |
| 251 | onosigchld = nosigchld; |
| 252 | Vsav = Vdp = NULL; |
| 253 | Vexpath = 0; |
| 254 | Vt = 0; |
| 255 | pid = vfork(); |
| 256 | |
| 257 | if (pid == -1) { |
| 258 | sigprocmask(SIG_SETMASK, &osigset, NULL); |
| 259 | stderror(ERR_NOPROC); |
| 260 | } |
| 261 | forked++; |
| 262 | if (pid) { |
| 263 | child = ochild; |
| 264 | setintr = osetintr; |
| 265 | haderr = ohaderr; |
| 266 | didfds = odidfds; |
| 267 | SHIN = oSHIN; |
| 268 | SHOUT = oSHOUT; |
| 269 | SHERR = oSHERR; |
| 270 | OLDSTD = oOLDSTD; |
| 271 | tpgrp = otpgrp; |
| 272 | csigset = ocsigset; |
| 273 | nosigchld = onosigchld; |
| 274 | |
| 275 | free(Vsav); |
| 276 | Vsav = NULL; |
| 277 | free(Vdp); |
| 278 | Vdp = NULL; |
| 279 | free(Vexpath); |
| 280 | Vexpath = NULL; |
| 281 | blkfree((Char **) Vt); |
| 282 | Vt = NULL; |
| 283 | |
| 284 | palloc(pid, t); |
| 285 | sigprocmask(SIG_SETMASK, &osigset, NULL); |
| 286 | } |
| 287 | else { |
| 288 | |
| 289 | int pgrp; |
| 290 | bool ignint = 0; |
| 291 | |
| 292 | if (nosigchld) { |
| 293 | sigprocmask(SIG_SETMASK, &csigset, NULL); |
| 294 | nosigchld = 0; |
| 295 | } |
| 296 | |
| 297 | if (setintr) |
| 298 | ignint = |
| 299 | (tpgrp == -1 && |
| 300 | (t->t_dflg & F_NOINTERRUPT)) |
| 301 | || (gointr && eq(gointr, STRminus)); |
| 302 | pgrp = pcurrjob ? pcurrjob->p_jobid : getpid(); |
| 303 | child++; |
| 304 | if (setintr) { |
| 305 | setintr = 0; |
| 306 | if (ignint) { |
| 307 | (void) signal(SIGINT, SIG_IGN); |
| 308 | (void) signal(SIGQUIT, SIG_IGN); |
| 309 | } |
| 310 | else { |
| 311 | (void) signal(SIGINT, vffree); |
| 312 | (void) signal(SIGQUIT, SIG_DFL); |
| 313 | } |
| 314 | |
| 315 | if (wanttty >= 0) { |
| 316 | (void) signal(SIGTSTP, SIG_DFL); |
| 317 | (void) signal(SIGTTIN, SIG_DFL); |
| 318 | (void) signal(SIGTTOU, SIG_DFL); |
| 319 | } |
| 320 | |
| 321 | (void) signal(SIGTERM, parterm); |
| 322 | } |
| 323 | else if (tpgrp == -1 && |
| 324 | (t->t_dflg & F_NOINTERRUPT)) { |
| 325 | (void) signal(SIGINT, SIG_IGN); |
| 326 | (void) signal(SIGQUIT, SIG_IGN); |
| 327 | } |
| 328 | |
| 329 | pgetty(wanttty, pgrp); |
| 330 | if (t->t_dflg & F_NOHUP) |
| 331 | (void) signal(SIGHUP, SIG_IGN); |
| 332 | if (t->t_dflg & F_NICE) |
| 333 | (void) setpriority(PRIO_PROCESS, 0, t->t_nice); |
| 334 | } |
| 335 | |
| 336 | } |
| 337 | } |
| 338 | if (pid != 0) { |
| |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | if (didfds == 0 && t->t_dflg & F_PIPEIN) { |
| 346 | (void) close(pipein[0]); |
| 347 | (void) close(pipein[1]); |
| 348 | } |
| 349 | if ((t->t_dflg & F_PIPEOUT) == 0) { |
| 350 | if (nosigchld) { |
| 351 | sigprocmask(SIG_SETMASK, &csigset, NULL); |
| 352 | nosigchld = 0; |
| 353 | } |
| 354 | if ((t->t_dflg & F_AMPERSAND) == 0) |
| 355 | pwait(); |
| 356 | } |
| 357 | break; |
| 358 | } |
| 359 | doio(t, pipein, pipeout); |
| |
| 360 | if (t->t_dflg & F_PIPEOUT) { |
| 361 | (void) close(pipeout[0]); |
| 362 | (void) close(pipeout[1]); |
| 363 | } |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | if (bifunc) { |
| 369 | func(t, bifunc); |
| 370 | if (forked) |
| 371 | exitstat(); |
| 372 | break; |
| 373 | } |
| 374 | if (t->t_dtyp != NODE_PAREN) { |
| 375 | doexec(NULL, t); |
| 376 | |
| 377 | } |
| 378 | |
| 379 | |
| 380 | |
| 381 | OLDSTD = dcopy(0, FOLDSTD); |
| 382 | SHOUT = dcopy(1, FSHOUT); |
| 383 | SHERR = dcopy(2, FSHERR); |
| 384 | (void) close(SHIN); |
| 385 | SHIN = -1; |
| 386 | didfds = 0; |
| 387 | wanttty = -1; |
| 388 | t->t_dspr->t_dflg |= t->t_dflg & F_NOINTERRUPT; |
| 389 | execute(t->t_dspr, wanttty, NULL, NULL); |
| 390 | exitstat(); |
| 391 | |
| 392 | case NODE_PIPE: |
| 393 | t->t_dcar->t_dflg |= F_PIPEOUT | |
| 394 | (t->t_dflg & (F_PIPEIN | F_AMPERSAND | F_STDERR | F_NOINTERRUPT)); |
| 395 | execute(t->t_dcar, wanttty, pipein, pv); |
| 396 | t->t_dcdr->t_dflg |= F_PIPEIN | (t->t_dflg & |
| 397 | (F_PIPEOUT | F_AMPERSAND | F_NOFORK | F_NOINTERRUPT)); |
| 398 | if (wanttty > 0) |
| 399 | wanttty = 0; |
| 400 | execute(t->t_dcdr, wanttty, pv, pipeout); |
| 401 | break; |
| 402 | |
| 403 | case NODE_LIST: |
| 404 | if (t->t_dcar) { |
| 405 | t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT; |
| 406 | execute(t->t_dcar, wanttty, NULL, NULL); |
| 407 | |
| 408 | |
| 409 | |
| 410 | if (t->t_dcar->t_dflg & F_AMPERSAND && t->t_dcdr && |
| 411 | (t->t_dcdr->t_dflg & F_AMPERSAND) == 0) |
| 412 | pendjob(); |
| 413 | } |
| 414 | if (t->t_dcdr) { |
| 415 | t->t_dcdr->t_dflg |= t->t_dflg & |
| 416 | (F_NOFORK | F_NOINTERRUPT); |
| 417 | execute(t->t_dcdr, wanttty, NULL, NULL); |
| 418 | } |
| 419 | break; |
| 420 | |
| 421 | case NODE_OR: |
| 422 | case NODE_AND: |
| 423 | if (t->t_dcar) { |
| 424 | t->t_dcar->t_dflg |= t->t_dflg & F_NOINTERRUPT; |
| 425 | execute(t->t_dcar, wanttty, NULL, NULL); |
| 426 | if ((getn(value(STRstatus)) == 0) != |
| 427 | (t->t_dtyp == NODE_AND)) |
| 428 | return; |
| 429 | } |
| 430 | if (t->t_dcdr) { |
| 431 | t->t_dcdr->t_dflg |= t->t_dflg & |
| 432 | (F_NOFORK | F_NOINTERRUPT); |
| 433 | execute(t->t_dcdr, wanttty, NULL, NULL); |
| 434 | } |
| 435 | break; |
| 436 | } |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | if (didfds && !(t->t_dflg & F_REPEAT)) |
| 445 | donefds(); |
| 446 | } |
| 447 | |
| 448 | static void |
| 449 | vffree(int i) |
| 450 | { |
| 451 | _exit(i); |
| 452 | } |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | static Char * |
| 470 | splicepipe(struct command *t, Char *cp) |
| 471 | { |
| 472 | Char *blk[2]; |
| 473 | |
| 474 | if (adrof(STRnoambiguous)) { |
| 27 | | Assuming the condition is true | |
|
| |
| 475 | Char **pv; |
| 476 | |
| 477 | blk[0] = Dfix1(cp); |
| 478 | blk[1] = NULL; |
| 479 | |
| 480 | gflag = 0, tglob(blk); |
| 481 | if (gflag) { |
| 29 | | Assuming 'gflag' is not equal to 0 | |
|
| |
| 482 | pv = globall(blk); |
| |
| 483 | if (pv == NULL) { |
| 32 | | Assuming 'pv' is equal to NULL | |
|
| |
| 484 | setname(vis_str(blk[0])); |
| 485 | free(blk[0]); |
| 486 | stderror(ERR_NAME | ERR_NOMATCH); |
| 487 | } |
| 488 | gargv = NULL; |
| 489 | if (pv[1] != NULL) { |
| 34 | | Array access (from variable 'pv') results in a null pointer dereference |
|
| 490 | Char **av = blkspl(t->t_dcom, &pv[1]); |
| 491 | free(t->t_dcom); |
| 492 | t->t_dcom = av; |
| 493 | } |
| 494 | free(blk[0]); |
| 495 | blk[0] = pv[0]; |
| 496 | free(pv); |
| 497 | } |
| 498 | } |
| 499 | else { |
| 500 | blk[0] = globone(blk[1] = Dfix1(cp), G_ERROR); |
| 501 | free(blk[1]); |
| 502 | } |
| 503 | return(blk[0]); |
| 504 | } |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | static void |
| 511 | doio(struct command *t, int *pipein, int *pipeout) |
| 512 | { |
| 513 | int fd; |
| 514 | Char *cp; |
| 515 | int flags = t->t_dflg; |
| 516 | |
| 517 | if (didfds || (flags & F_REPEAT)) |
| |
| 21 | | Assuming the condition is false | |
|
| |
| 518 | return; |
| 519 | if ((flags & F_READ) == 0) { |
| |
| 520 | if (t->t_dlef) { |
| 521 | char tmp[PATH_MAX]; |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | (void) dcopy(SHIN, 0); |
| 527 | (void) dcopy(SHOUT, 1); |
| 528 | (void) dcopy(SHERR, 2); |
| 529 | cp = splicepipe(t, t->t_dlef); |
| 530 | strlcpy(tmp, short2str(cp), sizeof tmp); |
| 531 | free(cp); |
| 532 | if ((fd = open(tmp, O_RDONLY)) == -1) |
| 533 | stderror(ERR_SYSTEM, tmp, strerror(errno)); |
| 534 | (void) dmove(fd, 0); |
| 535 | } |
| 536 | else if (flags & F_PIPEIN) { |
| 537 | (void) close(0); |
| 538 | (void) dup(pipein[0]); |
| 539 | (void) close(pipein[0]); |
| 540 | (void) close(pipein[1]); |
| 541 | } |
| 542 | else if ((flags & F_NOINTERRUPT) && tpgrp == -1) { |
| 543 | (void) close(0); |
| 544 | (void) open(_PATH_DEVNULL, O_RDONLY); |
| 545 | } |
| 546 | else { |
| 547 | (void) close(0); |
| 548 | (void) dup(OLDSTD); |
| 549 | (void) fcntl(STDIN_FILENO, F_SETFD, 0); |
| 550 | } |
| 551 | } |
| 552 | if (t->t_drit) { |
| 24 | | Assuming field 'T_drit' is non-null | |
|
| |
| 553 | char tmp[PATH_MAX]; |
| 554 | |
| 555 | cp = splicepipe(t, t->t_drit); |
| |
| 556 | strlcpy(tmp, short2str(cp), sizeof tmp); |
| 557 | free(cp); |
| 558 | |
| 559 | |
| 560 | |
| 561 | (void) dcopy(SHOUT, 1); |
| 562 | (void) dcopy(SHERR, 2); |
| 563 | if ((flags & F_APPEND) && |
| 564 | (fd = open(tmp, O_WRONLY | O_APPEND)) >= 0); |
| 565 | else { |
| 566 | if (!(flags & F_OVERWRITE) && adrof(STRnoclobber)) { |
| 567 | if (flags & F_APPEND) |
| 568 | stderror(ERR_SYSTEM, tmp, strerror(errno)); |
| 569 | chkclob(tmp); |
| 570 | } |
| 571 | if ((fd = open(tmp, O_WRONLY | O_CREAT | O_TRUNC, 0666)) == -1) |
| 572 | stderror(ERR_SYSTEM, tmp, strerror(errno)); |
| 573 | } |
| 574 | (void) dmove(fd, 1); |
| 575 | } |
| 576 | else if (flags & F_PIPEOUT) { |
| 577 | (void) close(1); |
| 578 | (void) dup(pipeout[1]); |
| 579 | } |
| 580 | else { |
| 581 | (void) close(1); |
| 582 | (void) dup(SHOUT); |
| 583 | (void) fcntl(STDOUT_FILENO, F_SETFD, 0); |
| 584 | } |
| 585 | |
| 586 | (void) close(2); |
| 587 | if (flags & F_STDERR) { |
| 588 | (void) dup(1); |
| 589 | } |
| 590 | else { |
| 591 | (void) dup(SHERR); |
| 592 | (void) fcntl(STDERR_FILENO, F_SETFD, 0); |
| 593 | } |
| 594 | didfds = 1; |
| 595 | } |
| 596 | |
| 597 | void |
| 598 | mypipe(int *pv) |
| 599 | { |
| 600 | |
| 601 | if (pipe(pv) == -1) |
| 602 | goto oops; |
| 603 | pv[0] = dmove(pv[0], -1); |
| 604 | pv[1] = dmove(pv[1], -1); |
| 605 | if (pv[0] >= 0 && pv[1] >= 0) |
| 606 | return; |
| 607 | oops: |
| 608 | stderror(ERR_PIPE); |
| 609 | } |
| 610 | |
| 611 | static void |
| 612 | chkclob(char *cp) |
| 613 | { |
| 614 | struct stat stb; |
| 615 | |
| 616 | if (stat(cp, &stb) == -1) |
| 617 | return; |
| 618 | if (S_ISCHR(stb.st_mode)) |
| 619 | return; |
| 620 | stderror(ERR_EXISTS, cp); |
| 621 | } |