diff options
Diffstat (limited to 'examples/lineup.c')
| -rw-r--r-- | examples/lineup.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/examples/lineup.c b/examples/lineup.c new file mode 100644 index 0000000..60402d0 --- /dev/null +++ b/examples/lineup.c | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /* lineup.c | ||
| 2 | |||
| 3 | Converts a file to uppercase in-place. | ||
| 4 | |||
| 5 | Incidentally, another way to do this while avoiding the seeks | ||
| 6 | would be to open the input file, then remove() it and reopen | ||
| 7 | it under another handle. Because of Unix deletion semantics | ||
| 8 | this works fine. */ | ||
| 9 | |||
| 10 | #include <ctype.h> | ||
| 11 | #include <stdio.h> | ||
| 12 | #include <syscall.h> | ||
| 13 | |||
| 14 | int | ||
| 15 | main (int argc, char *argv[]) | ||
| 16 | { | ||
| 17 | char buf[1024]; | ||
| 18 | int handle; | ||
| 19 | |||
| 20 | if (argc != 2) | ||
| 21 | exit (1); | ||
| 22 | |||
| 23 | handle = open (argv[1]); | ||
| 24 | if (handle < 0) | ||
| 25 | exit (2); | ||
| 26 | |||
| 27 | for (;;) | ||
| 28 | { | ||
| 29 | int n, i; | ||
| 30 | |||
| 31 | n = read (handle, buf, sizeof buf); | ||
| 32 | if (n <= 0) | ||
| 33 | break; | ||
| 34 | |||
| 35 | for (i = 0; i < n; i++) | ||
| 36 | buf[i] = toupper ((unsigned char) buf[i]); | ||
| 37 | |||
| 38 | seek (handle, tell (handle) - n); | ||
| 39 | if (write (handle, buf, n) != n) | ||
| 40 | printf ("write failed\n"); | ||
| 41 | } | ||
| 42 | |||
| 43 | close (handle); | ||
| 44 | |||
| 45 | return EXIT_SUCCESS; | ||
| 46 | } | ||
