]> err.no Git - moreutils/blob - ifne.c
Update name one more place
[moreutils] / ifne.c
1 /* 
2  *
3  * Copyright 2008 Javier Merino <cibervicho@gmail.com>
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12  * Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <sys/wait.h>
24 #include <sys/types.h>
25 #include <string.h>
26 #define streq(a, b) (!strcmp((a), (b)))
27
28 static void stdin_to_stream(char *buf, ssize_t r, FILE *outf) {
29         while (r > 0) {
30                 if (fwrite(buf, r*sizeof(char), 1, outf) < 1) {
31                         fprintf(stderr, "Write error\n");
32                         exit(EXIT_FAILURE);
33                 }
34                 r = read(0, buf, BUFSIZ*sizeof(char));
35         }
36         if (r == -1) {
37                 perror("read");
38                 exit(EXIT_FAILURE);
39         }
40 }
41
42 int main(int argc, char **argv) {
43         ssize_t r;
44         int run_if_empty;
45         char **argv_exec;
46         int fds[2];
47         int child_status;
48         pid_t child_pid;
49         char buf[BUFSIZ];
50         FILE *outf;
51
52         if ((argc < 2) || ((argc == 2) && streq(argv[1], "-n"))) {
53                 fprintf(stderr, "Usage: ifne [-n] command [args]\n");
54                 return EXIT_FAILURE;
55         }
56
57         if (streq(argv[1], "-n")) {
58                 run_if_empty = 1;
59                 argv_exec = &argv[2];
60         } else {
61                 run_if_empty = 0;
62                 argv_exec = &argv[1];
63         }
64
65         r = read(0, buf, BUFSIZ*sizeof(char));
66
67         if ((r == 0) && !run_if_empty)
68                 return EXIT_SUCCESS;
69         else if (r == -1) {
70                 perror("read");
71                 return EXIT_FAILURE;
72         }
73
74         if (pipe(fds)) {
75                 perror("pipe");
76                 return EXIT_FAILURE;
77         }
78
79         if (r && run_if_empty) {
80                 /* don't run the subcommand if we read something from stdin and -n was set */
81                 /* But write stdin to stdout so ifne -n can be piped without sucking the stream */
82                 stdin_to_stream(buf, r, stdout);
83                 return EXIT_SUCCESS;
84         }
85
86         child_pid = fork();
87         if (!child_pid) {
88                 /* child process: rebind stdin and exec the subcommand */
89                 close(fds[1]);
90                 if (dup2(fds[0], 0)) {
91                         perror("dup2");
92                         return EXIT_FAILURE;
93                 }
94
95                 execvp(*argv_exec, argv_exec);
96                 perror(*argv_exec);
97                 close(fds[0]);
98                 return EXIT_FAILURE;
99         } else if (child_pid == -1) {
100                 perror("fork");
101                 return EXIT_FAILURE;
102         }
103
104         /* Parent: write stdin to fds[1] */
105         close(fds[0]);
106         outf = fdopen(fds[1], "w");
107         if (! outf) {
108                 perror("fdopen");
109                 exit(1);
110         }
111
112         stdin_to_stream(buf, r, outf);
113         fclose(outf);
114
115         if (waitpid(child_pid, &child_status, 0) != child_pid) {
116                 perror("waitpid");
117                 return EXIT_FAILURE;
118         }
119         if (WIFEXITED(child_status)) {
120                 return (WEXITSTATUS(child_status));
121         } else if (WIFSIGNALED(child_status)) {
122                 raise(WTERMSIG(child_status));
123                 return EXIT_FAILURE;
124         }
125
126         return EXIT_FAILURE;
127 }