OSDN Git Service

mkostemp: fix implementation
[uclinux-h8/uClibc.git] / test / stdlib / test-mkostemp-O_CLOEXEC.c
1 #define _XOPEN_SOURCE_EXTENDED
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <sys/wait.h>
10 #include <errno.h>
11
12 #if !defined __ARCH_USE_MMU__
13 # define fork vfork
14 #endif
15
16 int main(int argc, char *argv[]) {
17     int fd, status;
18     char buff[5];
19     char template[] = "/tmp/test-mkostemp.XXXXXX";
20
21     fd = mkostemp(template, O_CLOEXEC);
22     unlink(template);
23
24     snprintf(buff, 5, "%d", fd);
25
26     if(!fork())
27         if(execl("./test-mkostemp-child", "test-mkostemp-child", buff, NULL) == -1)
28             exit(EXIT_FAILURE);
29
30     wait(&status);
31
32     memset(buff, 0, 5);
33     lseek(fd, 0, SEEK_SET);
34     errno = 0;
35     if(read(fd, buff, 5) == -1)
36         exit(EXIT_FAILURE);
37
38     if(!strncmp(buff, "test", 5))
39         exit(EXIT_FAILURE);
40     else
41         exit(EXIT_SUCCESS);
42
43     close(fd);
44     exit(EXIT_SUCCESS);
45 }