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>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 On Debian systems, the complete text of the GNU General Public License
20 can be found in /usr/share/common-licenses/GPL-3 file.
23 /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
24 * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
33 #define _LINUX_STAT_H /* hack to avoid inclusion of <linux/stat.h> */
34 #define _LINUX_STRING_H_ /* hack to avoid inclusion of <linux/string.h>*/
35 #define _LINUX_FS_H /* hack to avoid inclusion of <linux/fs.h> */
37 # include <asm/types.h>
39 #include <linux/msdos_fs.h>
48 static void put_char(char **p,unsigned char c)
50 if ((c >= ' ' && c < 0x7f) || c >= 0xa0) *(*p)++ = c;
53 *(*p)++ = '0'+(c >> 6);
54 *(*p)++ = '0'+((c >> 3) & 7);
55 *(*p)++ = '0'+(c & 7);
61 * Construct the "pretty-printed" representation of the name in a short directory entry.
63 * @param[in] fixed Pointer to name[0] of a DIR_ENT
65 * @return Pointer to static string containing pretty "8.3" equivalent of the
66 * name in the directory entry.
68 char *file_name(unsigned char *fixed)
70 static char path[MSDOS_NAME*4+2];
75 for (i = j = 0; i < 8; i++)
76 if (fixed[i] != ' ') {
77 while (j++ < i) *p++ = ' ';
78 put_char(&p,fixed[i]);
80 if (strncmp(fixed+8," ",3)) {
82 for (i = j = 0; i < 3; i++)
83 if (fixed[i+8] != ' ') {
84 while (j++ < i) *p++ = ' ';
85 put_char(&p,fixed[i+8]);
93 int file_cvt(unsigned char *name,unsigned char *fixed)
102 if (c < ' ' || c > 0x7e || strchr("*?<>|\"/",c)) {
103 printf("Invalid character in name. Use \\ooo for special "
109 printf("Duplicate dots in name.\n");
112 while (size--) *fixed++ = ' ';
120 for (cnt = 3; cnt; cnt--) {
121 if (*name < '0' || *name > '7') {
122 printf("Invalid octal character.\n");
128 printf("Expected three octal digits.\n");
133 if (islower(c)) c = toupper(c);
140 if (*name || size == 8) return 0;
142 while (size--) *fixed++ = ' ';
145 while (size--) *fixed++ = ' ';
150 void file_add(char *path,FD_TYPE type)
152 FDSC **current,*walk;
153 char name[MSDOS_NAME];
157 if (*path != '/') die("%s: Absolute path required.",path);
160 if ((here = strchr(path,'/'))) *here = 0;
161 if (!file_cvt(path,name)) exit(2);
162 for (walk = *current; walk; walk = walk->next)
163 if (!here && (!strncmp(name,walk->name,MSDOS_NAME) || (type ==
164 fdt_undelete && !strncmp(name+1,walk->name+1,MSDOS_NAME-1))))
165 die("Ambiguous name: \"%s\"",path);
166 else if (here && !strncmp(name,walk->name,MSDOS_NAME)) break;
168 walk = alloc(sizeof(FDSC));
169 strncpy(walk->name,name,MSDOS_NAME);
170 walk->type = here ? fdt_none : type;
172 walk->next = *current;
175 current = &walk->first;
183 FDSC **file_cd(FDSC **curr,char *fixed)
187 if (!curr || !*curr) return NULL;
188 for (walk = curr; *walk; walk = &(*walk)->next)
189 if (!strncmp((*walk)->name,fixed,MSDOS_NAME) && (*walk)->first)
190 return &(*walk)->first;
195 static FDSC **file_find(FDSC **dir,char *fixed)
197 if (!dir || !*dir) return NULL;
198 if (*(unsigned char *) fixed == DELETED_FLAG) {
200 if (!strncmp((*dir)->name+1,fixed+1,MSDOS_NAME-1) && !(*dir)->first)
207 if (!strncmp((*dir)->name,fixed,MSDOS_NAME) && !(*dir)->first)
215 /* Returns the attribute of the file FIXED in directory CURR or FDT_NONE if no
216 such file exists or if CURR is NULL. */
217 FD_TYPE file_type(FDSC **curr,char *fixed)
221 if ((this = file_find(curr,fixed))) return (*this)->type;
226 void file_modify(FDSC **curr,char *fixed)
230 if (!(this = file_find(curr,fixed)))
231 die("Internal error: file_find failed");
232 switch ((*this)->type) {
234 printf("Dropping %s\n",file_name(fixed));
235 *(unsigned char *) fixed = DELETED_FLAG;
238 *fixed = *(*this)->name;
239 printf("Undeleting %s\n",file_name(fixed));
242 die("Internal error: file_modify");
244 next = (*this)->next;
250 static void report_unused(FDSC *this)
256 if (this->first) report_unused(this->first);
257 else if (this->type != fdt_none)
258 printf("Warning: did not %s file %s\n",this->type == fdt_drop ?
259 "drop" : "undelete",file_name(this->name));
266 void file_unused(void)
268 report_unused(fp_root);