summaryrefslogtreecommitdiffstats
path: root/src/examples/lineup.c
blob: 60402d0a90b20895060b0af9d7b08c0121cdd154 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* lineup.c

   Converts a file to uppercase in-place.

   Incidentally, another way to do this while avoiding the seeks
   would be to open the input file, then remove() it and reopen
   it under another handle.  Because of Unix deletion semantics
   this works fine. */

#include <ctype.h>
#include <stdio.h>
#include <syscall.h>

int
main (int argc, char *argv[])
{
  char buf[1024];
  int handle;

  if (argc != 2)
    exit (1);

  handle = open (argv[1]);
  if (handle < 0)
    exit (2);

  for (;;) 
    {
      int n, i;

      n = read (handle, buf, sizeof buf);
      if (n <= 0)
        break;

      for (i = 0; i < n; i++)
        buf[i] = toupper ((unsigned char) buf[i]);

      seek (handle, tell (handle) - n);
      if (write (handle, buf, n) != n)
        printf ("write failed\n");
    }

  close (handle);

  return EXIT_SUCCESS;
}