FreeCalypso > hg > fc-rfcal-tools
annotate scripts/fc-rfcal-tee.c @ 133:c99b1dce04ec default tip
fc-rfcal-txcheck: check and report ramp tolerance
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 20 Dec 2021 04:22:19 +0000 |
parents | 713749548df6 |
children |
rev | line source |
---|---|
90
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This C program is a workaround for a stupidity in Bourne shell. |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * I would like to have a shell script like the following: |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 * |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 * set -e |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 * fc-rfcal-vcxo | tee vcxo |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 * fc-rfcal-rxband 900 | tee rx-900 |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 * fc-rfcal-rxband 1800 | tee rx-1800 |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 * ... |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 * |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 * and if one of the fc-rfcal-* steps encounters an error and returns |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 * a non-zero exit code, I want the script to stop right there, hence |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 * the set -e at the beginning. Sounds like a very simple and reasonable |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 * desire, doesn't it? But oh noes - because I am piping the output |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 * through tee in order to save it in log files, the process return |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 * codes from fc-rfcal-* get *ignored* by the shell (the always successful |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 * exit code from tee is all that counts), and it will keep executing |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 * the following lines without stopping. |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 * |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 * This C program is like tee, but with invokation of the "main" command |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 * whose output is to be saved built in. The process forks, the "main" |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 * command is executed in the child, the parent performs the tee function, |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 * and when the child terminates, the parent propagates its exit code. |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 */ |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 #include <sys/types.h> |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 #include <sys/file.h> |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 #include <sys/wait.h> |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 #include <stdio.h> |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 #include <stdlib.h> |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 #include <unistd.h> |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 char shell_pathname[] = "/bin/sh"; |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 int log_fd; |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 tee_process(pipe_fd) |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 { |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 char buf[1024]; |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 int cc; |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 while ((cc = read(pipe_fd, buf, sizeof buf)) > 0) { |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 write(1, buf, cc); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 if (log_fd > 2) |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 write(log_fd, buf, cc); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 } |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 } |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 main(argc, argv) |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 char **argv; |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 { |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 int p[2]; |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 pid_t child, waitres; |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 int status; |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 if (argc != 3) { |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 fprintf(stderr, "usage: %s command logfile\n", argv[0]); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 exit(1); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 } |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 if (pipe(p) < 0) { |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 perror("pipe"); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 exit(1); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 } |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 child = fork(); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 if (child < 0) { |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 perror("fork"); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 exit(1); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 } |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 if (!child) { |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 dup2(p[1], 1); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 close(p[0]); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 close(p[1]); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 execl(shell_pathname, "sh", "-c", argv[1], 0); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 perror(shell_pathname); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 exit(1); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 } |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 close(p[1]); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 log_fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0666); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 if (log_fd < 0) |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 perror(argv[2]); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 tee_process(p[0]); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 waitres = waitpid(child, &status, 0); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 if (waitres == child && WIFEXITED(status)) |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 exit(WEXITSTATUS(status)); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 else |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 exit(1); |
713749548df6
fc-rfcal-tri900 script and fc-rfcal-tee helper implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 } |