added port of dosfsck. Does not work yet as I cannot figure how to open the device...
[reactos.git] / rosapps / sysutils / dosfsck / dosfsck.c
1 /* dosfsck.c - User interface */
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 "../version.h"
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <getopt.h>
17
18 #include "common.h"
19 #include "dosfsck.h"
20 #include "io.h"
21 #include "boot.h"
22 #include "fat.h"
23 #include "file.h"
24 #include "check.h"
25
26
27 int interactive = 0,list = 0,test = 0,verbose = 0,write_immed = 0;
28 int atari_format = 0;
29 unsigned n_files = 0;
30 void *mem_queue = NULL;
31
32
33 static void usage(char *name)
34 {
35 fprintf(stderr,"usage: %s [-aAflrtvVwy] [-d path -d ...] "
36 "[-u path -u ...]\n%15sdevice\n",name,"");
37 fprintf(stderr," -a automatically repair the file system\n");
38 fprintf(stderr," -A toggle Atari file system format\n");
39 fprintf(stderr," -d path drop that file\n");
40 fprintf(stderr," -f salvage unused chains to files\n");
41 fprintf(stderr," -l list path names\n");
42 fprintf(stderr," -n no-op, check non-interactively without changing\n");
43 fprintf(stderr," -r interactively repair the file system\n");
44 fprintf(stderr," -t test for bad clusters\n");
45 fprintf(stderr," -u path try to undelete that (non-directory) file\n");
46 fprintf(stderr," -v verbose mode\n");
47 fprintf(stderr," -V perform a verification pass\n");
48 fprintf(stderr," -w write changes to disk immediately\n");
49 fprintf(stderr," -y same as -a, for compat with other *fsck\n");
50 exit(2);
51 }
52
53
54 /*
55 * ++roman: On m68k, check if this is an Atari; if yes, turn on Atari variant
56 * of MS-DOS filesystem by default.
57 */
58 static void check_atari( void )
59 {
60 #ifdef __mc68000__
61 FILE *f;
62 char line[128], *p;
63
64 if (!(f = fopen( "/proc/hardware", "r" ))) {
65 perror( "/proc/hardware" );
66 return;
67 }
68
69 while( fgets( line, sizeof(line), f ) ) {
70 if (strncmp( line, "Model:", 6 ) == 0) {
71 p = line + 6;
72 p += strspn( p, " \t" );
73 if (strncmp( p, "Atari ", 6 ) == 0)
74 atari_format = 1;
75 break;
76 }
77 }
78 fclose( f );
79 #endif
80 }
81
82
83 int main(int argc,char **argv)
84 {
85 DOS_FS fs;
86 int rw,salvage_files,verify,c;
87 unsigned long free_clusters;
88
89 rw = salvage_files = verify = 0;
90 interactive = 1;
91 check_atari();
92
93 while ((c = getopt(argc,argv,"Aad:flnrtu:vVwy")) != EOF)
94 switch (c) {
95 case 'A': /* toggle Atari format */
96 atari_format = !atari_format;
97 break;
98 case 'a':
99 case 'y':
100 rw = 1;
101 interactive = 0;
102 salvage_files = 1;
103 break;
104 case 'd':
105 file_add(optarg,fdt_drop);
106 break;
107 case 'f':
108 salvage_files = 1;
109 break;
110 case 'l':
111 list = 1;
112 break;
113 case 'n':
114 rw = 0;
115 interactive = 0;
116 break;
117 case 'r':
118 rw = 1;
119 interactive = 1;
120 break;
121 case 't':
122 test = 1;
123 break;
124 case 'u':
125 file_add(optarg,fdt_undelete);
126 break;
127 case 'v':
128 verbose = 1;
129 printf("dosfsck " VERSION " (" VERSION_DATE ")\n");
130 break;
131 case 'V':
132 verify = 1;
133 break;
134 case 'w':
135 write_immed = 1;
136 break;
137 default:
138 usage(argv[0]);
139 }
140 if ((test || write_immed) && !rw) {
141 fprintf(stderr,"-t and -w require -a or -r\n");
142 exit(2);
143 }
144 if (optind != argc-1) usage(argv[0]);
145
146 printf( "dosfsck " VERSION ", " VERSION_DATE ", FAT32, LFN\n" );
147 fs_open(argv[optind],rw);
148 read_boot(&fs);
149 if (verify) printf("Starting check/repair pass.\n");
150 while (read_fat(&fs), scan_root(&fs)) qfree(&mem_queue);
151 if (test) fix_bad(&fs);
152 if (salvage_files) reclaim_file(&fs);
153 else reclaim_free(&fs);
154 free_clusters = update_free(&fs);
155 file_unused();
156 qfree(&mem_queue);
157 if (verify) {
158 printf("Starting verification pass.\n");
159 read_fat(&fs);
160 scan_root(&fs);
161 reclaim_free(&fs);
162 qfree(&mem_queue);
163 }
164
165 if (fs_changed()) {
166 if (rw) {
167 if (interactive)
168 rw = get_key("yn","Perform changes ? (y/n)") == 'y';
169 else printf("Performing changes.\n");
170 }
171 else
172 printf("Leaving file system unchanged.\n");
173 }
174
175 printf( "%s: %u files, %lu/%lu clusters\n", argv[optind],
176 n_files, fs.clusters - free_clusters, fs.clusters );
177
178 return fs_close(rw) ? 1 : 0;
179 }
180
181 /* Local Variables: */
182 /* tab-width: 8 */
183 /* End: */