Fixed time command segfault with no arguments
[busybox4maemo] / archival / libunarchive / open_transformer.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4  */
5
6 #include "libbb.h"
7 #include "unarchive.h"
8
9 /* transformer(), more than meets the eye */
10 /*
11  * On MMU machine, the transform_prog is removed by macro magic
12  * in include/unarchive.h. On NOMMU, transformer is removed.
13  */
14 int open_transformer(int src_fd,
15         USE_DESKTOP(long long) int (*transformer)(int src_fd, int dst_fd),
16         const char *transform_prog)
17 {
18         struct fd_pair fd_pipe;
19         int pid;
20
21         xpiped_pair(fd_pipe);
22
23 #if BB_MMU
24         pid = fork();
25 #else
26         pid = vfork();
27 #endif
28         if (pid == -1)
29                 bb_perror_msg_and_die("fork failed");
30
31         if (pid == 0) {
32                 /* child process */
33                 close(fd_pipe.rd); /* We don't want to read from the parent */
34                 // FIXME: error check?
35 #if BB_MMU
36                 transformer(src_fd, fd_pipe.wr);
37                 if (ENABLE_FEATURE_CLEAN_UP) {
38                         close(fd_pipe.wr); /* Send EOF */
39                         close(src_fd);
40                 }
41                 exit(0);
42 #else
43                 {
44                         char *argv[4];
45                         xmove_fd(src_fd, 0);
46                         xmove_fd(fd_pipe.wr, 1);
47                         argv[0] = (char*)transform_prog;
48                         argv[1] = (char*)"-cf";
49                         argv[2] = (char*)"-";
50                         argv[3] = NULL;
51                         BB_EXECVP(transform_prog, argv);
52                         bb_perror_msg_and_die("exec failed");
53                 }
54 #endif
55                 /* notreached */
56         }
57
58         /* parent process */
59         close(fd_pipe.wr); /* Don't want to write to the child */
60
61         return fd_pipe.rd;
62 }