4
0

system.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /* System-dependent calls for tar.
  2. Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the
  5. Free Software Foundation; either version 2, or (at your option) any later
  6. version.
  7. This program is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  10. Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. #include <system.h>
  15. #include "common.h"
  16. #include <rmt.h>
  17. #include <signal.h>
  18. void
  19. sys_stat_nanoseconds (struct tar_stat_info *st)
  20. {
  21. #if defined(HAVE_STRUCT_STAT_ST_SPARE1)
  22. st->atime_nsec = st->stat.st_spare1 * 1000;
  23. st->mtime_nsec = st->stat.st_spare2 * 1000;
  24. st->ctime_nsec = st->stat.st_spare3 * 1000;
  25. #elif defined(HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC)
  26. st->atime_nsec = st->stat.st_atim.tv_nsec;
  27. st->mtime_nsec = st->stat.st_mtim.tv_nsec;
  28. st->ctime_nsec = st->stat.st_ctim.tv_nsec;
  29. #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC_TV_NSEC)
  30. st->atime_nsec = st->stat.st_atimespec.tv_nsec;
  31. st->mtime_nsec = st->stat.st_mtimespec.tv_nsec;
  32. st->ctime_nsec = st->stat.st_ctimespec.tv_nsec;
  33. #elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
  34. st->atime_nsec = st->stat.st_atimensec;
  35. st->mtime_nsec = st->stat.st_mtimensec;
  36. st->ctime_nsec = st->stat.st_ctimensec;
  37. #else
  38. st->atime_nsec = st->mtime_nsec = st->ctime_nsec = 0;
  39. #endif
  40. }
  41. #if MSDOS
  42. bool
  43. sys_get_archive_stat (void)
  44. {
  45. return 0;
  46. }
  47. bool
  48. sys_file_is_archive (struct tar_stat_info *p)
  49. {
  50. return false;
  51. }
  52. void
  53. sys_save_archive_dev_ino (void)
  54. {
  55. }
  56. void
  57. sys_detect_dev_null_output (void)
  58. {
  59. static char const dev_null[] = "nul";
  60. dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0
  61. || (! _isrmt (archive)));
  62. }
  63. void
  64. sys_drain_input_pipe (void)
  65. {
  66. }
  67. void
  68. sys_wait_for_child (pid_t child_pid)
  69. {
  70. }
  71. void
  72. sys_spawn_shell (void)
  73. {
  74. spawnl (P_WAIT, getenv ("COMSPEC"), "-", 0);
  75. }
  76. /* stat() in djgpp's C library gives a constant number of 42 as the
  77. uid and gid of a file. So, comparing an FTP'ed archive just after
  78. unpack would fail on MSDOS. */
  79. bool
  80. sys_compare_uid (struct stat *a, struct stat *b)
  81. {
  82. return true;
  83. }
  84. bool
  85. sys_compare_gid (struct stat *a, struct stat *b)
  86. {
  87. return true;
  88. }
  89. void
  90. sys_compare_links (struct stat *link_data, struct stat *stat_data)
  91. {
  92. return true;
  93. }
  94. int
  95. sys_truncate (int fd)
  96. {
  97. return write (fd, "", 0);
  98. }
  99. size_t
  100. sys_write_archive_buffer (void)
  101. {
  102. return full_write (archive, record_start->buffer, record_size);
  103. }
  104. /* Set ARCHIVE for writing, then compressing an archive. */
  105. void
  106. sys_child_open_for_compress (void)
  107. {
  108. FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
  109. }
  110. /* Set ARCHIVE for uncompressing, then reading an archive. */
  111. void
  112. sys_child_open_for_uncompress (void)
  113. {
  114. FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
  115. }
  116. #else
  117. extern union block *record_start; /* FIXME */
  118. static struct stat archive_stat; /* stat block for archive file */
  119. bool
  120. sys_get_archive_stat (void)
  121. {
  122. return fstat (archive, &archive_stat) == 0;
  123. }
  124. bool
  125. sys_file_is_archive (struct tar_stat_info *p)
  126. {
  127. return (ar_dev && p->stat.st_dev == ar_dev && p->stat.st_ino == ar_ino);
  128. }
  129. /* Save archive file inode and device numbers */
  130. void
  131. sys_save_archive_dev_ino (void)
  132. {
  133. if (!_isrmt (archive) && S_ISREG (archive_stat.st_mode))
  134. {
  135. ar_dev = archive_stat.st_dev;
  136. ar_ino = archive_stat.st_ino;
  137. }
  138. else
  139. ar_dev = 0;
  140. }
  141. /* Detect if outputting to "/dev/null". */
  142. void
  143. sys_detect_dev_null_output (void)
  144. {
  145. static char const dev_null[] = "/dev/null";
  146. struct stat dev_null_stat;
  147. dev_null_output = (strcmp (archive_name_array[0], dev_null) == 0
  148. || (! _isrmt (archive)
  149. && S_ISCHR (archive_stat.st_mode)
  150. && stat (dev_null, &dev_null_stat) == 0
  151. && archive_stat.st_dev == dev_null_stat.st_dev
  152. && archive_stat.st_ino == dev_null_stat.st_ino));
  153. }
  154. /* Manage to fully drain a pipe we might be reading, so to not break it on
  155. the producer after the EOF block. FIXME: one of these days, GNU tar
  156. might become clever enough to just stop working, once there is no more
  157. work to do, we might have to revise this area in such time. */
  158. void
  159. sys_drain_input_pipe (void)
  160. {
  161. size_t r;
  162. if (access_mode == ACCESS_READ
  163. && ! _isrmt (archive)
  164. && (S_ISFIFO (archive_stat.st_mode) || S_ISSOCK (archive_stat.st_mode)))
  165. while ((r = rmtread (archive, record_start->buffer, record_size)) != 0
  166. && r != SAFE_READ_ERROR)
  167. continue;
  168. }
  169. void
  170. sys_wait_for_child (pid_t child_pid)
  171. {
  172. if (child_pid)
  173. {
  174. int wait_status;
  175. while (waitpid (child_pid, &wait_status, 0) == -1)
  176. if (errno != EINTR)
  177. {
  178. waitpid_error (use_compress_program_option);
  179. break;
  180. }
  181. if (WIFSIGNALED (wait_status))
  182. ERROR ((0, 0, _("Child died with signal %d"),
  183. WTERMSIG (wait_status)));
  184. else if (WEXITSTATUS (wait_status) != 0)
  185. ERROR ((0, 0, _("Child returned status %d"),
  186. WEXITSTATUS (wait_status)));
  187. }
  188. }
  189. void
  190. sys_spawn_shell (void)
  191. {
  192. pid_t child;
  193. const char *shell = getenv ("SHELL");
  194. if (! shell)
  195. shell = "/bin/sh";
  196. child = xfork ();
  197. if (child == 0)
  198. {
  199. execlp (shell, "-sh", "-i", (char *) 0);
  200. exec_fatal (shell);
  201. }
  202. else
  203. {
  204. int wait_status;
  205. while (waitpid (child, &wait_status, 0) == -1)
  206. if (errno != EINTR)
  207. {
  208. waitpid_error (shell);
  209. break;
  210. }
  211. }
  212. }
  213. bool
  214. sys_compare_uid (struct stat *a, struct stat *b)
  215. {
  216. return a->st_uid == b->st_uid;
  217. }
  218. bool
  219. sys_compare_gid (struct stat *a, struct stat *b)
  220. {
  221. return a->st_gid == b->st_gid;
  222. }
  223. bool
  224. sys_compare_links (struct stat *link_data, struct stat *stat_data)
  225. {
  226. return stat_data->st_dev == link_data->st_dev
  227. && stat_data->st_ino == link_data->st_ino;
  228. }
  229. int
  230. sys_truncate (int fd)
  231. {
  232. off_t pos = lseek (fd, (off_t) 0, SEEK_CUR);
  233. return pos < 0 ? -1 : ftruncate (fd, pos);
  234. }
  235. /* Return nonzero if NAME is the name of a regular file, or if the file
  236. does not exist (so it would be created as a regular file). */
  237. static int
  238. is_regular_file (const char *name)
  239. {
  240. struct stat stbuf;
  241. if (stat (name, &stbuf) == 0)
  242. return S_ISREG (stbuf.st_mode);
  243. else
  244. return errno == ENOENT;
  245. }
  246. size_t
  247. sys_write_archive_buffer (void)
  248. {
  249. return rmtwrite (archive, record_start->buffer, record_size);
  250. }
  251. #define PREAD 0 /* read file descriptor from pipe() */
  252. #define PWRITE 1 /* write file descriptor from pipe() */
  253. /* Duplicate file descriptor FROM into becoming INTO.
  254. INTO is closed first and has to be the next available slot. */
  255. static void
  256. xdup2 (int from, int into)
  257. {
  258. if (from != into)
  259. {
  260. int status = close (into);
  261. if (status != 0 && errno != EBADF)
  262. {
  263. int e = errno;
  264. FATAL_ERROR ((0, e, _("Cannot close")));
  265. }
  266. status = dup (from);
  267. if (status != into)
  268. {
  269. if (status < 0)
  270. {
  271. int e = errno;
  272. FATAL_ERROR ((0, e, _("Cannot dup")));
  273. }
  274. abort ();
  275. }
  276. xclose (from);
  277. }
  278. }
  279. /* Set ARCHIVE for writing, then compressing an archive. */
  280. pid_t
  281. sys_child_open_for_compress (void)
  282. {
  283. int parent_pipe[2];
  284. int child_pipe[2];
  285. pid_t grandchild_pid;
  286. pid_t child_pid;
  287. int wait_status;
  288. xpipe (parent_pipe);
  289. child_pid = xfork ();
  290. if (child_pid > 0)
  291. {
  292. /* The parent tar is still here! Just clean up. */
  293. archive = parent_pipe[PWRITE];
  294. xclose (parent_pipe[PREAD]);
  295. return child_pid;
  296. }
  297. /* The new born child tar is here! */
  298. program_name = _("tar (child)");
  299. xdup2 (parent_pipe[PREAD], STDIN_FILENO);
  300. xclose (parent_pipe[PWRITE]);
  301. /* Check if we need a grandchild tar. This happens only if either:
  302. a) we are writing stdout: to force reblocking;
  303. b) the file is to be accessed by rmt: compressor doesn't know how;
  304. c) the file is not a plain file. */
  305. if (strcmp (archive_name_array[0], "-") != 0
  306. && !_remdev (archive_name_array[0])
  307. && is_regular_file (archive_name_array[0]))
  308. {
  309. if (backup_option)
  310. maybe_backup_file (archive_name_array[0], 1);
  311. /* We don't need a grandchild tar. Open the archive and launch the
  312. compressor. */
  313. archive = creat (archive_name_array[0], MODE_RW);
  314. if (archive < 0)
  315. {
  316. int saved_errno = errno;
  317. if (backup_option)
  318. undo_last_backup ();
  319. errno = saved_errno;
  320. open_fatal (archive_name_array[0]);
  321. }
  322. xdup2 (archive, STDOUT_FILENO);
  323. execlp (use_compress_program_option, use_compress_program_option,
  324. (char *) 0);
  325. exec_fatal (use_compress_program_option);
  326. }
  327. /* We do need a grandchild tar. */
  328. xpipe (child_pipe);
  329. grandchild_pid = xfork ();
  330. if (grandchild_pid == 0)
  331. {
  332. /* The newborn grandchild tar is here! Launch the compressor. */
  333. program_name = _("tar (grandchild)");
  334. xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
  335. xclose (child_pipe[PREAD]);
  336. execlp (use_compress_program_option, use_compress_program_option,
  337. (char *) 0);
  338. exec_fatal (use_compress_program_option);
  339. }
  340. /* The child tar is still here! */
  341. /* Prepare for reblocking the data from the compressor into the archive. */
  342. xdup2 (child_pipe[PREAD], STDIN_FILENO);
  343. xclose (child_pipe[PWRITE]);
  344. if (strcmp (archive_name_array[0], "-") == 0)
  345. archive = STDOUT_FILENO;
  346. else
  347. {
  348. archive = rmtcreat (archive_name_array[0], MODE_RW, rsh_command_option);
  349. if (archive < 0)
  350. open_fatal (archive_name_array[0]);
  351. }
  352. /* Let's read out of the stdin pipe and write an archive. */
  353. while (1)
  354. {
  355. size_t status = 0;
  356. char *cursor;
  357. size_t length;
  358. /* Assemble a record. */
  359. for (length = 0, cursor = record_start->buffer;
  360. length < record_size;
  361. length += status, cursor += status)
  362. {
  363. size_t size = record_size - length;
  364. status = safe_read (STDIN_FILENO, cursor, size);
  365. if (status == SAFE_READ_ERROR)
  366. read_fatal (use_compress_program_option);
  367. if (status == 0)
  368. break;
  369. }
  370. /* Copy the record. */
  371. if (status == 0)
  372. {
  373. /* We hit the end of the file. Write last record at
  374. full length, as the only role of the grandchild is
  375. doing proper reblocking. */
  376. if (length > 0)
  377. {
  378. memset (record_start->buffer + length, 0, record_size - length);
  379. status = sys_write_archive_buffer ();
  380. if (status != record_size)
  381. archive_write_error (status);
  382. }
  383. /* There is nothing else to read, break out. */
  384. break;
  385. }
  386. status = sys_write_archive_buffer ();
  387. if (status != record_size)
  388. archive_write_error (status);
  389. }
  390. /* Propagate any failure of the grandchild back to the parent. */
  391. while (waitpid (grandchild_pid, &wait_status, 0) == -1)
  392. if (errno != EINTR)
  393. {
  394. waitpid_error (use_compress_program_option);
  395. break;
  396. }
  397. if (WIFSIGNALED (wait_status))
  398. {
  399. kill (child_pid, WTERMSIG (wait_status));
  400. exit_status = TAREXIT_FAILURE;
  401. }
  402. else if (WEXITSTATUS (wait_status) != 0)
  403. exit_status = WEXITSTATUS (wait_status);
  404. exit (exit_status);
  405. }
  406. /* Set ARCHIVE for uncompressing, then reading an archive. */
  407. pid_t
  408. sys_child_open_for_uncompress (void)
  409. {
  410. int parent_pipe[2];
  411. int child_pipe[2];
  412. pid_t grandchild_pid;
  413. pid_t child_pid;
  414. int wait_status;
  415. xpipe (parent_pipe);
  416. child_pid = xfork ();
  417. if (child_pid > 0)
  418. {
  419. /* The parent tar is still here! Just clean up. */
  420. archive = parent_pipe[PREAD];
  421. xclose (parent_pipe[PWRITE]);
  422. return child_pid;
  423. }
  424. /* The newborn child tar is here! */
  425. program_name = _("tar (child)");
  426. xdup2 (parent_pipe[PWRITE], STDOUT_FILENO);
  427. xclose (parent_pipe[PREAD]);
  428. /* Check if we need a grandchild tar. This happens only if either:
  429. a) we're reading stdin: to force unblocking;
  430. b) the file is to be accessed by rmt: compressor doesn't know how;
  431. c) the file is not a plain file. */
  432. if (strcmp (archive_name_array[0], "-") != 0
  433. && !_remdev (archive_name_array[0])
  434. && is_regular_file (archive_name_array[0]))
  435. {
  436. /* We don't need a grandchild tar. Open the archive and lauch the
  437. uncompressor. */
  438. archive = open (archive_name_array[0], O_RDONLY | O_BINARY, MODE_RW);
  439. if (archive < 0)
  440. open_fatal (archive_name_array[0]);
  441. xdup2 (archive, STDIN_FILENO);
  442. execlp (use_compress_program_option, use_compress_program_option,
  443. "-d", (char *) 0);
  444. exec_fatal (use_compress_program_option);
  445. }
  446. /* We do need a grandchild tar. */
  447. xpipe (child_pipe);
  448. grandchild_pid = xfork ();
  449. if (grandchild_pid == 0)
  450. {
  451. /* The newborn grandchild tar is here! Launch the uncompressor. */
  452. program_name = _("tar (grandchild)");
  453. xdup2 (child_pipe[PREAD], STDIN_FILENO);
  454. xclose (child_pipe[PWRITE]);
  455. execlp (use_compress_program_option, use_compress_program_option,
  456. "-d", (char *) 0);
  457. exec_fatal (use_compress_program_option);
  458. }
  459. /* The child tar is still here! */
  460. /* Prepare for unblocking the data from the archive into the
  461. uncompressor. */
  462. xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
  463. xclose (child_pipe[PREAD]);
  464. if (strcmp (archive_name_array[0], "-") == 0)
  465. archive = STDIN_FILENO;
  466. else
  467. archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
  468. MODE_RW, rsh_command_option);
  469. if (archive < 0)
  470. open_fatal (archive_name_array[0]);
  471. /* Let's read the archive and pipe it into stdout. */
  472. while (1)
  473. {
  474. char *cursor;
  475. size_t maximum;
  476. size_t count;
  477. size_t status;
  478. clear_read_error_count ();
  479. error_loop:
  480. status = rmtread (archive, record_start->buffer, record_size);
  481. if (status == SAFE_READ_ERROR)
  482. {
  483. archive_read_error ();
  484. goto error_loop;
  485. }
  486. if (status == 0)
  487. break;
  488. cursor = record_start->buffer;
  489. maximum = status;
  490. while (maximum)
  491. {
  492. count = maximum < BLOCKSIZE ? maximum : BLOCKSIZE;
  493. if (full_write (STDOUT_FILENO, cursor, count) != count)
  494. write_error (use_compress_program_option);
  495. cursor += count;
  496. maximum -= count;
  497. }
  498. }
  499. xclose (STDOUT_FILENO);
  500. /* Propagate any failure of the grandchild back to the parent. */
  501. while (waitpid (grandchild_pid, &wait_status, 0) == -1)
  502. if (errno != EINTR)
  503. {
  504. waitpid_error (use_compress_program_option);
  505. break;
  506. }
  507. if (WIFSIGNALED (wait_status))
  508. {
  509. kill (child_pid, WTERMSIG (wait_status));
  510. exit_status = TAREXIT_FAILURE;
  511. }
  512. else if (WEXITSTATUS (wait_status) != 0)
  513. exit_status = WEXITSTATUS (wait_status);
  514. exit (exit_status);
  515. }
  516. static void
  517. dec_to_env (char *envar, uintmax_t num)
  518. {
  519. char buf[UINTMAX_STRSIZE_BOUND];
  520. char *numstr;
  521. numstr = STRINGIFY_BIGINT (num, buf);
  522. setenv (envar, numstr, 1);
  523. }
  524. static void
  525. oct_to_env (char *envar, unsigned long num)
  526. {
  527. char buf[1+1+(sizeof(unsigned long)*CHAR_BIT+2)/3];
  528. snprintf (buf, sizeof buf, "0%lo", num);
  529. setenv (envar, buf, 1);
  530. }
  531. static void
  532. str_to_env (char *envar, char const *str)
  533. {
  534. if (str)
  535. setenv (envar, str, 1);
  536. else
  537. unsetenv (envar);
  538. }
  539. static void
  540. chr_to_env (char *envar, char c)
  541. {
  542. char buf[2];
  543. buf[0] = c;
  544. buf[1] = 0;
  545. setenv (envar, buf, 1);
  546. }
  547. static void
  548. stat_to_env (char *name, char type, struct tar_stat_info *st)
  549. {
  550. chr_to_env ("TAR_FILETYPE", type);
  551. oct_to_env ("TAR_MODE", st->stat.st_mode);
  552. str_to_env ("TAR_FILENAME", name);
  553. str_to_env ("TAR_REALNAME", st->file_name);
  554. str_to_env ("TAR_UNAME", st->uname);
  555. str_to_env ("TAR_GNAME", st->gname);
  556. dec_to_env ("TAR_MTIME", st->stat.st_mtime);
  557. dec_to_env ("TAR_ATIME", st->stat.st_atime);
  558. dec_to_env ("TAR_CTIME", st->stat.st_ctime);
  559. dec_to_env ("TAR_SIZE", st->stat.st_size);
  560. dec_to_env ("TAR_UID", st->stat.st_uid);
  561. dec_to_env ("TAR_GID", st->stat.st_gid);
  562. switch (type)
  563. {
  564. case 'b':
  565. case 'c':
  566. dec_to_env ("TAR_MINOR", minor (st->stat.st_rdev));
  567. dec_to_env ("TAR_MAJOR", major (st->stat.st_rdev));
  568. unsetenv ("TAR_LINKNAME");
  569. break;
  570. case 'l':
  571. case 'h':
  572. unsetenv ("TAR_MINOR");
  573. unsetenv ("TAR_MAJOR");
  574. str_to_env ("TAR_LINKNAME", st->link_name);
  575. break;
  576. default:
  577. unsetenv ("TAR_MINOR");
  578. unsetenv ("TAR_MAJOR");
  579. unsetenv ("TAR_LINKNAME");
  580. break;
  581. }
  582. }
  583. static pid_t pid;
  584. static RETSIGTYPE (*pipe_handler) (int sig);
  585. int
  586. sys_exec_command (char *file_name, int typechar, struct tar_stat_info *st)
  587. {
  588. int p[2];
  589. char *argv[4];
  590. xpipe (p);
  591. pipe_handler = signal (SIGPIPE, SIG_IGN);
  592. pid = xfork ();
  593. if (pid != 0)
  594. {
  595. xclose (p[PREAD]);
  596. return p[PWRITE];
  597. }
  598. /* Child */
  599. xdup2 (p[PREAD], STDIN_FILENO);
  600. xclose (p[PWRITE]);
  601. stat_to_env (file_name, typechar, st);
  602. argv[0] = "/bin/sh";
  603. argv[1] = "-c";
  604. argv[2] = to_command_option;
  605. argv[3] = NULL;
  606. execv ("/bin/sh", argv);
  607. exec_fatal (file_name);
  608. }
  609. void
  610. sys_wait_command (void)
  611. {
  612. int status;
  613. if (pid < 0)
  614. return;
  615. signal (SIGPIPE, pipe_handler);
  616. while (waitpid (pid, &status, 0) == -1)
  617. if (errno != EINTR)
  618. {
  619. pid = -1;
  620. waitpid_error (to_command_option);
  621. return;
  622. }
  623. if (WIFEXITED (status))
  624. {
  625. if (!ignore_command_error_option && WEXITSTATUS (status))
  626. ERROR ((0, 0, _("%lu: Child returned status %d"),
  627. (unsigned long) pid, WEXITSTATUS (status)));
  628. }
  629. else if (WIFSIGNALED (status))
  630. {
  631. WARN ((0, 0, _("%lu: Child terminated on signal %d"),
  632. (unsigned long) pid, WTERMSIG (status)));
  633. }
  634. else
  635. ERROR ((0, 0, _("%lu: Child terminated on unknown reason"),
  636. (unsigned long) pid));
  637. pid = -1;
  638. }
  639. #endif /* not MSDOS */