File ifne.c changed (mode: 100644) (index 817b1c5..a7abd21) |
22 |
22 |
#include <unistd.h> |
#include <unistd.h> |
23 |
23 |
#include <sys/wait.h> |
#include <sys/wait.h> |
24 |
24 |
#include <sys/types.h> |
#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 |
|
} |
25 |
41 |
|
|
26 |
42 |
int main(int argc, char **argv) { |
int main(int argc, char **argv) { |
27 |
43 |
ssize_t r; |
ssize_t r; |
|
44 |
|
int run_if_empty; |
|
45 |
|
char **argv_exec; |
28 |
46 |
int fds[2]; |
int fds[2]; |
29 |
47 |
int child_status; |
int child_status; |
30 |
48 |
pid_t child_pid; |
pid_t child_pid; |
31 |
49 |
char buf[BUFSIZ]; |
char buf[BUFSIZ]; |
32 |
50 |
FILE *outf; |
FILE *outf; |
33 |
51 |
|
|
34 |
|
if (argc < 2) { |
|
|
52 |
|
if ((argc < 2) || ((argc == 2) && streq(argv[1], "-n"))) { |
35 |
53 |
/* Noop */ |
/* Noop */ |
36 |
54 |
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
37 |
55 |
} |
} |
38 |
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 |
|
|
39 |
65 |
r = read(0, buf, BUFSIZ*sizeof(char)); |
r = read(0, buf, BUFSIZ*sizeof(char)); |
40 |
66 |
|
|
41 |
|
if (r == 0) |
|
|
67 |
|
if ((r == 0) && !run_if_empty) |
42 |
68 |
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
43 |
69 |
else if (r == -1) { |
else if (r == -1) { |
44 |
70 |
perror("read"); |
perror("read"); |
|
... |
... |
int main(int argc, char **argv) { |
50 |
76 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
51 |
77 |
} |
} |
52 |
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 |
|
|
53 |
86 |
child_pid = fork(); |
child_pid = fork(); |
54 |
87 |
if (!child_pid) { |
if (!child_pid) { |
55 |
88 |
/* child process: rebind stdin and exec the subcommand */ |
/* child process: rebind stdin and exec the subcommand */ |
|
... |
... |
int main(int argc, char **argv) { |
57 |
90 |
if (dup2(fds[0], 0)) { |
if (dup2(fds[0], 0)) { |
58 |
91 |
perror("dup2"); |
perror("dup2"); |
59 |
92 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
60 |
|
} |
|
|
93 |
|
} |
61 |
94 |
|
|
62 |
|
execvp(argv[1], &argv[1]); |
|
63 |
|
perror(argv[1]); |
|
|
95 |
|
execvp(*argv_exec, argv_exec); |
|
96 |
|
perror(*argv_exec); |
64 |
97 |
close(fds[0]); |
close(fds[0]); |
65 |
98 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |
66 |
99 |
} else if (child_pid == -1) { |
} else if (child_pid == -1) { |
|
... |
... |
int main(int argc, char **argv) { |
75 |
108 |
perror("fdopen"); |
perror("fdopen"); |
76 |
109 |
exit(1); |
exit(1); |
77 |
110 |
} |
} |
78 |
|
do { |
|
79 |
|
if (fwrite(buf, r*sizeof(char), 1, outf) < 1) { |
|
80 |
|
fprintf(stderr, "Write error to %s\n", argv[1]); |
|
81 |
|
exit(EXIT_FAILURE); |
|
82 |
|
} |
|
83 |
|
r = read(0, buf, BUFSIZ*sizeof(char)); |
|
84 |
|
} while (r > 0); |
|
85 |
|
if (r == -1) { |
|
86 |
|
perror("read"); |
|
87 |
|
exit(EXIT_FAILURE); |
|
88 |
|
} |
|
|
111 |
|
|
|
112 |
|
stdin_to_stream(buf, r, outf); |
89 |
113 |
fclose(outf); |
fclose(outf); |
90 |
114 |
|
|
91 |
115 |
if (waitpid(child_pid, &child_status, 0) != child_pid) { |
if (waitpid(child_pid, &child_status, 0) != child_pid) { |
File ifne.docbook changed (mode: 100644) (index 92c7f48..38e3889) |
... |
... |
with this program; if not, write to the Free Software Foundation, Inc., |
33 |
33 |
<firstname>Javier</firstname> |
<firstname>Javier</firstname> |
34 |
34 |
<surname>Merino</surname> |
<surname>Merino</surname> |
35 |
35 |
</author> |
</author> |
36 |
|
<date>2008-03-19</date> |
|
|
36 |
|
<date>2008-05-01</date> |
37 |
37 |
</refentryinfo> |
</refentryinfo> |
38 |
38 |
|
|
39 |
39 |
<refmeta> |
<refmeta> |
|
... |
... |
with this program; if not, write to the Free Software Foundation, Inc., |
48 |
48 |
|
|
49 |
49 |
<refsynopsisdiv> |
<refsynopsisdiv> |
50 |
50 |
<cmdsynopsis> |
<cmdsynopsis> |
51 |
|
<command>ifne command</command> |
|
|
51 |
|
<command>ifne [-n] command</command> |
52 |
52 |
</cmdsynopsis> |
</cmdsynopsis> |
53 |
53 |
</refsynopsisdiv> |
</refsynopsisdiv> |
54 |
54 |
|
|
55 |
55 |
<refsect1> |
<refsect1> |
56 |
56 |
<title>DESCRIPTION</title> |
<title>DESCRIPTION</title> |
57 |
57 |
|
|
58 |
|
<para><command>ifne</command> runs the following command if and only if |
|
59 |
|
the standard input is not empty.</para> |
|
|
58 |
|
<para><command>ifne</command> runs the following command if and only if |
|
59 |
|
the standard input is not empty.</para> |
60 |
60 |
</refsect1> |
</refsect1> |
61 |
61 |
|
|
|
62 |
|
<refsect1> |
|
63 |
|
<title>OPTIONS</title> |
|
64 |
|
|
|
65 |
|
<variablelist> |
|
66 |
|
<varlistentry> |
|
67 |
|
<term><option>-n</option></term> |
|
68 |
|
<listitem> |
|
69 |
|
<para>Reverse operation. Run the command if the standard input is empty.</para> |
|
70 |
|
</listitem> |
|
71 |
|
</varlistentry> |
|
72 |
|
</variablelist> |
|
73 |
|
</refsect1> |
|
74 |
|
|
62 |
75 |
<refsect1> |
<refsect1> |
63 |
76 |
<title>EXAMPLE</title> |
<title>EXAMPLE</title> |
64 |
77 |
<cmdsynopsis> |
<cmdsynopsis> |
65 |
78 |
<command>find . -name core | ifne mail -s "Core files found" root</command> |
<command>find . -name core | ifne mail -s "Core files found" root</command> |
66 |
79 |
</cmdsynopsis> |
</cmdsynopsis> |
67 |
80 |
</refsect1> |
</refsect1> |
|
81 |
|
|
|
82 |
|
<refsect1> |
|
83 |
|
<title>AUTHOR</title> |
|
84 |
|
|
|
85 |
|
<para>Copyright 2008 by Javier Merino <cibervicho@gmail.com></para> |
|
86 |
|
<para>Licensed under the GNU GPL</para> |
|
87 |
|
</refsect1> |
|
88 |
|
|
68 |
89 |
</refentry> |
</refentry> |