9 #define NULL ((void*) 0)
12 static void replacefd(int fd
, const char *filename
, int flags
, int mode
) {
14 n
= open(filename
, flags
, mode
);
24 int main(int argc
, char **argv
) {
25 int do_fork
= 1, command_start
, i
;
26 char *infile
= NULL
, *outfile
= NULL
, *errfile
= NULL
, *pidfile
= NULL
;
29 /* Parse command line */
30 for(i
= 1; i
< argc
; i
++) {
31 if(!strcmp(argv
[i
], "-e")) errfile
= argv
[++i
];
32 else if(!strcmp(argv
[i
], "-f")) do_fork
= 0;
33 else if(!strcmp(argv
[i
], "-i")) infile
= argv
[++i
];
34 else if(!strcmp(argv
[i
], "-o")) outfile
= argv
[++i
];
35 else if(!strcmp(argv
[i
], "-p")) pidfile
= argv
[++i
];
36 else if(!strcmp(argv
[i
], "--")) {
39 } else if(argv
[i
][0] == '-') {
40 fprintf(stderr
, "Invalid option: %s\n", argv
[i
]);
46 if(do_fork
&& fork()) return 0;
49 pidfh
= fopen(pidfile
, "w");
54 fprintf(pidfh
, "%d\n", getpid());
58 if(infile
) replacefd(0, infile
, O_RDONLY
, 0666);
60 if(outfile
) replacefd(1, outfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
62 if(errfile
) replacefd(2, errfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
66 execvp(argv
[command_start
], &argv
[command_start
]);
68 perror(argv
[command_start
]);