prep release
[moreutils.git] / ifne.c
blobe8bc10048f75efccf7db285eef3db76cc73417ba
1 /*
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.
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.
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
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 #ifdef __sun
27 #include <signal.h> /* raise() */
28 #endif
30 #define streq(a, b) (!strcmp((a), (b)))
32 static void stdin_to_stream(char *buf, ssize_t r, FILE *outf) {
33 while (r > 0) {
34 if (fwrite(buf, r*sizeof(char), 1, outf) < 1) {
35 fprintf(stderr, "Write error\n");
36 exit(EXIT_FAILURE);
38 r = read(0, buf, BUFSIZ*sizeof(char));
40 if (r == -1) {
41 perror("read");
42 exit(EXIT_FAILURE);
46 int main(int argc, char **argv) {
47 ssize_t r;
48 int run_if_empty;
49 char **argv_exec;
50 int fds[2];
51 int child_status;
52 pid_t child_pid;
53 char buf[BUFSIZ];
54 FILE *outf;
56 if ((argc < 2) || ((argc == 2) && streq(argv[1], "-n"))) {
57 fprintf(stderr, "Usage: ifne [-n] command [args]\n");
58 return EXIT_FAILURE;
61 if (streq(argv[1], "-n")) {
62 run_if_empty = 1;
63 argv_exec = &argv[2];
64 } else {
65 run_if_empty = 0;
66 argv_exec = &argv[1];
69 r = read(0, buf, BUFSIZ*sizeof(char));
71 if ((r == 0) && !run_if_empty)
72 return EXIT_SUCCESS;
73 else if (r == -1) {
74 perror("read");
75 return EXIT_FAILURE;
78 if (pipe(fds)) {
79 perror("pipe");
80 return EXIT_FAILURE;
83 if (r && run_if_empty) {
84 /* don't run the subcommand if we read something from stdin and -n was set */
85 /* But write stdin to stdout so ifne -n can be piped without sucking the stream */
86 stdin_to_stream(buf, r, stdout);
87 return EXIT_SUCCESS;
90 child_pid = fork();
91 if (!child_pid) {
92 /* child process: rebind stdin and exec the subcommand */
93 close(fds[1]);
94 if (dup2(fds[0], 0)) {
95 perror("dup2");
96 return EXIT_FAILURE;
99 execvp(*argv_exec, argv_exec);
100 perror(*argv_exec);
101 close(fds[0]);
102 return EXIT_FAILURE;
103 } else if (child_pid == -1) {
104 perror("fork");
105 return EXIT_FAILURE;
108 /* Parent: write stdin to fds[1] */
109 close(fds[0]);
110 outf = fdopen(fds[1], "w");
111 if (! outf) {
112 perror("fdopen");
113 exit(1);
116 stdin_to_stream(buf, r, outf);
117 fclose(outf);
119 if (waitpid(child_pid, &child_status, 0) != child_pid) {
120 perror("waitpid");
121 return EXIT_FAILURE;
123 if (WIFEXITED(child_status)) {
124 return (WEXITSTATUS(child_status));
125 } else if (WIFSIGNALED(child_status)) {
126 raise(WTERMSIG(child_status));
127 return EXIT_FAILURE;
130 return EXIT_FAILURE;