-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen.c
31 lines (27 loc) · 798 Bytes
/
open.c
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
/* open bug */
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/errno.h>
int main(){
const char *f = "tmp.txt";
int fd = open(f, O_WRONLY); /* int fd = open(f, O_EXCL | O_CREAT); */
if(fd != -1) {
printf("[pid %ld] file %s already exists\n", (long)getpid(), f);
close(fd);
} else {
if(errno != ENOENT) {
fprintf(stderr, "open failed, errno=%d\n", errno); /* open failed for unknow reason */
return -1;
} else {
fd = open(f, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
if (fd == -1) {
fprintf(stderr, "open failed\n");
return -1;
}
/* below line is buggy, may not be true, because first open() didn't use the flag
O_EXCL | O_CREATE for atomicity */
printf("[pid %ld] created file %s execlusively\n", (long)getpid(), f);
}
}
}