Add FoundationDB stuff
[slow/toolbox.git] / nbmand-demo.c
1 #define _FILE_OFFSET_BITS 64
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <stdint.h>
9
10 int main(int argc, char *argv[])
11 {
12     const char *file;
13     struct flock fl;
14     int fd;
15     pid_t pid;
16
17     if (argc != 2) {
18         printf("usage: %s FILE\n", argv[0]);
19         exit(1);
20     }
21     file = argv[1];
22
23     printf("Press <RETURN> to open file \"%s\": ", file);
24     getchar();
25
26     if ((fd = open(file, O_RDWR)) == -1) {
27         perror("open");
28         exit(1);
29     }
30
31     if ((pid = fork()) == -1) {
32         perror("fork");
33         exit(1);
34     }
35
36     if (pid == 0) {
37         /* Child */
38         printf("Press <RETURN> to rename file \"%s\" -> \"tmp\": ", file);
39         getchar();
40
41         if (rename(file, "tmp") == -1) {
42             perror("rename");
43         } else {
44             printf("renamed.\n");
45             rename("tmp", file);
46         }
47
48         printf("Press <RETURN> to exit");
49         getchar();
50         exit(0);
51     }
52
53     /* Parent */
54     if (waitpid(pid, NULL, 0) != pid) {
55         perror("waitpid");
56         exit(1);
57     }
58
59     close(fd);
60
61     return 0;
62 }