48 lines · cpp
1// RUN: %clangxx -O1 %s -o %t2// RUN: rm -rf %t.tmp3// RUN: %run %t 14// RUN: %run %t 25 6#include <assert.h>7#include <errno.h>8#include <fcntl.h>9#include <stdio.h>10#include <stdlib.h>11#include <string.h>12#include <sys/stat.h>13#include <unistd.h>14 15void test(const char *path, int flags) {16 int fd = open(path, flags, 0600);17 if (fd == -1) {18 perror(path);19 if (errno == EOPNOTSUPP || errno == EINVAL)20 return;21 }22 assert(fd != -1);23 struct stat info;24 int result = fstat(fd, &info);25 assert((info.st_mode & ~S_IFMT) == 0600);26 assert(result == 0);27 close(fd);28}29 30int main(int argc, char *argv[]) {31 assert(argc == 2);32 char buff[10000];33 sprintf(buff, "%s.tmp", argv[0]);34 if (atoi(argv[1]) == 1) {35 unlink(buff);36 test(buff, O_RDWR | O_CREAT);37 }38 39#ifdef O_TMPFILE40 if (atoi(argv[1]) == 2) {41 char *last = strrchr(buff, '/');42 assert(last);43 *last = 0;44 test(buff, O_RDWR | O_TMPFILE);45 }46#endif47}48