forked from MeiK2333/apue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.13.c
37 lines (31 loc) · 754 Bytes
/
5.13.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
32
33
34
35
36
37
#include <errno.h>
#include "apue.h"
void make_temp(char *template);
int main() {
char good_template[] = "/tmp/dirXXXXXX";
char *bad_template = "/tmp/dirXXXXXX";
printf("trying to create first temp file...\n");
make_temp(good_template);
printf("try to create second temp file...\n");
make_temp(bad_template);
return 0;
}
void make_temp(char *template) {
int fd;
struct stat sbuf;
if ((fd = mkstemp(template)) < 0) {
err_sys("can't create temp file");
}
printf("temp name = %s\n", template);
close(fd);
if (stat(template, &sbuf) < 0) {
if (errno == ENOENT) {
printf("file doesn't exist\n");
} else {
err_sys("stat failed");
}
} else {
printf("file exists\n");
unlink(template);
}
}