1 /* 2 * Copyright (c) Ian F. Darwin 1986-1995. 3 * Software written by Ian F. Darwin and others; 4 * maintained 1995-present by Christos Zoulas and others. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice immediately at the beginning of the file, without modification, 11 * this list of conditions, and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 /* 29 * compress routines: 30 * zmagic() - returns 0 if not recognized, uncompresses and prints 31 * information if recognized 32 * uncompress(method, old, n, newch) - uncompress old into new, 33 * using method, return sizeof new 34 */ 35 #include "file.h" 36 37 #ifndef lint 38 FILE_RCSID("@(#)$File: compress.c,v 1.96 2016/04/20 00:00:26 christos Exp $") 39 #endif 40 41 #include "magic.h" 42 #include <stdlib.h> 43 #ifdef HAVE_UNISTD_H 44 #include <unistd.h> 45 #endif 46 #include <string.h> 47 #include <errno.h> 48 #include <ctype.h> 49 #include <stdarg.h> 50 #ifdef HAVE_SIGNAL_H 51 #include <signal.h> 52 # ifndef HAVE_SIG_T 53 typedef void (*sig_t)(int); 54 # endif /* HAVE_SIG_T */ 55 #endif 56 #if !defined(__MINGW32__) && !defined(WIN32) 57 #include <sys/ioctl.h> 58 #endif 59 #ifdef HAVE_SYS_WAIT_H 60 #include <sys/wait.h> 61 #endif 62 #if defined(HAVE_SYS_TIME_H) 63 #include <sys/time.h> 64 #endif 65 #if defined(HAVE_ZLIB_H) && defined(HAVE_LIBZ) 66 #define BUILTIN_DECOMPRESS 67 #include <zlib.h> 68 #define ZLIBSUPPORT 69 #endif 70 #ifdef DEBUG 71 int tty = -1; 72 #define DPRINTF(...) do { \ 73 if (tty == -1) \ 74 tty = open("/dev/tty", O_RDWR); \ 75 if (tty == -1) \ 76 abort(); \ 77 dprintf(tty, __VA_ARGS__); \ 78 } while (/*CONSTCOND*/0) 79 #else 80 #define DPRINTF(...) 81 #endif 82 83 #ifdef ZLIBSUPPORT 84 /* 85 * The following python code is not really used because ZLIBSUPPORT is only 86 * defined if we have a built-in zlib, and the built-in zlib handles that. 87 */ 88 static const char zlibcode[] = 89 "import sys, zlib; sys.stdout.write(zlib.decompress(sys.stdin.read()))"; 90 91 static const char *zlib_args[] = { "python", "-c", zlibcode, NULL }; 92 93 static int 94 zlibcmp(const unsigned char *buf) 95 { 96 unsigned short x = 1; 97 unsigned char *s = (unsigned char *)&x; 98 99 if ((buf[0] & 0xf) != 8 || (buf[0] & 0x80) != 0) 100 return 0; 101 if (s[0] != 1) /* endianness test */ 102 x = buf[0] | (buf[1] << 8); 103 else 104 x = buf[1] | (buf[0] << 8); 105 if (x % 31) 106 return 0; 107 return 1; 108 } 109 #endif 110 111 #define gzip_flags "-cd" 112 #define lrzip_flags "-do" 113 #define lzip_flags gzip_flags 114 115 static const char *gzip_args[] = { 116 "gzip", gzip_flags, NULL 117 }; 118 static const char *uncompress_args[] = { 119 "uncompress", "-c", NULL 120 }; 121 static const char *bzip2_args[] = { 122 "bzip2", "-cd", NULL 123 }; 124 static const char *lzip_args[] = { 125 "lzip", lzip_flags, NULL 126 }; 127 static const char *xz_args[] = { 128 "xz", "-cd", NULL 129 }; 130 static const char *lrzip_args[] = { 131 "lrzip", lrzip_flags, NULL 132 }; 133 static const char *lz4_args[] = { 134 "lz4", "-cd", NULL 135 }; 136 137 private const struct { 138 const void *magic; 139 size_t maglen; 140 const char **argv; 141 } compr[] = { 142 { "\037\235", 2, gzip_args }, /* compressed */ 143 /* Uncompress can get stuck; so use gzip first if we have it 144 * Idea from Damien Clark, thanks! */ 145 { "\037\235", 2, uncompress_args }, /* compressed */ 146 { "\037\213", 2, gzip_args }, /* gzipped */ 147 { "\037\236", 2, gzip_args }, /* frozen */ 148 { "\037\240", 2, gzip_args }, /* SCO LZH */ 149 /* the standard pack utilities do not accept standard input */ 150 { "\037\036", 2, gzip_args }, /* packed */ 151 { "PK\3\4", 4, gzip_args }, /* pkzipped, */ 152 /* ...only first file examined */ 153 { "BZh", 3, bzip2_args }, /* bzip2-ed */ 154 { "LZIP", 4, lzip_args }, /* lzip-ed */ 155 { "\3757zXZ\0", 6, xz_args }, /* XZ Utils */ 156 { "LRZI", 4, lrzip_args }, /* LRZIP */ 157 { "\004\"M\030",4, lz4_args }, /* LZ4 */ 158 #ifdef ZLIBSUPPORT 159 { zlibcmp, 0, zlib_args }, /* zlib */ 160 #endif 161 }; 162 163 #define OKDATA 0 164 #define NODATA 1 165 #define ERRDATA 2 166 167 private ssize_t swrite(int, const void *, size_t); 168 #if HAVE_FORK 169 private size_t ncompr = sizeof(compr) / sizeof(compr[0]); 170 private int uncompressbuf(int, size_t, size_t, const unsigned char *, 171 unsigned char **, size_t *); 172 #ifdef BUILTIN_DECOMPRESS 173 private int uncompresszlib(const unsigned char *, unsigned char **, size_t, 174 size_t *, int); 175 private int uncompressgzipped(const unsigned char *, unsigned char **, size_t, 176 size_t *); 177 #endif 178 static int makeerror(unsigned char **, size_t *, const char *, ...) 179 __attribute__((__format__(__printf__, 3, 4))); 180 private const char *methodname(size_t); 181 182 protected int 183 file_zmagic(struct magic_set *ms, int fd, const char *name, 184 const unsigned char *buf, size_t nbytes) 185 { 186 unsigned char *newbuf = NULL; 187 size_t i, nsz; 188 char *rbuf; 189 file_pushbuf_t *pb; 190 int urv, prv, rv = 0; 191 int mime = ms->flags & MAGIC_MIME; 192 #ifdef HAVE_SIGNAL_H 193 sig_t osigpipe; 194 #endif 195 196 if ((ms->flags & MAGIC_COMPRESS) == 0) 197 return 0; 198 199 #ifdef HAVE_SIGNAL_H 200 osigpipe = signal(SIGPIPE, SIG_IGN); 201 #endif 202 for (i = 0; i < ncompr; i++) { 203 int zm; 204 if (nbytes < compr[i].maglen) 205 continue; 206 #ifdef ZLIBSUPPORT 207 if (compr[i].maglen == 0) 208 zm = (CAST(int (*)(const unsigned char *), 209 CCAST(void *, compr[i].magic)))(buf); 210 else 211 #endif 212 zm = memcmp(buf, compr[i].magic, compr[i].maglen) == 0; 213 214 if (!zm) 215 continue; 216 nsz = nbytes; 217 urv = uncompressbuf(fd, ms->bytes_max, i, buf, &newbuf, &nsz); 218 DPRINTF("uncompressbuf = %d, %s, %zu\n", urv, (char *)newbuf, 219 nsz); 220 switch (urv) { 221 case OKDATA: 222 case ERRDATA: 223 224 ms->flags &= ~MAGIC_COMPRESS; 225 if (urv == ERRDATA) 226 prv = file_printf(ms, "%s ERROR: %s", 227 methodname(i), newbuf); 228 else 229 prv = file_buffer(ms, -1, name, newbuf, nsz); 230 if (prv == -1) 231 goto error; 232 rv = 1; 233 if ((ms->flags & MAGIC_COMPRESS_TRANSP) != 0) 234 goto out; 235 if (mime != MAGIC_MIME && mime != 0) 236 goto out; 237 if ((file_printf(ms, 238 mime ? " compressed-encoding=" : " (")) == -1) 239 goto error; 240 if ((pb = file_push_buffer(ms)) == NULL) 241 goto error; 242 /* 243 * XXX: If file_buffer fails here, we overwrite 244 * the compressed text. FIXME. 245 */ 246 if (file_buffer(ms, -1, NULL, buf, nbytes) == -1) 247 goto error; 248 if ((rbuf = file_pop_buffer(ms, pb)) != NULL) { 249 if (file_printf(ms, "%s", rbuf) == -1) { 250 free(rbuf); 251 goto error; 252 } 253 free(rbuf); 254 } 255 if (!mime && file_printf(ms, ")") == -1) 256 goto error; 257 /*FALLTHROUGH*/ 258 case NODATA: 259 break; 260 default: 261 abort(); 262 /*NOTREACHED*/ 263 error: 264 rv = -1; 265 break; 266 } 267 } 268 out: 269 DPRINTF("rv = %d\n", rv); 270 271 #ifdef HAVE_SIGNAL_H 272 (void)signal(SIGPIPE, osigpipe); 273 #endif 274 free(newbuf); 275 ms->flags |= MAGIC_COMPRESS; 276 DPRINTF("Zmagic returns %d\n", rv); 277 return rv; 278 } 279 #endif 280 /* 281 * `safe' write for sockets and pipes. 282 */ 283 private ssize_t 284 swrite(int fd, const void *buf, size_t n) 285 { 286 ssize_t rv; 287 size_t rn = n; 288 289 do 290 switch (rv = write(fd, buf, n)) { 291 case -1: 292 if (errno == EINTR) 293 continue; 294 return -1; 295 default: 296 n -= rv; 297 buf = CAST(const char *, buf) + rv; 298 break; 299 } 300 while (n > 0); 301 return rn; 302 } 303 304 305 /* 306 * `safe' read for sockets and pipes. 307 */ 308 protected ssize_t 309 sread(int fd, void *buf, size_t n, int canbepipe __attribute__((__unused__))) 310 { 311 ssize_t rv; 312 #ifdef FIONREAD 313 int t = 0; 314 #endif 315 size_t rn = n; 316 317 if (fd == STDIN_FILENO) 318 goto nocheck; 319 320 #ifdef FIONREAD 321 if (canbepipe && (ioctl(fd, FIONREAD, &t) == -1 || t == 0)) { 322 #ifdef FD_ZERO 323 ssize_t cnt; 324 for (cnt = 0;; cnt++) { 325 fd_set check; 326 struct timeval tout = {0, 100 * 1000}; 327 int selrv; 328 329 FD_ZERO(&check); 330 FD_SET(fd, &check); 331 332 /* 333 * Avoid soft deadlock: do not read if there 334 * is nothing to read from sockets and pipes. 335 */ 336 selrv = select(fd + 1, &check, NULL, NULL, &tout); 337 if (selrv == -1) { 338 if (errno == EINTR || errno == EAGAIN) 339 continue; 340 } else if (selrv == 0 && cnt >= 5) { 341 return 0; 342 } else 343 break; 344 } 345 #endif 346 (void)ioctl(fd, FIONREAD, &t); 347 } 348 349 if (t > 0 && (size_t)t < n) { 350 n = t; 351 rn = n; 352 } 353 #endif 354 355 nocheck: 356 do 357 switch ((rv = read(fd, buf, n))) { 358 case -1: 359 if (errno == EINTR) 360 continue; 361 return -1; 362 case 0: 363 return rn - n; 364 default: 365 n -= rv; 366 buf = ((char *)buf) + rv; 367 break; 368 } 369 while (n > 0); 370 return rn; 371 } 372 373 protected int 374 file_pipe2file(struct magic_set *ms, int fd, const void *startbuf, 375 size_t nbytes) 376 { 377 char buf[4096]; 378 ssize_t r; 379 int tfd; 380 381 (void)strlcpy(buf, "/tmp/file.XXXXXX", sizeof buf); 382 #ifndef HAVE_MKSTEMP 383 { 384 char *ptr = mktemp(buf); 385 tfd = open(ptr, O_RDWR|O_TRUNC|O_EXCL|O_CREAT, 0600); 386 r = errno; 387 (void)unlink(ptr); 388 errno = r; 389 } 390 #else 391 { 392 int te; 393 tfd = mkstemp(buf); 394 te = errno; 395 (void)unlink(buf); 396 errno = te; 397 } 398 #endif 399 if (tfd == -1) { 400 file_error(ms, errno, 401 "cannot create temporary file for pipe copy"); 402 return -1; 403 } 404 405 if (swrite(tfd, startbuf, nbytes) != (ssize_t)nbytes) 406 r = 1; 407 else { 408 while ((r = sread(fd, buf, sizeof(buf), 1)) > 0) 409 if (swrite(tfd, buf, (size_t)r) != r) 410 break; 411 } 412 413 switch (r) { 414 case -1: 415 file_error(ms, errno, "error copying from pipe to temp file"); 416 return -1; 417 case 0: 418 break; 419 default: 420 file_error(ms, errno, "error while writing to temp file"); 421 return -1; 422 } 423 424 /* 425 * We duplicate the file descriptor, because fclose on a 426 * tmpfile will delete the file, but any open descriptors 427 * can still access the phantom inode. 428 */ 429 if ((fd = dup2(tfd, fd)) == -1) { 430 file_error(ms, errno, "could not dup descriptor for temp file"); 431 return -1; 432 } 433 (void)close(tfd); 434 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) { 435 file_badseek(ms); 436 return -1; 437 } 438 return fd; 439 } 440 #if HAVE_FORK 441 #ifdef BUILTIN_DECOMPRESS 442 443 #define FHCRC (1 << 1) 444 #define FEXTRA (1 << 2) 445 #define FNAME (1 << 3) 446 #define FCOMMENT (1 << 4) 447 448 449 private int 450 uncompressgzipped(const unsigned char *old, unsigned char **newch, 451 size_t bytes_max, size_t *n) 452 { 453 unsigned char flg = old[3]; 454 size_t data_start = 10; 455 456 if (flg & FEXTRA) { 457 if (data_start + 1 >= *n) 458 goto err; 459 data_start += 2 + old[data_start] + old[data_start + 1] * 256; 460 } 461 if (flg & FNAME) { 462 while(data_start < *n && old[data_start]) 463 data_start++; 464 data_start++; 465 } 466 if (flg & FCOMMENT) { 467 while(data_start < *n && old[data_start]) 468 data_start++; 469 data_start++; 470 } 471 if (flg & FHCRC) 472 data_start += 2; 473 474 if (data_start >= *n) 475 goto err; 476 477 *n -= data_start; 478 old += data_start; 479 return uncompresszlib(old, newch, bytes_max, n, 0); 480 err: 481 return makeerror(newch, n, "File too short"); 482 } 483 484 private int 485 uncompresszlib(const unsigned char *old, unsigned char **newch, 486 size_t bytes_max, size_t *n, int zlib) 487 { 488 int rc; 489 z_stream z; 490 491 if ((*newch = CAST(unsigned char *, malloc(bytes_max + 1))) == NULL) 492 return makeerror(newch, n, "No buffer, %s", strerror(errno)); 493 494 z.next_in = CCAST(Bytef *, old); 495 z.avail_in = CAST(uint32_t, *n); 496 z.next_out = *newch; 497 z.avail_out = bytes_max; 498 z.zalloc = Z_NULL; 499 z.zfree = Z_NULL; 500 z.opaque = Z_NULL; 501 502 /* LINTED bug in header macro */ 503 rc = zlib ? inflateInit(&z) : inflateInit2(&z, -15); 504 if (rc != Z_OK) 505 goto err; 506 507 rc = inflate(&z, Z_SYNC_FLUSH); 508 if (rc != Z_OK && rc != Z_STREAM_END) 509 goto err; 510 511 *n = (size_t)z.total_out; 512 rc = inflateEnd(&z); 513 if (rc != Z_OK) 514 goto err; 515 516 /* let's keep the nul-terminate tradition */ 517 (*newch)[*n] = '\0'; 518 519 return OKDATA; 520 err: 521 strlcpy((char *)*newch, z.msg, bytes_max); 522 *n = strlen((char *)*newch); 523 return ERRDATA; 524 } 525 #endif 526 527 static int 528 makeerror(unsigned char **buf, size_t *len, const char *fmt, ...) 529 { 530 char *msg; 531 va_list ap; 532 int rv; 533 534 va_start(ap, fmt); 535 rv = vasprintf(&msg, fmt, ap); 536 va_end(ap); 537 if (rv < 0) { 538 *buf = NULL; 539 *len = 0; 540 return NODATA; 541 } 542 *buf = (unsigned char *)msg; 543 *len = strlen(msg); 544 return ERRDATA; 545 } 546 547 static void 548 closefd(int *fd, size_t i) 549 { 550 if (fd[i] == -1) 551 return; 552 (void) close(fd[i]); 553 fd[i] = -1; 554 } 555 556 static void 557 closep(int *fd) 558 { 559 size_t i; 560 for (i = 0; i < 2; i++) 561 closefd(fd, i); 562 } 563 564 static void 565 copydesc(int i, int *fd) 566 { 567 int j = fd[i == STDIN_FILENO ? 0 : 1]; 568 if (j == i) 569 return; 570 if (dup2(j, i) == -1) { 571 DPRINTF("dup(%d, %d) failed (%s)\n", j, i, strerror(errno)); 572 exit(1); 573 } 574 closep(fd); 575 } 576 577 static void 578 writechild(int fdp[3][2], const void *old, size_t n) 579 { 580 int status; 581 582 closefd(fdp[STDIN_FILENO], 0); 583 /* 584 * fork again, to avoid blocking because both 585 * pipes filled 586 */ 587 switch (fork()) { 588 case 0: /* child */ 589 closefd(fdp[STDOUT_FILENO], 0); 590 if (swrite(fdp[STDIN_FILENO][1], old, n) != (ssize_t)n) { 591 DPRINTF("Write failed (%s)\n", strerror(errno)); 592 exit(1); 593 } 594 exit(0); 595 /*NOTREACHED*/ 596 597 case -1: 598 DPRINTF("Fork failed (%s)\n", strerror(errno)); 599 exit(1); 600 /*NOTREACHED*/ 601 602 default: /* parent */ 603 if (wait(&status) == -1) { 604 DPRINTF("Wait failed (%s)\n", strerror(errno)); 605 exit(1); 606 } 607 DPRINTF("Grandchild wait return %#x\n", status); 608 } 609 closefd(fdp[STDIN_FILENO], 1); 610 } 611 612 static ssize_t 613 filter_error(unsigned char *ubuf, ssize_t n) 614 { 615 char *p; 616 char *buf; 617 618 ubuf[n] = '\0'; 619 buf = (char *)ubuf; 620 while (isspace((unsigned char)*buf)) 621 buf++; 622 DPRINTF("Filter error[[[%s]]]\n", buf); 623 if ((p = strchr((char *)buf, '\n')) != NULL) 624 *p = '\0'; 625 if ((p = strchr((char *)buf, ';')) != NULL) 626 *p = '\0'; 627 if ((p = strrchr((char *)buf, ':')) != NULL) { 628 ++p; 629 while (isspace((unsigned char)*p)) 630 p++; 631 n = strlen(p); 632 memmove(ubuf, p, n + 1); 633 } 634 DPRINTF("Filter error after[[[%s]]]\n", (char *)ubuf); 635 if (islower(*ubuf)) 636 *ubuf = toupper(*ubuf); 637 return n; 638 } 639 640 private const char * 641 methodname(size_t method) 642 { 643 #ifdef BUILTIN_DECOMPRESS 644 /* FIXME: This doesn't cope with bzip2 */ 645 if (method == 2 || compr[method].maglen == 0) 646 return "zlib"; 647 #endif 648 return compr[method].argv[0]; 649 } 650 651 private int 652 uncompressbuf(int fd, size_t bytes_max, size_t method, const unsigned char *old, 653 unsigned char **newch, size_t* n) 654 { 655 int fdp[3][2]; 656 int status, rv; 657 size_t i; 658 ssize_t r; 659 660 #ifdef BUILTIN_DECOMPRESS 661 /* FIXME: This doesn't cope with bzip2 */ 662 if (method == 2) 663 return uncompressgzipped(old, newch, bytes_max, n); 664 if (compr[method].maglen == 0) 665 return uncompresszlib(old, newch, bytes_max, n, 1); 666 #endif 667 (void)fflush(stdout); 668 (void)fflush(stderr); 669 670 for (i = 0; i < __arraycount(fdp); i++) 671 fdp[i][0] = fdp[i][1] = -1; 672 673 if ((fd == -1 && pipe(fdp[STDIN_FILENO]) == -1) || 674 pipe(fdp[STDOUT_FILENO]) == -1 || pipe(fdp[STDERR_FILENO]) == -1) { 675 closep(fdp[STDIN_FILENO]); 676 closep(fdp[STDOUT_FILENO]); 677 return makeerror(newch, n, "Cannot create pipe, %s", 678 strerror(errno)); 679 } 680 switch (fork()) { 681 case 0: /* child */ 682 if (fd != -1) { 683 fdp[STDIN_FILENO][0] = fd; 684 (void) lseek(fd, (off_t)0, SEEK_SET); 685 } 686 687 for (i = 0; i < __arraycount(fdp); i++) 688 copydesc(i, fdp[i]); 689 690 (void)execvp(compr[method].argv[0], 691 (char *const *)(intptr_t)compr[method].argv); 692 dprintf(STDERR_FILENO, "exec `%s' failed, %s", 693 compr[method].argv[0], strerror(errno)); 694 exit(1); 695 /*NOTREACHED*/ 696 case -1: 697 return makeerror(newch, n, "Cannot fork, %s", 698 strerror(errno)); 699 700 default: /* parent */ 701 for (i = 1; i < __arraycount(fdp); i++) 702 closefd(fdp[i], 1); 703 704 /* Write the buffer data to the child, if we don't have fd */ 705 if (fd == -1) 706 writechild(fdp, old, *n); 707 708 *newch = CAST(unsigned char *, malloc(bytes_max + 1)); 709 if (*newch == NULL) { 710 rv = makeerror(newch, n, "No buffer, %s", 711 strerror(errno)); 712 goto err; 713 } 714 rv = OKDATA; 715 if ((r = sread(fdp[STDOUT_FILENO][0], *newch, bytes_max, 0)) > 0) 716 break; 717 DPRINTF("Read stdout failed %d (%s)\n", fdp[STDOUT_FILENO][0], 718 r != -1 ? strerror(errno) : "no data"); 719 720 rv = ERRDATA; 721 if (r == 0 && 722 (r = sread(fdp[STDERR_FILENO][0], *newch, bytes_max, 0)) > 0) 723 { 724 r = filter_error(*newch, r); 725 break; 726 } 727 free(*newch); 728 if (r == 0) 729 rv = makeerror(newch, n, "Read failed, %s", 730 strerror(errno)); 731 else 732 rv = makeerror(newch, n, "No data"); 733 goto err; 734 } 735 736 *n = r; 737 /* NUL terminate, as every buffer is handled here. */ 738 (*newch)[*n] = '\0'; 739 err: 740 closefd(fdp[STDIN_FILENO], 1); 741 closefd(fdp[STDOUT_FILENO], 0); 742 closefd(fdp[STDERR_FILENO], 0); 743 if (wait(&status) == -1) { 744 free(*newch); 745 rv = makeerror(newch, n, "Wait failed, %s", strerror(errno)); 746 DPRINTF("Child wait return %#x\n", status); 747 } else if (!WIFEXITED(status)) { 748 DPRINTF("Child not exited (0x%x)\n", status); 749 } else if (WEXITSTATUS(status) != 0) { 750 DPRINTF("Child exited (0x%d)\n", WEXITSTATUS(status)); 751 } 752 753 closefd(fdp[STDIN_FILENO], 0); 754 DPRINTF("Returning %p n=%zu rv=%d\n", *newch, *n, rv); 755 756 return rv; 757 } 758 #endif 759