1 /* file.c - Additional file attributes
3 Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
4 Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
5 Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
6 Copyright (C) 2020 Pali Rohár <pali.rohar@gmail.com>
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 The complete text of the GNU General Public License
22 can be found in /usr/share/common-licenses/GPL-3 file.
25 /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
26 * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
41 static void put_char(char **p, unsigned char c, unsigned int out_size)
43 if (dos_char_to_printable(p, c, out_size))
45 if (out_size >= 1 && c >= ' ' && c < 0x7f)
47 else if (out_size >= 4) {
49 *(*p)++ = '0' + (c >> 6);
50 *(*p)++ = '0' + ((c >> 3) & 7);
51 *(*p)++ = '0' + (c & 7);
56 * Construct the "pretty-printed" representation of the name in a short directory entry.
58 * @param[in] fixed Pointer to name[0] of a DIR_ENT
60 * @return Pointer to static string containing pretty "8.3" equivalent of the
61 * name in the directory entry.
63 char *file_name(unsigned char *fixed)
65 static char path[256];
71 if (fixed[0] == 0x05) {
72 put_char(&p, 0xe5, path + sizeof(path) - 1 - p);
77 if (fixed[i] != ' ') {
80 put_char(&p, fixed[i], path + sizeof(path) - 1 - p);
82 if (strncmp((const char *)(fixed + 8), " ", 3)) {
84 for (i = j = 0; i < 3; i++)
85 if (fixed[i + 8] != ' ') {
88 put_char(&p, fixed[i + 8], path + sizeof(path) - 1 - p);
95 int file_cvt(unsigned char *name, unsigned char *fixed)
104 if (c < ' ' || c > 0x7e || strchr("*?<>|\"/", c)) {
105 printf("Invalid character in name. Use \\ooo for special "
111 printf("Duplicate dots in name.\n");
124 for (cnt = 3; cnt; cnt--) {
125 if (*name < '0' || *name > '7') {
126 printf("Expected three octal digits.\n");
129 c = c * 8 + *name++ - '0';
136 if (size == 8 && c == 0xE5)
144 if (*name || size == 8)
156 void file_add(char *path, FD_TYPE type)
158 FDSC **current, *walk;
159 char name[MSDOS_NAME];
164 die("%s: Absolute path required.", path);
167 if ((here = strchr(path, '/')))
169 if (!file_cvt((unsigned char *)path, (unsigned char *)name))
171 for (walk = *current; walk; walk = walk->next)
172 if (!here && (!strncmp(name, walk->name, MSDOS_NAME) || (type ==
181 die("Ambiguous name: \"%s\"", path);
182 else if (here && !strncmp(name, walk->name, MSDOS_NAME))
185 walk = alloc(sizeof(FDSC));
186 strncpy(walk->name, name, MSDOS_NAME);
187 walk->type = here ? fdt_none : type;
189 walk->next = *current;
192 current = &walk->first;
200 FDSC **file_cd(FDSC ** curr, char *fixed)
206 for (walk = curr; *walk; walk = &(*walk)->next)
207 if (!strncmp((*walk)->name, fixed, MSDOS_NAME) && (*walk)->first)
208 return &(*walk)->first;
212 static FDSC **file_find(FDSC ** dir, char *fixed)
216 if (*(unsigned char *)fixed == DELETED_FLAG) {
218 if (!strncmp((*dir)->name + 1, fixed + 1, MSDOS_NAME - 1)
226 if (!strncmp((*dir)->name, fixed, MSDOS_NAME) && !(*dir)->first)
233 /* Returns the attribute of the file FIXED in directory CURR or FDT_NONE if no
234 such file exists or if CURR is NULL. */
235 FD_TYPE file_type(FDSC ** curr, char *fixed)
239 if ((this = file_find(curr, fixed)))
240 return (*this)->type;
244 void file_modify(FDSC ** curr, char *fixed)
248 if (!(this = file_find(curr, fixed)))
249 die("Internal error: file_find failed");
250 switch ((*this)->type) {
252 printf("Dropping %s\n", file_name((unsigned char *)fixed));
253 *(unsigned char *)fixed = DELETED_FLAG;
256 *fixed = *(*this)->name;
257 printf("Undeleting %s\n", file_name((unsigned char *)fixed));
260 die("Internal error: file_modify");
262 next = (*this)->next;
267 static void report_unused(FDSC * this)
274 report_unused(this->first);
275 else if (this->type != fdt_none)
276 printf("Warning: did not %s file %s\n", this->type == fdt_drop ?
277 "drop" : "undelete", file_name((unsigned char *)this->name));
283 void file_unused(void)
285 report_unused(fp_root);