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