aboutsummaryrefslogtreecommitdiffstats
path: root/src/examples/create_remove_file.c
blob: 93f191e8b9a34b6aa031fa7c01211de1b3545d6e (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
47
48
49
50
51
52
53
54
55
56
57
58
/* klaar@ida
*/
  
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <syscall.h>

int main(int argc, char* argv[])
{
  char buf[40];
  char ref[40];
  int loops;
  int i, n;
  int fd;
  
  if (argc != 3)
  {
    printf("%s: bad arguments\n", argv[0]);
    return 1;
  }
  
  loops = atoi(argv[2]);
  
  for (i = 0; i < loops; ++i)
  {
    if ( create(argv[1], 500) )
    {
      snprintf(ref, 40, "file '%s' iteration %d", argv[1], i);
      fd = open(argv[1]);
      remove(argv[1]);
      
      write(fd, ref, strlen(ref));
      seek(fd, 0);
      for (n = 0; n < 100; ++n)
        ;
      read(fd, buf, 40);      
      close(fd);

      if (strcmp(ref, buf) != 0)
      {
        printf("%s: ERROR bad file content\n", argv[0]);
        printf("%s: written  : '%s'\n", argv[0], ref);
        printf("%s: read back: '%s'\n", argv[0], buf);
        exit(1);
      }
      
      if ( (i * 10000 / loops) % 1000 == 0 )
        printf("%s: %d%% done\n", argv[0], (i * 100 / loops));
    }
    else
    {
      printf("%s: ERROR create '%s' failed\n", argv[0], argv[1]);
      exit(1);
    }
  }
  return 0;
}