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