1 /* vi: set sw=4 ts=4: */
3 * Display or change file attributes on a fat file system
5 * Copyright 2005 H. Peter Anvin
6 * Busybox'ed (2014) by Pascal Bellard <pascal.bellard@ads-lu.com>
8 * This file can be redistributed under the terms of the GNU General
11 //config:config FATATTR
12 //config: bool "fatattr"
14 //config: select PLATFORM_LINUX
16 //config: fatattr lists or changes the file attributes on a fat file system.
18 //applet:IF_FATATTR(APPLET(fatattr, BB_DIR_BIN, BB_SUID_DROP))
20 //kbuild:lib-$(CONFIG_FATATTR) += fatattr.o
22 //usage:#define fatattr_trivial_usage
23 //usage: "[-+rhsvda] FILE..."
24 //usage:#define fatattr_full_usage "\n\n"
25 //usage: "Change file attributes on FAT filesystem\n"
26 //usage: "\n - Clear attributes"
27 //usage: "\n + Set attributes"
28 //usage: "\n r Read only"
29 //usage: "\n h Hidden"
30 //usage: "\n s System"
31 //usage: "\n v Volume label"
32 //usage: "\n d Directory"
33 //usage: "\n a Archive"
36 /* linux/msdos_fs.h says: */
37 #ifndef FAT_IOCTL_GET_ATTRIBUTES
38 # define FAT_IOCTL_GET_ATTRIBUTES _IOR('r', 0x10, uint32_t)
39 # define FAT_IOCTL_SET_ATTRIBUTES _IOW('r', 0x11, uint32_t)
42 /* Currently supports only the FAT flags, not the NTFS ones.
43 * Extra space at the end is a hack to print space separator in file listing.
44 * Let's hope no one ever passes space as an option char :)
46 static const char bit_to_char[] ALIGN1 = "rhsvda67 ";
48 static inline unsigned long get_flag(char c)
50 const char *fp = strchr(bit_to_char, c);
52 bb_error_msg_and_die("invalid character '%c'", c);
53 return 1 << (fp - bit_to_char);
56 static unsigned decode_arg(const char *arg)
64 int fatattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
65 int fatattr_main(int argc UNUSED_PARAM, char **argv)
67 unsigned set_mask = 0;
68 unsigned clear_mask = 0;
76 if (arg[0] != '-' && arg[0] != '+')
89 fd = xopen(*argv, O_RDONLY);
90 xioctl(fd, FAT_IOCTL_GET_ATTRIBUTES, &attr);
91 attr = (attr | set_mask) & ~clear_mask;
92 if (set_mask | clear_mask)
93 xioctl(fd, FAT_IOCTL_SET_ATTRIBUTES, &attr);
95 for (i = 0; bit_to_char[i]; i++) {
96 bb_putchar((attr & 1) ? bit_to_char[i] : ' ');