1#include <stdio.h>
2#include <unistd.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6#include <errno.h>
7
8/* backward compat in case it's not defined */
9#ifndef O_TMPFILE
10#define	O_TMPFILE	(020000000|O_DIRECTORY)
11#endif
12
13/*
14 * DESCRIPTION:
15 *	Check if the kernel support O_TMPFILE.
16 */
17
18int
19main(int argc, char *argv[])
20{
21	int fd;
22	struct stat buf;
23
24	if (argc < 2) {
25		fprintf(stderr, "Usage: %s dir\n", argv[0]);
26		return (2);
27	}
28	if (stat(argv[1], &buf) < 0) {
29		perror("stat");
30		return (2);
31	}
32	if (!S_ISDIR(buf.st_mode)) {
33		fprintf(stderr, "\"%s\" is not a directory\n", argv[1]);
34		return (2);
35	}
36
37	fd = open(argv[1], O_TMPFILE | O_WRONLY, 0666);
38	if (fd < 0) {
39		if (errno == EISDIR) {
40			fprintf(stderr,
41			    "The kernel doesn't support O_TMPFILE\n");
42			return (1);
43		} else if (errno == EOPNOTSUPP) {
44			fprintf(stderr,
45			    "The filesystem doesn't support O_TMPFILE\n");
46			return (2);
47		}
48		perror("open");
49	} else {
50		close(fd);
51	}
52	return (0);
53}
54