1 /* file.c - Additional file attributes
2
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
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.
10
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.
15
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/>.
18
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.
21 */
22
23 /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
24 * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
25
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <unistd.h>
32
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> */
36
37 # include <asm/types.h>
38
39 #include <linux/msdos_fs.h>
40
41 #include "common.h"
42 #include "file.h"
43
44
45 FDSC *fp_root = NULL;
46
47
put_char(char ** p,unsigned char c)48 static void put_char(char **p,unsigned char c)
49 {
50 if ((c >= ' ' && c < 0x7f) || c >= 0xa0) *(*p)++ = c;
51 else {
52 *(*p)++ = '\\';
53 *(*p)++ = '0'+(c >> 6);
54 *(*p)++ = '0'+((c >> 3) & 7);
55 *(*p)++ = '0'+(c & 7);
56 }
57 }
58
59
file_name(unsigned char * fixed)60 char *file_name(unsigned char *fixed)
61 {
62 static char path[MSDOS_NAME*4+2];
63 char *p;
64 int i,j;
65
66 p = path;
67 for (i = j = 0; i < 8; i++)
68 if (fixed[i] != ' ') {
69 while (j++ < i) *p++ = ' ';
70 put_char(&p,fixed[i]);
71 }
72 if (strncmp(fixed+8," ",3)) {
73 *p++ = '.';
74 for (i = j = 0; i < 3; i++)
75 if (fixed[i+8] != ' ') {
76 while (j++ < i) *p++ = ' ';
77 put_char(&p,fixed[i+8]);
78 }
79 }
80 *p = 0;
81 return path;
82 }
83
84
file_cvt(unsigned char * name,unsigned char * fixed)85 int file_cvt(unsigned char *name,unsigned char *fixed)
86 {
87 unsigned char c;
88 int size,ext,cnt;
89
90 size = 8;
91 ext = 0;
92 while (*name) {
93 c = *name;
94 if (c < ' ' || c > 0x7e || strchr("*?<>|\"/",c)) {
95 printf("Invalid character in name. Use \\ooo for special "
96 "characters.\n");
97 return 0;
98 }
99 if (c == '.') {
100 if (ext) {
101 printf("Duplicate dots in name.\n");
102 return 0;
103 }
104 while (size--) *fixed++ = ' ';
105 size = 3;
106 ext = 1;
107 name++;
108 continue;
109 }
110 if (c == '\\') {
111 c = 0;
112 for (cnt = 3; cnt; cnt--) {
113 if (*name < '0' || *name > '7') {
114 printf("Invalid octal character.\n");
115 return 0;
116 }
117 c = c*8+*name++-'0';
118 }
119 if (cnt < 4) {
120 printf("Expected three octal digits.\n");
121 return 0;
122 }
123 name += 3;
124 }
125 if (islower(c)) c = toupper(c);
126 if (size) {
127 *fixed++ = c;
128 size--;
129 }
130 name++;
131 }
132 if (*name || size == 8) return 0;
133 if (!ext) {
134 while (size--) *fixed++ = ' ';
135 size = 3;
136 }
137 while (size--) *fixed++ = ' ';
138 return 1;
139 }
140
141
file_add(char * path,FD_TYPE type)142 void file_add(char *path,FD_TYPE type)
143 {
144 FDSC **current,*walk;
145 char name[MSDOS_NAME];
146 char *here;
147
148 current = &fp_root;
149 if (*path != '/') die("%s: Absolute path required.",path);
150 path++;
151 while (1) {
152 if ((here = strchr(path,'/'))) *here = 0;
153 if (!file_cvt(path,name)) exit(2);
154 for (walk = *current; walk; walk = walk->next)
155 if (!here && (!strncmp(name,walk->name,MSDOS_NAME) || (type ==
156 fdt_undelete && !strncmp(name+1,walk->name+1,MSDOS_NAME-1))))
157 die("Ambiguous name: \"%s\"",path);
158 else if (here && !strncmp(name,walk->name,MSDOS_NAME)) break;
159 if (!walk) {
160 walk = alloc(sizeof(FDSC));
161 strncpy(walk->name,name,MSDOS_NAME);
162 walk->type = here ? fdt_none : type;
163 walk->first = NULL;
164 walk->next = *current;
165 *current = walk;
166 }
167 current = &walk->first;
168 if (!here) break;
169 *here = '/';
170 path = here+1;
171 }
172 }
173
174
file_cd(FDSC ** curr,char * fixed)175 FDSC **file_cd(FDSC **curr,char *fixed)
176 {
177 FDSC **walk;
178
179 if (!curr || !*curr) return NULL;
180 for (walk = curr; *walk; walk = &(*walk)->next)
181 if (!strncmp((*walk)->name,fixed,MSDOS_NAME) && (*walk)->first)
182 return &(*walk)->first;
183 return NULL;
184 }
185
186
file_find(FDSC ** dir,char * fixed)187 static FDSC **file_find(FDSC **dir,char *fixed)
188 {
189 if (!dir || !*dir) return NULL;
190 if (*(unsigned char *) fixed == DELETED_FLAG) {
191 while (*dir) {
192 if (!strncmp((*dir)->name+1,fixed+1,MSDOS_NAME-1) && !(*dir)->first)
193 return dir;
194 dir = &(*dir)->next;
195 }
196 return NULL;
197 }
198 while (*dir) {
199 if (!strncmp((*dir)->name,fixed,MSDOS_NAME) && !(*dir)->first)
200 return dir;
201 dir = &(*dir)->next;
202 }
203 return NULL;
204 }
205
206
file_type(FDSC ** curr,char * fixed)207 FD_TYPE file_type(FDSC **curr,char *fixed)
208 {
209 FDSC **this;
210
211 if ((this = file_find(curr,fixed))) return (*this)->type;
212 return fdt_none;
213 }
214
215
file_modify(FDSC ** curr,char * fixed)216 void file_modify(FDSC **curr,char *fixed)
217 {
218 FDSC **this,*next;
219
220 if (!(this = file_find(curr,fixed)))
221 die("Internal error: file_find failed");
222 switch ((*this)->type) {
223 case fdt_drop:
224 printf("Dropping %s\n",file_name(fixed));
225 *(unsigned char *) fixed = DELETED_FLAG;
226 break;
227 case fdt_undelete:
228 *fixed = *(*this)->name;
229 printf("Undeleting %s\n",file_name(fixed));
230 break;
231 default:
232 die("Internal error: file_modify");
233 }
234 next = (*this)->next;
235 free(*this);
236 *this = next;
237 }
238
239
report_unused(FDSC * this)240 static void report_unused(FDSC *this)
241 {
242 FDSC *next;
243
244 while (this) {
245 next = this->next;
246 if (this->first) report_unused(this->first);
247 else if (this->type != fdt_none)
248 printf("Warning: did not %s file %s\n",this->type == fdt_drop ?
249 "drop" : "undelete",file_name(this->name));
250 free(this);
251 this = next;
252 }
253 }
254
255
file_unused(void)256 void file_unused(void)
257 {
258 report_unused(fp_root);
259 }
260
261 /* Local Variables: */
262 /* tab-width: 8 */
263 /* End: */
264