summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/stripeof.c
blob: f2a17ac0df187415c18057b024ce42b1ecc4cc9e (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
**  STRIPEOF.C
**
**  public domain demo by Bob Stout
*/

#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>

#define BUFSIZE 16384

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

      if (2 > argc)
      {
            puts("Usage: STRIPEOF filename1 [...filenameN]");
            return EXIT_FAILURE;
      }
      if (NULL == (buf = malloc(BUFSIZE)))
      {
            puts("STRIPEOF internal failure");
            return EXIT_FAILURE;
      }
      while (--argc)
      {
            int fd;
            size_t bytes;
            int found = 0;
            long zpos = 0L;

            if (-1 == (fd = open(*(++argv), O_RDWR | O_BINARY)))
            {
                  printf("Couldn't open %s\n", *argv);
                  return EXIT_FAILURE;
            }
            while (0 < (bytes = read(fd, buf, BUFSIZE)))
            {
                  int i;

                  for (i = 0; i < (int)bytes; ++i)
                  {
                        if (('Z' - 64) == buf[i])
                        {
                              found = 1;
                              zpos += i;
                              break;
                        }
                  }
                  if (found)
                        break;
                  zpos += bytes;
            }
            if (found)
                  chsize(fd, zpos);
      }
      return EXIT_SUCCESS;
}