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))
19 //kbuild:lib-$(CONFIG_FATATTR) += fatattr.o
21 //usage:#define fatattr_trivial_usage
22 //usage: "[-+rhsvda] FILE..."
23 //usage:#define fatattr_full_usage "\n\n"
24 //usage: "Change file attributes on FAT filesystem\n"
25 //usage: "\n - Clear attributes"
26 //usage: "\n + Set attributes"
27 //usage: "\n r Read only"
28 //usage: "\n h Hidden"
29 //usage: "\n s System"
30 //usage: "\n v Volume label"
31 //usage: "\n d Directory"
32 //usage: "\n a Archive"
35 /* linux/msdos_fs.h says: */
36 #ifndef FAT_IOCTL_GET_ATTRIBUTES
37 # define FAT_IOCTL_GET_ATTRIBUTES _IOR('r', 0x10, uint32_t)
38 # define FAT_IOCTL_SET_ATTRIBUTES _IOW('r', 0x11, uint32_t)
41 /* Currently supports only the FAT flags, not the NTFS ones.
42 * Extra space at the end is a hack to print space separator in file listing.
43 * Let's hope no one ever passes space as an option char :)
45 static const char bit_to_char
[] = "rhsvda67 ";
47 static inline unsigned long get_flag(char c
)
49 const char *fp
= strchr(bit_to_char
, c
);
51 bb_error_msg_and_die("invalid character '%c'", c
);
52 return 1 << (fp
- bit_to_char
);
55 static unsigned decode_arg(const char *arg
)
63 int fatattr_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
64 int fatattr_main(int argc UNUSED_PARAM
, char **argv
)
66 unsigned set_mask
= 0;
67 unsigned clear_mask
= 0;
75 if (arg
[0] != '-' && arg
[0] != '+')
88 fd
= xopen(*argv
, O_RDONLY
);
89 xioctl(fd
, FAT_IOCTL_GET_ATTRIBUTES
, &attr
);
90 attr
= (attr
| set_mask
) & ~clear_mask
;
91 if (set_mask
| clear_mask
)
92 xioctl(fd
, FAT_IOCTL_SET_ATTRIBUTES
, &attr
);
94 for (i
= 0; bit_to_char
[i
]; i
++) {
95 bb_putchar((attr
& 1) ? bit_to_char
[i
] : ' ');