| @@ -1,41 +1,41 @@ | | | @@ -1,41 +1,41 @@ |
1 | /* $NetBSD: h_intr.c,v 1.1 2021/07/08 09:07:46 christos Exp $ */ | | 1 | /* $NetBSD: h_intr.c,v 1.2 2021/07/08 15:21:40 christos Exp $ */ |
2 | | | 2 | |
3 | /** | | 3 | /** |
4 | * Test of interrupted writes to popen()'ed commands. | | 4 | * Test of interrupted writes to popen()'ed commands. |
5 | * | | 5 | * |
6 | * Example 1: | | 6 | * Example 1: |
7 | * ./h_fwrite -c "gzip -t" *.gz | | 7 | * ./h_fwrite -c "gzip -t" *.gz |
8 | * | | 8 | * |
9 | * Example 2: | | 9 | * Example 2: |
10 | * while :; do ./h_fwrite -b $((12*1024)) -t 10 -c "bzip2 -t" *.bz2; sleep 2; done | | 10 | * while :; do ./h_fwrite -b $((12*1024)) -t 10 -c "bzip2 -t" *.bz2; sleep 2; done |
11 | * | | 11 | * |
12 | * Example 3: | | 12 | * Example 3: |
13 | * Create checksum file: | | 13 | * Create checksum file: |
14 | * find /mnt -type f -exec sha512 -n {} + >SHA512 | | 14 | * find /mnt -type f -exec sha512 -n {} + >SHA512 |
15 | * | | 15 | * |
16 | * Check program: | | 16 | * Check program: |
17 | * find /mnt -type f -exec ./h_fwrite -b 512 -c run.sh {} + | | 17 | * find /mnt -type f -exec ./h_fwrite -b 512 -c run.sh {} + |
18 | * | | 18 | * |
19 | * ./run.sh: | | 19 | * ./run.sh: |
20 | #!/bin/sh | | 20 | #!/bin/sh |
21 | set -eu | | 21 | set -eu |
22 | grep -q "^$(sha512 -q)" SHA512 | | 22 | grep -q "^$(sha512 -q)" SHA512 |
23 | * | | 23 | * |
24 | * Author: RVP at sdf.org | | 24 | * Author: RVP at sdf.org |
25 | */ | | 25 | */ |
26 | | | 26 | |
27 | #include <sys/cdefs.h> | | 27 | #include <sys/cdefs.h> |
28 | __RCSID("$NetBSD: h_intr.c,v 1.1 2021/07/08 09:07:46 christos Exp $"); | | 28 | __RCSID("$NetBSD: h_intr.c,v 1.2 2021/07/08 15:21:40 christos Exp $"); |
29 | | | 29 | |
30 | #include <time.h> | | 30 | #include <time.h> |
31 | #include <err.h> | | 31 | #include <err.h> |
32 | #include <errno.h> | | 32 | #include <errno.h> |
33 | #include <stdbool.h> | | 33 | #include <stdbool.h> |
34 | #include <libgen.h> | | 34 | #include <libgen.h> |
35 | #include <signal.h> | | 35 | #include <signal.h> |
36 | #include <stdio.h> | | 36 | #include <stdio.h> |
37 | #include <stdlib.h> | | 37 | #include <stdlib.h> |
38 | #include <string.h> | | 38 | #include <string.h> |
39 | #include <unistd.h> | | 39 | #include <unistd.h> |
40 | | | 40 | |
41 | static int process(const char *fn); | | 41 | static int process(const char *fn); |
| @@ -134,27 +134,27 @@ process(const char *fn) | | | @@ -134,27 +134,27 @@ process(const char *fn) |
134 | setvbuf(ofp, NULL, opts.btype, opts.ssize); | | 134 | setvbuf(ofp, NULL, opts.btype, opts.ssize); |
135 | setvbuf(ifp, NULL, opts.btype, opts.ssize); | | 135 | setvbuf(ifp, NULL, opts.btype, opts.ssize); |
136 | | | 136 | |
137 | alarmtimer(opts.tmout); | | 137 | alarmtimer(opts.tmout); |
138 | while ((n = maxread(ifp, buf, opts.bsize)) > 0) { | | 138 | while ((n = maxread(ifp, buf, opts.bsize)) > 0) { |
139 | ssize_t i; | | 139 | ssize_t i; |
140 | if ((i = maxwrite(ofp, buf, n)) == -1) { | | 140 | if ((i = maxwrite(ofp, buf, n)) == -1) { |
141 | warn("write failed"); | | 141 | warn("write failed"); |
142 | break; | | 142 | break; |
143 | } | | 143 | } |
144 | nw += i; | | 144 | nw += i; |
145 | } | | 145 | } |
146 | alarmtimer(0); | | 146 | alarmtimer(0); |
147 | // printf("%lu\n", nw); | | 147 | // printf("%zu\n", nw); |
148 | | | 148 | |
149 | fclose(ifp); | | 149 | fclose(ifp); |
150 | if (pclose(ofp) != 0) | | 150 | if (pclose(ofp) != 0) |
151 | warn("command failed `%s'", opts.cmd); | | 151 | warn("command failed `%s'", opts.cmd); |
152 | else | | 152 | else |
153 | rc = EXIT_SUCCESS; | | 153 | rc = EXIT_SUCCESS; |
154 | | | 154 | |
155 | return rc; | | 155 | return rc; |
156 | } | | 156 | } |
157 | | | 157 | |
158 | /** | | 158 | /** |
159 | * maxread - syscall version | | 159 | * maxread - syscall version |
160 | */ | | 160 | */ |
| @@ -331,31 +331,31 @@ getbtype(int val) { | | | @@ -331,31 +331,31 @@ getbtype(int val) { |
331 | } | | 331 | } |
332 | | | 332 | |
333 | /** | | 333 | /** |
334 | * Print usage information. | | 334 | * Print usage information. |
335 | */ | | 335 | */ |
336 | static void | | 336 | static void |
337 | usage(FILE* fp) | | 337 | usage(FILE* fp) |
338 | { | | 338 | { |
339 | fprintf(fp, "Usage: %s [-b SIZE] [-h] [-t TMOUT] -c CMD FILE...\n", | | 339 | fprintf(fp, "Usage: %s [-b SIZE] [-h] [-t TMOUT] -c CMD FILE...\n", |
340 | getprogname()); | | 340 | getprogname()); |
341 | fprintf(fp, "%s: Test interrupted writes to popen()ed CMD.\n", | | 341 | fprintf(fp, "%s: Test interrupted writes to popen()ed CMD.\n", |
342 | getprogname()); | | 342 | getprogname()); |
343 | fprintf(fp, "\n"); | | 343 | fprintf(fp, "\n"); |
344 | fprintf(fp, " -b SIZE Buffer size (%lu)\n", opts.bsize); | | 344 | fprintf(fp, " -b SIZE Buffer size (%zu)\n", opts.bsize); |
345 | fprintf(fp, " -c CMD Command to run on each FILE.\n"); | | 345 | fprintf(fp, " -c CMD Command to run on each FILE.\n"); |
346 | fprintf(fp, " -h This message.\n"); | | 346 | fprintf(fp, " -h This message.\n"); |
347 | fprintf(fp, " -p Buffering type %s.\n", getbtype(opts.btype)); | | 347 | fprintf(fp, " -p Buffering type %s.\n", getbtype(opts.btype)); |
348 | fprintf(fp, " -s SIZE stdio buffer size (%lu)\n", opts.ssize); | | 348 | fprintf(fp, " -s SIZE stdio buffer size (%zu)\n", opts.ssize); |
349 | fprintf(fp, " -t TMOUT Interrupt writing to CMD every (%d) ms\n", | | 349 | fprintf(fp, " -t TMOUT Interrupt writing to CMD every (%d) ms\n", |
350 | opts.tmout); | | 350 | opts.tmout); |
351 | } | | 351 | } |
352 | | | 352 | |
353 | /** | | 353 | /** |
354 | * Process program options. | | 354 | * Process program options. |
355 | */ | | 355 | */ |
356 | static int | | 356 | static int |
357 | do_opts(int argc, char *argv[]) | | 357 | do_opts(int argc, char *argv[]) |
358 | { | | 358 | { |
359 | int opt; | | 359 | int opt; |
360 | int i; | | 360 | int i; |
361 | size_t j; | | 361 | size_t j; |