blob: a73b73f5ed55786767debff91367826af0c91b7a [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/* 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#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
29#include <ctype.h>
30#include <unistd.h>
31
32#define _LINUX_STAT_H /* hack to avoid inclusion of <linux/stat.h> */
33#define _LINUX_STRING_H_ /* hack to avoid inclusion of <linux/string.h> */
34#define _LINUX_FS_H /* hack to avoid inclusion of <linux/fs.h> */
35
36#include <asm/types.h>
37
38#include <linux/msdos_fs.h>
39
40#include "common.h"
41#include "file.h"
42
43FDSC *fp_root = NULL;
44
45static void put_char(char **p, unsigned char c)
46{
47 if ((c >= ' ' && c < 0x7f) || c >= 0xa0)
48 *(*p)++ = c;
49 else {
50 *(*p)++ = '\\';
51 *(*p)++ = '0' + (c >> 6);
52 *(*p)++ = '0' + ((c >> 3) & 7);
53 *(*p)++ = '0' + (c & 7);
54 }
55}
56
57/**
58 * Construct the "pretty-printed" representation of the name in a short directory entry.
59 *
60 * @param[in] fixed Pointer to name[0] of a DIR_ENT
61 *
62 * @return Pointer to static string containing pretty "8.3" equivalent of the
63 * name in the directory entry.
64 */
65char *file_name(unsigned char *fixed)
66{
67 static char path[MSDOS_NAME * 4 + 2];
68 char *p;
69 int i, j;
70
71 p = path;
72 for (i = j = 0; i < 8; i++)
73 if (fixed[i] != ' ') {
74 while (j++ < i)
75 *p++ = ' ';
76 put_char(&p, fixed[i]);
77 }
78 if (strncmp((const char *)(fixed + 8), " ", 3)) {
79 *p++ = '.';
80 for (i = j = 0; i < 3; i++)
81 if (fixed[i + 8] != ' ') {
82 while (j++ < i)
83 *p++ = ' ';
84 put_char(&p, fixed[i + 8]);
85 }
86 }
87 *p = 0;
88 return path;
89}
90
91int file_cvt(unsigned char *name, unsigned char *fixed)
92{
93 unsigned char c;
94 int size, ext, cnt;
95
96 size = 8;
97 ext = 0;
98 while (*name) {
99 c = *name;
100 if (c < ' ' || c > 0x7e || strchr("*?<>|\"/", c)) {
101 printf("Invalid character in name. Use \\ooo for special "
102 "characters.\n");
103 return 0;
104 }
105 if (c == '.') {
106 if (ext) {
107 printf("Duplicate dots in name.\n");
108 return 0;
109 }
110 while (size--)
111 *fixed++ = ' ';
112 size = 3;
113 ext = 1;
114 name++;
115 continue;
116 }
117 if (c == '\\') {
118 c = 0;
119 for (cnt = 3; cnt; cnt--) {
120 if (*name < '0' || *name > '7') {
121 printf("Invalid octal character.\n");
122 return 0;
123 }
124 c = c * 8 + *name++ - '0';
125 }
126 if (cnt < 4) {
127 printf("Expected three octal digits.\n");
128 return 0;
129 }
130 name += 3;
131 }
132 if (islower(c))
133 c = toupper(c);
134 if (size) {
135 *fixed++ = c;
136 size--;
137 }
138 name++;
139 }
140 if (*name || size == 8)
141 return 0;
142 if (!ext) {
143 while (size--)
144 *fixed++ = ' ';
145 size = 3;
146 }
147 while (size--)
148 *fixed++ = ' ';
149 return 1;
150}
151
152void file_add(char *path, FD_TYPE type)
153{
154 FDSC **current, *walk;
155 char name[MSDOS_NAME];
156 char *here;
157
158 current = &fp_root;
159 if (*path != '/')
160 die("%s: Absolute path required.", path);
161 path++;
162 while (1) {
163 if ((here = strchr(path, '/')))
164 *here = 0;
165 if (!file_cvt((unsigned char *)path, (unsigned char *)name))
166 exit(2);
167 for (walk = *current; walk; walk = walk->next)
168 if (!here && (!strncmp(name, walk->name, MSDOS_NAME) || (type ==
169 fdt_undelete
170 &&
171 !strncmp
172 (name + 1,
173 walk->name
174 + 1,
175 MSDOS_NAME
176 - 1))))
177 die("Ambiguous name: \"%s\"", path);
178 else if (here && !strncmp(name, walk->name, MSDOS_NAME))
179 break;
180 if (!walk) {
181 walk = alloc(sizeof(FDSC));
182 strncpy(walk->name, name, MSDOS_NAME);
183 walk->type = here ? fdt_none : type;
184 walk->first = NULL;
185 walk->next = *current;
186 *current = walk;
187 }
188 current = &walk->first;
189 if (!here)
190 break;
191 *here = '/';
192 path = here + 1;
193 }
194}
195
196FDSC **file_cd(FDSC ** curr, char *fixed)
197{
198 FDSC **walk;
199
200 if (!curr || !*curr)
201 return NULL;
202 for (walk = curr; *walk; walk = &(*walk)->next)
203 if (!strncmp((*walk)->name, fixed, MSDOS_NAME) && (*walk)->first)
204 return &(*walk)->first;
205 return NULL;
206}
207
208static FDSC **file_find(FDSC ** dir, char *fixed)
209{
210 if (!dir || !*dir)
211 return NULL;
212 if (*(unsigned char *)fixed == DELETED_FLAG) {
213 while (*dir) {
214 if (!strncmp((*dir)->name + 1, fixed + 1, MSDOS_NAME - 1)
215 && !(*dir)->first)
216 return dir;
217 dir = &(*dir)->next;
218 }
219 return NULL;
220 }
221 while (*dir) {
222 if (!strncmp((*dir)->name, fixed, MSDOS_NAME) && !(*dir)->first)
223 return dir;
224 dir = &(*dir)->next;
225 }
226 return NULL;
227}
228
229/* Returns the attribute of the file FIXED in directory CURR or FDT_NONE if no
230 such file exists or if CURR is NULL. */
231FD_TYPE file_type(FDSC ** curr, char *fixed)
232{
233 FDSC **this;
234
235 if ((this = file_find(curr, fixed)))
236 return (*this)->type;
237 return fdt_none;
238}
239
240void file_modify(FDSC ** curr, char *fixed)
241{
242 FDSC **this, *next;
243
244 if (!(this = file_find(curr, fixed)))
245 die("Internal error: file_find failed");
246 switch ((*this)->type) {
247 case fdt_drop:
248 printf("Dropping %s\n", file_name((unsigned char *)fixed));
249 *(unsigned char *)fixed = DELETED_FLAG;
250 break;
251 case fdt_undelete:
252 *fixed = *(*this)->name;
253 printf("Undeleting %s\n", file_name((unsigned char *)fixed));
254 break;
255 default:
256 die("Internal error: file_modify");
257 }
258 next = (*this)->next;
259 free(*this);
260 *this = next;
261}
262
263static void report_unused(FDSC * this)
264{
265 FDSC *next;
266
267 while (this) {
268 next = this->next;
269 if (this->first)
270 report_unused(this->first);
271 else if (this->type != fdt_none)
272 printf("Warning: did not %s file %s\n", this->type == fdt_drop ?
273 "drop" : "undelete", file_name((unsigned char *)this->name));
274 free(this);
275 this = next;
276 }
277}
278
279void file_unused(void)
280{
281 report_unused(fp_root);
282}