-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkstemp.c
42 lines (34 loc) · 1.02 KB
/
mkstemp.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
38
39
40
41
42
#include"apue.h"
#include<errno.h>
void make_temp(char* template);
int main(int argc, char* argv[])
{
char good_template[] = "/tmp/fileXXXXXX";
/*因为mkstemp函数会试图对template指针指向的字符数组中的占位符进行修改,
所以这个template指针实参指向的字符数组空间必须存放在栈堆上,而不是这里的可执行文件只读段中*/
char* bad_template = "/tmp/fileXXXXXX";
printf("trying to create first temp file...\n");
make_temp(good_template);
printf("trying to create second temp file...\n");
make_temp(bad_template);
exit(EXIT_SUCCESS);
}
void make_temp(char* template) {
int fd;
struct stat statbuf;
if ((fd = mkstemp(template)) < 0)
err_sys("mkstemp error");
printf("temp file name = %s\n", template);
close(fd);
//检查是否可stat
if (stat(template, &statbuf) < 0) {
if (errno == ENOENT)
printf("file doesn't exist\n");
else
err_sys("stat fialed");
}
else {
printf("file exist\n");
unlink(template);
}
}