blob: dffcec1cdeb0947fbdc9ce81901bc15efbc0f1a7 [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>
Matt Mower18794c82015-11-11 16:22:45 -06005 Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
bigbiff bigbiff9c754052013-01-09 09:09:08 -05006
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19
Matt Mower18794c82015-11-11 16:22:45 -060020 The complete text of the GNU General Public License
bigbiff bigbiff9c754052013-01-09 09:09:08 -050021 can be found in /usr/share/common-licenses/GPL-3 file.
22*/
23
24/* FAT32, VFAT, Atari format support, and various fixes additions May 1998
25 * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
26
27#include <stdlib.h>
28#include <stdio.h>
29#include <string.h>
30#include <ctype.h>
31#include <unistd.h>
32
bigbiff bigbiff9c754052013-01-09 09:09:08 -050033#include "common.h"
34#include "file.h"
Matt Mower18794c82015-11-11 16:22:45 -060035#include "msdos_fs.h"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050036
37FDSC *fp_root = NULL;
38
39static void put_char(char **p, unsigned char c)
40{
41 if ((c >= ' ' && c < 0x7f) || c >= 0xa0)
42 *(*p)++ = c;
43 else {
44 *(*p)++ = '\\';
45 *(*p)++ = '0' + (c >> 6);
46 *(*p)++ = '0' + ((c >> 3) & 7);
47 *(*p)++ = '0' + (c & 7);
48 }
49}
50
51/**
52 * Construct the "pretty-printed" representation of the name in a short directory entry.
53 *
54 * @param[in] fixed Pointer to name[0] of a DIR_ENT
55 *
56 * @return Pointer to static string containing pretty "8.3" equivalent of the
57 * name in the directory entry.
58 */
59char *file_name(unsigned char *fixed)
60{
61 static char path[MSDOS_NAME * 4 + 2];
62 char *p;
63 int i, j;
64
65 p = path;
66 for (i = j = 0; i < 8; i++)
67 if (fixed[i] != ' ') {
68 while (j++ < i)
69 *p++ = ' ';
70 put_char(&p, fixed[i]);
71 }
72 if (strncmp((const char *)(fixed + 8), " ", 3)) {
73 *p++ = '.';
74 for (i = j = 0; i < 3; i++)
75 if (fixed[i + 8] != ' ') {
76 while (j++ < i)
77 *p++ = ' ';
78 put_char(&p, fixed[i + 8]);
79 }
80 }
81 *p = 0;
82 return path;
83}
84
85int 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--)
105 *fixed++ = ' ';
106 size = 3;
107 ext = 1;
108 name++;
109 continue;
110 }
111 if (c == '\\') {
112 c = 0;
113 for (cnt = 3; cnt; cnt--) {
114 if (*name < '0' || *name > '7') {
115 printf("Invalid octal character.\n");
116 return 0;
117 }
118 c = c * 8 + *name++ - '0';
119 }
120 if (cnt < 4) {
121 printf("Expected three octal digits.\n");
122 return 0;
123 }
124 name += 3;
125 }
126 if (islower(c))
127 c = toupper(c);
128 if (size) {
129 *fixed++ = c;
130 size--;
131 }
132 name++;
133 }
134 if (*name || size == 8)
135 return 0;
136 if (!ext) {
137 while (size--)
138 *fixed++ = ' ';
139 size = 3;
140 }
141 while (size--)
142 *fixed++ = ' ';
143 return 1;
144}
145
146void file_add(char *path, FD_TYPE type)
147{
148 FDSC **current, *walk;
149 char name[MSDOS_NAME];
150 char *here;
151
152 current = &fp_root;
153 if (*path != '/')
154 die("%s: Absolute path required.", path);
155 path++;
156 while (1) {
157 if ((here = strchr(path, '/')))
158 *here = 0;
159 if (!file_cvt((unsigned char *)path, (unsigned char *)name))
160 exit(2);
161 for (walk = *current; walk; walk = walk->next)
162 if (!here && (!strncmp(name, walk->name, MSDOS_NAME) || (type ==
163 fdt_undelete
164 &&
165 !strncmp
166 (name + 1,
167 walk->name
168 + 1,
169 MSDOS_NAME
170 - 1))))
171 die("Ambiguous name: \"%s\"", path);
172 else if (here && !strncmp(name, walk->name, MSDOS_NAME))
173 break;
174 if (!walk) {
175 walk = alloc(sizeof(FDSC));
176 strncpy(walk->name, name, MSDOS_NAME);
177 walk->type = here ? fdt_none : type;
178 walk->first = NULL;
179 walk->next = *current;
180 *current = walk;
181 }
182 current = &walk->first;
183 if (!here)
184 break;
185 *here = '/';
186 path = here + 1;
187 }
188}
189
190FDSC **file_cd(FDSC ** curr, char *fixed)
191{
192 FDSC **walk;
193
194 if (!curr || !*curr)
195 return NULL;
196 for (walk = curr; *walk; walk = &(*walk)->next)
197 if (!strncmp((*walk)->name, fixed, MSDOS_NAME) && (*walk)->first)
198 return &(*walk)->first;
199 return NULL;
200}
201
202static FDSC **file_find(FDSC ** dir, char *fixed)
203{
204 if (!dir || !*dir)
205 return NULL;
206 if (*(unsigned char *)fixed == DELETED_FLAG) {
207 while (*dir) {
208 if (!strncmp((*dir)->name + 1, fixed + 1, MSDOS_NAME - 1)
209 && !(*dir)->first)
210 return dir;
211 dir = &(*dir)->next;
212 }
213 return NULL;
214 }
215 while (*dir) {
216 if (!strncmp((*dir)->name, fixed, MSDOS_NAME) && !(*dir)->first)
217 return dir;
218 dir = &(*dir)->next;
219 }
220 return NULL;
221}
222
223/* Returns the attribute of the file FIXED in directory CURR or FDT_NONE if no
224 such file exists or if CURR is NULL. */
225FD_TYPE file_type(FDSC ** curr, char *fixed)
226{
227 FDSC **this;
228
229 if ((this = file_find(curr, fixed)))
230 return (*this)->type;
231 return fdt_none;
232}
233
234void file_modify(FDSC ** curr, char *fixed)
235{
236 FDSC **this, *next;
237
238 if (!(this = file_find(curr, fixed)))
239 die("Internal error: file_find failed");
240 switch ((*this)->type) {
241 case fdt_drop:
242 printf("Dropping %s\n", file_name((unsigned char *)fixed));
243 *(unsigned char *)fixed = DELETED_FLAG;
244 break;
245 case fdt_undelete:
246 *fixed = *(*this)->name;
247 printf("Undeleting %s\n", file_name((unsigned char *)fixed));
248 break;
249 default:
250 die("Internal error: file_modify");
251 }
252 next = (*this)->next;
253 free(*this);
254 *this = next;
255}
256
257static void report_unused(FDSC * this)
258{
259 FDSC *next;
260
261 while (this) {
262 next = this->next;
263 if (this->first)
264 report_unused(this->first);
265 else if (this->type != fdt_none)
266 printf("Warning: did not %s file %s\n", this->type == fdt_drop ?
267 "drop" : "undelete", file_name((unsigned char *)this->name));
268 free(this);
269 this = next;
270 }
271}
272
273void file_unused(void)
274{
275 report_unused(fp_root);
276}