1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2022 iXsystems, Inc.
14  */
15 
16 /*
17  * FreeBSD exposes additional file attributes via ls -o and chflags.
18  * Under Linux, we provide ZFS_IOC_[GS]ETDOSFLAGS ioctl()s.
19  *
20  * This application is the equivalent to FreeBSD ls -lo $1 | awk '{print $5}'.
21  */
22 
23 #include <err.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdbool.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <sys/fs/zfs.h>
32 #include "dos_attributes.h"
33 
34 int
35 main(int argc, const char *const *argv)
36 {
37 	if (argc != 2)
38 		errx(EXIT_FAILURE, "usage: %s file", argv[0]);
39 
40 	int fd = open(argv[1], O_RDONLY | O_CLOEXEC);
41 	if (fd == -1)
42 		err(EXIT_FAILURE, "%s", argv[1]);
43 
44 	uint64_t flags;
45 	if (ioctl(fd, ZFS_IOC_GETDOSFLAGS, &flags) == -1)
46 		err(EXIT_FAILURE, "ZFS_IOC_GETDOSFLAGS");
47 
48 	bool any = false;
49 	for (size_t i = 0; i < ARRAY_SIZE(all_dos_attributes); ++i)
50 		if (flags & all_dos_attributes[i]) {
51 			if (any)
52 				putchar(',');
53 			(void) fputs(*all_dos_attribute_names[i], stdout);
54 			any = true;
55 		}
56 	if (any)
57 		(void) putchar('\n');
58 	else
59 		(void) puts("-");
60 }
61