releasing package moreutils version 0.68
[moreutils.git] / ifne.c
blobff648cc55865f5bf9af76b9622b52b08a1b489fb
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 #include <signal.h>
28 #define streq(a, b) (!strcmp((a), (b)))
30 static void stdin_to_stream(char *buf, ssize_t r, FILE *outf) {
31 while (r > 0) {
32 if (fwrite(buf, r*sizeof(char), 1, outf) < 1) {
33 fprintf(stderr, "Write error\n");
34 exit(EXIT_FAILURE);
36 r = read(0, buf, BUFSIZ*sizeof(char));
38 if (r == -1) {
39 perror("read");
40 exit(EXIT_FAILURE);
44 int main(int argc, char **argv) {
45 ssize_t r;
46 int run_if_empty;
47 char **argv_exec;
48 int fds[2];
49 int child_status;
50 pid_t child_pid;
51 char buf[BUFSIZ];
52 FILE *outf;
54 if ((argc < 2) || ((argc == 2) && streq(argv[1], "-n"))) {
55 fprintf(stderr, "Usage: ifne [-n] command [args]\n");
56 return EXIT_FAILURE;
59 if (streq(argv[1], "-n")) {
60 run_if_empty = 1;
61 argv_exec = &argv[2];
62 } else {
63 run_if_empty = 0;
64 argv_exec = &argv[1];
67 r = read(0, buf, BUFSIZ*sizeof(char));
69 if ((r == 0) && !run_if_empty)
70 return EXIT_SUCCESS;
71 else if (r == -1) {
72 perror("read");
73 return EXIT_FAILURE;
76 if (pipe(fds)) {
77 perror("pipe");
78 return EXIT_FAILURE;
81 if (r && run_if_empty) {
82 /* don't run the subcommand if we read something from stdin and -n was set */
83 /* But write stdin to stdout so ifne -n can be piped without sucking the stream */
84 stdin_to_stream(buf, r, stdout);
85 return EXIT_SUCCESS;
88 child_pid = fork();
89 if (!child_pid) {
90 /* child process: rebind stdin and exec the subcommand */
91 close(fds[1]);
92 if (dup2(fds[0], 0)) {
93 perror("dup2");
94 return EXIT_FAILURE;
97 execvp(*argv_exec, argv_exec);
98 perror(*argv_exec);
99 close(fds[0]);
100 return EXIT_FAILURE;
101 } else if (child_pid == -1) {
102 perror("fork");
103 return EXIT_FAILURE;
106 /* Parent: write stdin to fds[1] */
107 close(fds[0]);
108 outf = fdopen(fds[1], "w");
109 if (! outf) {
110 perror("fdopen");
111 exit(1);
114 stdin_to_stream(buf, r, outf);
115 fclose(outf);
117 if (waitpid(child_pid, &child_status, 0) != child_pid) {
118 perror("waitpid");
119 return EXIT_FAILURE;
121 if (WIFEXITED(child_status)) {
122 return (WEXITSTATUS(child_status));
123 } else if (WIFSIGNALED(child_status)) {
124 raise(WTERMSIG(child_status));
125 return EXIT_FAILURE;
128 return EXIT_FAILURE;