Don't use strings directly in calls to usage(). This is in preparation
[oweals/busybox.git] / util-linux / fsck_minix.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * fsck.c - a file system consistency checker for Linux.
4  *
5  * (C) 1991, 1992 Linus Torvalds. This file may be redistributed
6  * as per the GNU copyleft.
7  */
8
9 /*
10  * 09.11.91  -  made the first rudimetary functions
11  *
12  * 10.11.91  -  updated, does checking, no repairs yet.
13  *              Sent out to the mailing-list for testing.
14  *
15  * 14.11.91  -  Testing seems to have gone well. Added some
16  *              correction-code, and changed some functions.
17  *
18  * 15.11.91  -  More correction code. Hopefully it notices most
19  *              cases now, and tries to do something about them.
20  *
21  * 16.11.91  -  More corrections (thanks to Mika Jalava). Most
22  *              things seem to work now. Yeah, sure.
23  *
24  *
25  * 19.04.92  -  Had to start over again from this old version, as a
26  *              kernel bug ate my enhanced fsck in february.
27  *
28  * 28.02.93  -  added support for different directory entry sizes..
29  *
30  * Sat Mar  6 18:59:42 1993, faith@cs.unc.edu: Output namelen with
31  *                           super-block information
32  *
33  * Sat Oct  9 11:17:11 1993, faith@cs.unc.edu: make exit status conform
34  *                           to that required by fsutil
35  *
36  * Mon Jan  3 11:06:52 1994 - Dr. Wettstein (greg%wind.uucp@plains.nodak.edu)
37  *                            Added support for file system valid flag.  Also
38  *                            added program_version variable and output of
39  *                            program name and version number when program
40  *                            is executed.
41  *
42  * 30.10.94 - added support for v2 filesystem
43  *            (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
44  *
45  * 10.12.94  -  added test to prevent checking of mounted fs adapted
46  *              from Theodore Ts'o's (tytso@athena.mit.edu) e2fsck
47  *              program.  (Daniel Quinlan, quinlan@yggdrasil.com)
48  *
49  * 01.07.96  - Fixed the v2 fs stuff to use the right #defines and such
50  *             for modern libcs (janl@math.uio.no, Nicolai Langfeldt)
51  *
52  * 02.07.96  - Added C bit fiddling routines from rmk@ecs.soton.ac.uk 
53  *             (Russell King).  He made them for ARM.  It would seem
54  *             that the ARM is powerful enough to do this in C whereas
55  *             i386 and m64k must use assembly to get it fast >:-)
56  *             This should make minix fsck systemindependent.
57  *             (janl@math.uio.no, Nicolai Langfeldt)
58  *
59  * 04.11.96  - Added minor fixes from Andreas Schwab to avoid compiler
60  *             warnings.  Added mc68k bitops from 
61  *             Joerg Dorchain <dorchain@mpi-sb.mpg.de>.
62  *
63  * 06.11.96  - Added v2 code submitted by Joerg Dorchain, but written by
64  *             Andreas Schwab.
65  *
66  * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
67  * - added Native Language Support
68  *
69  *
70  * I've had no time to add comments - hopefully the function names
71  * are comments enough. As with all file system checkers, this assumes
72  * the file system is quiescent - don't use it on a mounted device
73  * unless you can be sure nobody is writing to it (and remember that the
74  * kernel can write to it when it searches for files).
75  *
76  * Usuage: fsck [-larvsm] device
77  *      -l for a listing of all the filenames
78  *      -a for automatic repairs (not implemented)
79  *      -r for repairs (interactive) (not implemented)
80  *      -v for verbose (tells how many files)
81  *      -s for super-block info
82  *      -m for minix-like "mode not cleared" warnings
83  *      -f force filesystem check even if filesystem marked as valid
84  *
85  * The device may be a block device or a image of one, but this isn't
86  * enforced (but it's not much fun on a character device :-). 
87  */
88
89 #include "internal.h"
90 #include <stdio.h>
91 #include <errno.h>
92 #include <unistd.h>
93 #include <string.h>
94 #include <fcntl.h>
95 #include <ctype.h>
96 #include <stdlib.h>
97 #include <termios.h>
98 #include <mntent.h>
99 #include <sys/stat.h>
100 #include <sys/param.h>
101
102  
103  typedef unsigned char u8;
104 typedef unsigned short u16;
105 typedef unsigned int u32;
106
107
108 #define MINIX_ROOT_INO 1
109 #define MINIX_LINK_MAX  250
110 #define MINIX2_LINK_MAX 65530
111
112 #define MINIX_I_MAP_SLOTS       8
113 #define MINIX_Z_MAP_SLOTS       64
114 #define MINIX_SUPER_MAGIC       0x137F          /* original minix fs */
115 #define MINIX_SUPER_MAGIC2      0x138F          /* minix fs, 30 char names */
116 #define MINIX2_SUPER_MAGIC      0x2468          /* minix V2 fs */
117 #define MINIX2_SUPER_MAGIC2     0x2478          /* minix V2 fs, 30 char names */
118 #define MINIX_VALID_FS          0x0001          /* Clean fs. */
119 #define MINIX_ERROR_FS          0x0002          /* fs has errors. */
120
121 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
122 #define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
123
124 #define MINIX_V1                0x0001          /* original minix fs */
125 #define MINIX_V2                0x0002          /* minix V2 fs */
126
127 #define INODE_VERSION(inode)    inode->i_sb->u.minix_sb.s_version
128
129 /*
130  * This is the original minix inode layout on disk.
131  * Note the 8-bit gid and atime and ctime.
132  */
133 struct minix_inode {
134         u16 i_mode;
135         u16 i_uid;
136         u32 i_size;
137         u32 i_time;
138         u8  i_gid;
139         u8  i_nlinks;
140         u16 i_zone[9];
141 };
142
143 /*
144  * The new minix inode has all the time entries, as well as
145  * long block numbers and a third indirect block (7+1+1+1
146  * instead of 7+1+1). Also, some previously 8-bit values are
147  * now 16-bit. The inode is now 64 bytes instead of 32.
148  */
149 struct minix2_inode {
150         u16 i_mode;
151         u16 i_nlinks;
152         u16 i_uid;
153         u16 i_gid;
154         u32 i_size;
155         u32 i_atime;
156         u32 i_mtime;
157         u32 i_ctime;
158         u32 i_zone[10];
159 };
160
161 /*
162  * minix super-block data on disk
163  */
164 struct minix_super_block {
165         u16 s_ninodes;
166         u16 s_nzones;
167         u16 s_imap_blocks;
168         u16 s_zmap_blocks;
169         u16 s_firstdatazone;
170         u16 s_log_zone_size;
171         u32 s_max_size;
172         u16 s_magic;
173         u16 s_state;
174         u32 s_zones;
175 };
176
177 struct minix_dir_entry {
178         u16 inode;
179         char name[0];
180 };
181
182 #define BLOCK_SIZE_BITS 10
183 #define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
184
185 #define NAME_MAX         255   /* # chars in a file name */
186
187 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
188
189 #define MINIX_VALID_FS               0x0001          /* Clean fs. */
190 #define MINIX_ERROR_FS               0x0002          /* fs has errors. */
191
192 #define MINIX_SUPER_MAGIC    0x137F          /* original minix fs */
193 #define MINIX_SUPER_MAGIC2   0x138F          /* minix fs, 30 char names */
194
195 #ifndef BLKGETSIZE
196 #define BLKGETSIZE _IO(0x12,96)    /* return device size */
197 #endif
198
199 #ifdef MINIX2_SUPER_MAGIC2
200 #define HAVE_MINIX2 1
201 #endif
202
203 #ifndef __linux__
204 #define volatile
205 #endif
206
207 #define ROOT_INO 1
208
209 #define UPPER(size,n) ((size+((n)-1))/(n))
210 #define INODE_SIZE (sizeof(struct minix_inode))
211 #ifdef HAVE_MINIX2
212 #define INODE_SIZE2 (sizeof(struct minix2_inode))
213 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
214                                     : MINIX_INODES_PER_BLOCK))
215 #else
216 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
217 #endif
218 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
219
220 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
221
222 static char *program_version = "1.2 - 11/11/96";
223 static char *device_name = NULL;
224 static int IN;
225 static int repair = 0, automatic = 0, verbose = 0, list = 0, show =
226         0, warn_mode = 0, force = 0;
227 static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links =
228         0, symlinks = 0, total = 0;
229
230 static int changed = 0;                 /* flags if the filesystem has been changed */
231 static int errors_uncorrected = 0;      /* flag if some error was not corrected */
232 static int dirsize = 16;
233 static int namelen = 14;
234 static int version2 = 0;
235 static struct termios termios;
236 static int termios_set = 0;
237
238 /* File-name data */
239 #define MAX_DEPTH 32
240 static int name_depth = 0;
241 // static char name_list[MAX_DEPTH][BUFSIZ + 1];
242 static char **name_list = NULL;
243
244 static char *inode_buffer = NULL;
245
246 #define Inode (((struct minix_inode *) inode_buffer)-1)
247 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
248 static char super_block_buffer[BLOCK_SIZE];
249
250 #define Super (*(struct minix_super_block *)super_block_buffer)
251 #define INODES ((unsigned long)Super.s_ninodes)
252 #ifdef HAVE_MINIX2
253 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
254 #else
255 #define ZONES ((unsigned long)(Super.s_nzones))
256 #endif
257 #define IMAPS ((unsigned long)Super.s_imap_blocks)
258 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
259 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
260 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
261 #define MAXSIZE ((unsigned long)Super.s_max_size)
262 #define MAGIC (Super.s_magic)
263 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
264
265 static char *inode_map;
266 static char *zone_map;
267
268 static unsigned char *inode_count = NULL;
269 static unsigned char *zone_count = NULL;
270
271 static void recursive_check(unsigned int ino);
272 static void recursive_check2(unsigned int ino);
273
274 #define inode_in_use(x) (isset(inode_map,(x)))
275 #define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1))
276
277 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
278 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
279
280 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
281 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
282
283 static void leave(int) __attribute__ ((noreturn));
284 static void leave(int status)
285 {
286         if (termios_set)
287                 tcsetattr(0, TCSANOW, &termios);
288         exit(status);
289 }
290
291 const char fsck_minix_usage[] =
292         "Usage: fsck.minix [-larvsmf] /dev/name\n"
293 #ifndef BB_FEATURE_TRIVIAL_HELP
294         "\nPerforms a consistency check for MINIX filesystems.\n\n"
295         "Options:\n"
296         "\t-l\tLists all filenames\n"
297         "\t-r\tPerform interactive repairs\n"
298         "\t-a\tPerform automatic repairs\n"
299         "\t-v\tverbose\n"
300         "\t-s\tOutputs super-block information\n"
301         "\t-m\tActivates MINIX-like \"mode not cleared\" warnings\n"
302         "\t-f\tForce file system check.\n\n"
303 #endif
304         ;
305
306 static void show_usage(void)
307 {
308         usage(fsck_minix_usage);
309 }
310
311 static void die(const char *str)
312 {
313         errorMsg("%s\n", str);
314         leave(8);
315 }
316
317 /*
318  * This simply goes through the file-name data and prints out the
319  * current file.
320  */
321 static void print_current_name(void)
322 {
323         int i = 0;
324
325         while (i < name_depth)
326                 printf("/%.*s", namelen, name_list[i++]);
327         if (i == 0)
328                 printf("/");
329 }
330
331 static int ask(const char *string, int def)
332 {
333         int c;
334
335         if (!repair) {
336                 printf("\n");
337                 errors_uncorrected = 1;
338                 return 0;
339         }
340         if (automatic) {
341                 printf("\n");
342                 if (!def)
343                         errors_uncorrected = 1;
344                 return def;
345         }
346         printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
347         for (;;) {
348                 fflush(stdout);
349                 if ((c = getchar()) == EOF) {
350                         if (!def)
351                                 errors_uncorrected = 1;
352                         return def;
353                 }
354                 c = toupper(c);
355                 if (c == 'Y') {
356                         def = 1;
357                         break;
358                 } else if (c == 'N') {
359                         def = 0;
360                         break;
361                 } else if (c == ' ' || c == '\n')
362                         break;
363         }
364         if (def)
365                 printf("y\n");
366         else {
367                 printf("n\n");
368                 errors_uncorrected = 1;
369         }
370         return def;
371 }
372
373 /*
374  * Make certain that we aren't checking a filesystem that is on a
375  * mounted partition.  Code adapted from e2fsck, Copyright (C) 1993,
376  * 1994 Theodore Ts'o.  Also licensed under GPL.
377  */
378 static void check_mount(void)
379 {
380         FILE *f;
381         struct mntent *mnt;
382         int cont;
383         int fd;
384
385         if ((f = setmntent(MOUNTED, "r")) == NULL)
386                 return;
387         while ((mnt = getmntent(f)) != NULL)
388                 if (strcmp(device_name, mnt->mnt_fsname) == 0)
389                         break;
390         endmntent(f);
391         if (!mnt)
392                 return;
393
394         /*
395          * If the root is mounted read-only, then /etc/mtab is
396          * probably not correct; so we won't issue a warning based on
397          * it.
398          */
399         fd = open(MOUNTED, O_RDWR);
400         if (fd < 0 && errno == EROFS)
401                 return;
402         else
403                 close(fd);
404
405         printf("%s is mounted.   ", device_name);
406         if (isatty(0) && isatty(1))
407                 cont = ask("Do you really want to continue", 0);
408         else
409                 cont = 0;
410         if (!cont) {
411                 printf("check aborted.\n");
412                 exit(0);
413         }
414         return;
415 }
416
417 /*
418  * check_zone_nr checks to see that *nr is a valid zone nr. If it
419  * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
420  * if an error was corrected, and returns the zone (0 for no zone
421  * or a bad zone-number).
422  */
423 static int check_zone_nr(unsigned short *nr, int *corrected)
424 {
425         if (!*nr)
426                 return 0;
427         if (*nr < FIRSTZONE)
428                 printf("Zone nr < FIRSTZONE in file `");
429         else if (*nr >= ZONES)
430                 printf("Zone nr >= ZONES in file `");
431         else
432                 return *nr;
433         print_current_name();
434         printf("'.");
435         if (ask("Remove block", 1)) {
436                 *nr = 0;
437                 *corrected = 1;
438         }
439         return 0;
440 }
441
442 #ifdef HAVE_MINIX2
443 static int check_zone_nr2(unsigned int *nr, int *corrected)
444 {
445         if (!*nr)
446                 return 0;
447         if (*nr < FIRSTZONE)
448                 printf("Zone nr < FIRSTZONE in file `");
449         else if (*nr >= ZONES)
450                 printf("Zone nr >= ZONES in file `");
451         else
452                 return *nr;
453         print_current_name();
454         printf("'.");
455         if (ask("Remove block", 1)) {
456                 *nr = 0;
457                 *corrected = 1;
458         }
459         return 0;
460 }
461 #endif
462
463 /*
464  * read-block reads block nr into the buffer at addr.
465  */
466 static void read_block(unsigned int nr, char *addr)
467 {
468         if (!nr) {
469                 memset(addr, 0, BLOCK_SIZE);
470                 return;
471         }
472         if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
473                 printf("Read error: unable to seek to block in file '");
474                 print_current_name();
475                 printf("'\n");
476                 memset(addr, 0, BLOCK_SIZE);
477                 errors_uncorrected = 1;
478         } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
479                 printf("Read error: bad block in file '");
480                 print_current_name();
481                 printf("'\n");
482                 memset(addr, 0, BLOCK_SIZE);
483                 errors_uncorrected = 1;
484         }
485 }
486
487 /*
488  * write_block writes block nr to disk.
489  */
490 static void write_block(unsigned int nr, char *addr)
491 {
492         if (!nr)
493                 return;
494         if (nr < FIRSTZONE || nr >= ZONES) {
495                 printf("Internal error: trying to write bad block\n"
496                            "Write request ignored\n");
497                 errors_uncorrected = 1;
498                 return;
499         }
500         if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
501                 die("seek failed in write_block");
502         if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
503                 printf("Write error: bad block in file '");
504                 print_current_name();
505                 printf("'\n");
506                 errors_uncorrected = 1;
507         }
508 }
509
510 /*
511  * map-block calculates the absolute block nr of a block in a file.
512  * It sets 'changed' if the inode has needed changing, and re-writes
513  * any indirect blocks with errors.
514  */
515 static int map_block(struct minix_inode *inode, unsigned int blknr)
516 {
517         unsigned short ind[BLOCK_SIZE >> 1];
518         unsigned short dind[BLOCK_SIZE >> 1];
519         int blk_chg, block, result;
520
521         if (blknr < 7)
522                 return check_zone_nr(inode->i_zone + blknr, &changed);
523         blknr -= 7;
524         if (blknr < 512) {
525                 block = check_zone_nr(inode->i_zone + 7, &changed);
526                 read_block(block, (char *) ind);
527                 blk_chg = 0;
528                 result = check_zone_nr(blknr + ind, &blk_chg);
529                 if (blk_chg)
530                         write_block(block, (char *) ind);
531                 return result;
532         }
533         blknr -= 512;
534         block = check_zone_nr(inode->i_zone + 8, &changed);
535         read_block(block, (char *) dind);
536         blk_chg = 0;
537         result = check_zone_nr(dind + (blknr / 512), &blk_chg);
538         if (blk_chg)
539                 write_block(block, (char *) dind);
540         block = result;
541         read_block(block, (char *) ind);
542         blk_chg = 0;
543         result = check_zone_nr(ind + (blknr % 512), &blk_chg);
544         if (blk_chg)
545                 write_block(block, (char *) ind);
546         return result;
547 }
548
549 #ifdef HAVE_MINIX2
550 static int map_block2(struct minix2_inode *inode, unsigned int blknr)
551 {
552         unsigned int ind[BLOCK_SIZE >> 2];
553         unsigned int dind[BLOCK_SIZE >> 2];
554         unsigned int tind[BLOCK_SIZE >> 2];
555         int blk_chg, block, result;
556
557         if (blknr < 7)
558                 return check_zone_nr2(inode->i_zone + blknr, &changed);
559         blknr -= 7;
560         if (blknr < 256) {
561                 block = check_zone_nr2(inode->i_zone + 7, &changed);
562                 read_block(block, (char *) ind);
563                 blk_chg = 0;
564                 result = check_zone_nr2(blknr + ind, &blk_chg);
565                 if (blk_chg)
566                         write_block(block, (char *) ind);
567                 return result;
568         }
569         blknr -= 256;
570         if (blknr >= 256 * 256) {
571                 block = check_zone_nr2(inode->i_zone + 8, &changed);
572                 read_block(block, (char *) dind);
573                 blk_chg = 0;
574                 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
575                 if (blk_chg)
576                         write_block(block, (char *) dind);
577                 block = result;
578                 read_block(block, (char *) ind);
579                 blk_chg = 0;
580                 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
581                 if (blk_chg)
582                         write_block(block, (char *) ind);
583                 return result;
584         }
585         blknr -= 256 * 256;
586         block = check_zone_nr2(inode->i_zone + 9, &changed);
587         read_block(block, (char *) tind);
588         blk_chg = 0;
589         result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
590         if (blk_chg)
591                 write_block(block, (char *) tind);
592         block = result;
593         read_block(block, (char *) dind);
594         blk_chg = 0;
595         result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
596         if (blk_chg)
597                 write_block(block, (char *) dind);
598         block = result;
599         read_block(block, (char *) ind);
600         blk_chg = 0;
601         result = check_zone_nr2(ind + blknr % 256, &blk_chg);
602         if (blk_chg)
603                 write_block(block, (char *) ind);
604         return result;
605 }
606 #endif
607
608 static void write_super_block(void)
609 {
610         /*
611          * Set the state of the filesystem based on whether or not there
612          * are uncorrected errors.  The filesystem valid flag is
613          * unconditionally set if we get this far.
614          */
615         Super.s_state |= MINIX_VALID_FS;
616         if (errors_uncorrected)
617                 Super.s_state |= MINIX_ERROR_FS;
618         else
619                 Super.s_state &= ~MINIX_ERROR_FS;
620
621         if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
622                 die("seek failed in write_super_block");
623         if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
624                 die("unable to write super-block");
625
626         return;
627 }
628
629 static void write_tables(void)
630 {
631         write_super_block();
632
633         if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
634                 die("Unable to write inode map");
635         if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
636                 die("Unable to write zone map");
637         if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
638                 die("Unable to write inodes");
639 }
640
641 static void get_dirsize(void)
642 {
643         int block;
644         char blk[BLOCK_SIZE];
645         int size;
646
647 #if HAVE_MINIX2
648         if (version2)
649                 block = Inode2[ROOT_INO].i_zone[0];
650         else
651 #endif
652                 block = Inode[ROOT_INO].i_zone[0];
653         read_block(block, blk);
654         for (size = 16; size < BLOCK_SIZE; size <<= 1) {
655                 if (strcmp(blk + size + 2, "..") == 0) {
656                         dirsize = size;
657                         namelen = size - 2;
658                         return;
659                 }
660         }
661         /* use defaults */
662 }
663
664 static void read_superblock(void)
665 {
666         if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
667                 die("seek failed");
668         if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
669                 die("unable to read super block");
670         if (MAGIC == MINIX_SUPER_MAGIC) {
671                 namelen = 14;
672                 dirsize = 16;
673                 version2 = 0;
674         } else if (MAGIC == MINIX_SUPER_MAGIC2) {
675                 namelen = 30;
676                 dirsize = 32;
677                 version2 = 0;
678 #ifdef HAVE_MINIX2
679         } else if (MAGIC == MINIX2_SUPER_MAGIC) {
680                 namelen = 14;
681                 dirsize = 16;
682                 version2 = 1;
683         } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
684                 namelen = 30;
685                 dirsize = 32;
686                 version2 = 1;
687 #endif
688         } else
689                 die("bad magic number in super-block");
690         if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
691                 die("Only 1k blocks/zones supported");
692         if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
693                 die("bad s_imap_blocks field in super-block");
694         if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
695                 die("bad s_zmap_blocks field in super-block");
696 }
697
698 static void read_tables(void)
699 {
700         inode_map = xmalloc(IMAPS * BLOCK_SIZE);
701         zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
702         memset(inode_map, 0, sizeof(inode_map));
703         memset(zone_map, 0, sizeof(zone_map));
704         inode_buffer = xmalloc(INODE_BUFFER_SIZE);
705         inode_count = xmalloc(INODES + 1);
706         zone_count = xmalloc(ZONES);
707         if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
708                 die("Unable to read inode map");
709         if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
710                 die("Unable to read zone map");
711         if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
712                 die("Unable to read inodes");
713         if (NORM_FIRSTZONE != FIRSTZONE) {
714                 printf("Warning: Firstzone != Norm_firstzone\n");
715                 errors_uncorrected = 1;
716         }
717         get_dirsize();
718         if (show) {
719                 printf("%ld inodes\n", INODES);
720                 printf("%ld blocks\n", ZONES);
721                 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
722                 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
723                 printf("Maxsize=%ld\n", MAXSIZE);
724                 printf("Filesystem state=%d\n", Super.s_state);
725                 printf("namelen=%d\n\n", namelen);
726         }
727 }
728
729 struct minix_inode *get_inode(unsigned int nr)
730 {
731         struct minix_inode *inode;
732
733         if (!nr || nr > INODES)
734                 return NULL;
735         total++;
736         inode = Inode + nr;
737         if (!inode_count[nr]) {
738                 if (!inode_in_use(nr)) {
739                         printf("Inode %d marked not used, but used for file '", nr);
740                         print_current_name();
741                         printf("'\n");
742                         if (repair) {
743                                 if (ask("Mark in use", 1))
744                                         mark_inode(nr);
745                         } else {
746                                 errors_uncorrected = 1;
747                         }
748                 }
749                 if (S_ISDIR(inode->i_mode))
750                         directory++;
751                 else if (S_ISREG(inode->i_mode))
752                         regular++;
753                 else if (S_ISCHR(inode->i_mode))
754                         chardev++;
755                 else if (S_ISBLK(inode->i_mode))
756                         blockdev++;
757                 else if (S_ISLNK(inode->i_mode))
758                         symlinks++;
759                 else if (S_ISSOCK(inode->i_mode));
760                 else if (S_ISFIFO(inode->i_mode));
761                 else {
762                         print_current_name();
763                         printf(" has mode %05o\n", inode->i_mode);
764                 }
765
766         } else
767                 links++;
768         if (!++inode_count[nr]) {
769                 printf("Warning: inode count too big.\n");
770                 inode_count[nr]--;
771                 errors_uncorrected = 1;
772         }
773         return inode;
774 }
775
776 #ifdef HAVE_MINIX2
777 struct minix2_inode *get_inode2(unsigned int nr)
778 {
779         struct minix2_inode *inode;
780
781         if (!nr || nr > INODES)
782                 return NULL;
783         total++;
784         inode = Inode2 + nr;
785         if (!inode_count[nr]) {
786                 if (!inode_in_use(nr)) {
787                         printf("Inode %d marked not used, but used for file '", nr);
788                         print_current_name();
789                         printf("'\n");
790                         if (repair) {
791                                 if (ask("Mark in use", 1))
792                                         mark_inode(nr);
793                                 else
794                                         errors_uncorrected = 1;
795                         }
796                 }
797                 if (S_ISDIR(inode->i_mode))
798                         directory++;
799                 else if (S_ISREG(inode->i_mode))
800                         regular++;
801                 else if (S_ISCHR(inode->i_mode))
802                         chardev++;
803                 else if (S_ISBLK(inode->i_mode))
804                         blockdev++;
805                 else if (S_ISLNK(inode->i_mode))
806                         symlinks++;
807                 else if (S_ISSOCK(inode->i_mode));
808                 else if (S_ISFIFO(inode->i_mode));
809                 else {
810                         print_current_name();
811                         printf(" has mode %05o\n", inode->i_mode);
812                 }
813         } else
814                 links++;
815         if (!++inode_count[nr]) {
816                 printf("Warning: inode count too big.\n");
817                 inode_count[nr]--;
818                 errors_uncorrected = 1;
819         }
820         return inode;
821 }
822 #endif
823
824 static void check_root(void)
825 {
826         struct minix_inode *inode = Inode + ROOT_INO;
827
828         if (!inode || !S_ISDIR(inode->i_mode))
829                 die("root inode isn't a directory");
830 }
831
832 #ifdef HAVE_MINIX2
833 static void check_root2(void)
834 {
835         struct minix2_inode *inode = Inode2 + ROOT_INO;
836
837         if (!inode || !S_ISDIR(inode->i_mode))
838                 die("root inode isn't a directory");
839 }
840 #endif
841
842 static int add_zone(unsigned short *znr, int *corrected)
843 {
844         int result;
845         int block;
846
847         result = 0;
848         block = check_zone_nr(znr, corrected);
849         if (!block)
850                 return 0;
851         if (zone_count[block]) {
852                 printf("Block has been used before. Now in file `");
853                 print_current_name();
854                 printf("'.");
855                 if (ask("Clear", 1)) {
856                         *znr = 0;
857                         block = 0;
858                         *corrected = 1;
859                 }
860         }
861         if (!block)
862                 return 0;
863         if (!zone_in_use(block)) {
864                 printf("Block %d in file `", block);
865                 print_current_name();
866                 printf("' is marked not in use.");
867                 if (ask("Correct", 1))
868                         mark_zone(block);
869         }
870         if (!++zone_count[block])
871                 zone_count[block]--;
872         return block;
873 }
874
875 #ifdef HAVE_MINIX2
876 static int add_zone2(unsigned int *znr, int *corrected)
877 {
878         int result;
879         int block;
880
881         result = 0;
882         block = check_zone_nr2(znr, corrected);
883         if (!block)
884                 return 0;
885         if (zone_count[block]) {
886                 printf("Block has been used before. Now in file `");
887                 print_current_name();
888                 printf("'.");
889                 if (ask("Clear", 1)) {
890                         *znr = 0;
891                         block = 0;
892                         *corrected = 1;
893                 }
894         }
895         if (!block)
896                 return 0;
897         if (!zone_in_use(block)) {
898                 printf("Block %d in file `", block);
899                 print_current_name();
900                 printf("' is marked not in use.");
901                 if (ask("Correct", 1))
902                         mark_zone(block);
903         }
904         if (!++zone_count[block])
905                 zone_count[block]--;
906         return block;
907 }
908 #endif
909
910 static void add_zone_ind(unsigned short *znr, int *corrected)
911 {
912         static char blk[BLOCK_SIZE];
913         int i, chg_blk = 0;
914         int block;
915
916         block = add_zone(znr, corrected);
917         if (!block)
918                 return;
919         read_block(block, blk);
920         for (i = 0; i < (BLOCK_SIZE >> 1); i++)
921                 add_zone(i + (unsigned short *) blk, &chg_blk);
922         if (chg_blk)
923                 write_block(block, blk);
924 }
925
926 #ifdef HAVE_MINIX2
927 static void add_zone_ind2(unsigned int *znr, int *corrected)
928 {
929         static char blk[BLOCK_SIZE];
930         int i, chg_blk = 0;
931         int block;
932
933         block = add_zone2(znr, corrected);
934         if (!block)
935                 return;
936         read_block(block, blk);
937         for (i = 0; i < BLOCK_SIZE >> 2; i++)
938                 add_zone2(i + (unsigned int *) blk, &chg_blk);
939         if (chg_blk)
940                 write_block(block, blk);
941 }
942 #endif
943
944 static void add_zone_dind(unsigned short *znr, int *corrected)
945 {
946         static char blk[BLOCK_SIZE];
947         int i, blk_chg = 0;
948         int block;
949
950         block = add_zone(znr, corrected);
951         if (!block)
952                 return;
953         read_block(block, blk);
954         for (i = 0; i < (BLOCK_SIZE >> 1); i++)
955                 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
956         if (blk_chg)
957                 write_block(block, blk);
958 }
959
960 #ifdef HAVE_MINIX2
961 static void add_zone_dind2(unsigned int *znr, int *corrected)
962 {
963         static char blk[BLOCK_SIZE];
964         int i, blk_chg = 0;
965         int block;
966
967         block = add_zone2(znr, corrected);
968         if (!block)
969                 return;
970         read_block(block, blk);
971         for (i = 0; i < BLOCK_SIZE >> 2; i++)
972                 add_zone_ind2(i + (unsigned int *) blk, &blk_chg);
973         if (blk_chg)
974                 write_block(block, blk);
975 }
976
977 static void add_zone_tind2(unsigned int *znr, int *corrected)
978 {
979         static char blk[BLOCK_SIZE];
980         int i, blk_chg = 0;
981         int block;
982
983         block = add_zone2(znr, corrected);
984         if (!block)
985                 return;
986         read_block(block, blk);
987         for (i = 0; i < BLOCK_SIZE >> 2; i++)
988                 add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
989         if (blk_chg)
990                 write_block(block, blk);
991 }
992 #endif
993
994 static void check_zones(unsigned int i)
995 {
996         struct minix_inode *inode;
997
998         if (!i || i > INODES)
999                 return;
1000         if (inode_count[i] > 1)         /* have we counted this file already? */
1001                 return;
1002         inode = Inode + i;
1003         if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1004                 !S_ISLNK(inode->i_mode)) return;
1005         for (i = 0; i < 7; i++)
1006                 add_zone(i + inode->i_zone, &changed);
1007         add_zone_ind(7 + inode->i_zone, &changed);
1008         add_zone_dind(8 + inode->i_zone, &changed);
1009 }
1010
1011 #ifdef HAVE_MINIX2
1012 static void check_zones2(unsigned int i)
1013 {
1014         struct minix2_inode *inode;
1015
1016         if (!i || i > INODES)
1017                 return;
1018         if (inode_count[i] > 1)         /* have we counted this file already? */
1019                 return;
1020         inode = Inode2 + i;
1021         if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
1022                 && !S_ISLNK(inode->i_mode))
1023                 return;
1024         for (i = 0; i < 7; i++)
1025                 add_zone2(i + inode->i_zone, &changed);
1026         add_zone_ind2(7 + inode->i_zone, &changed);
1027         add_zone_dind2(8 + inode->i_zone, &changed);
1028         add_zone_tind2(9 + inode->i_zone, &changed);
1029 }
1030 #endif
1031
1032 static void check_file(struct minix_inode *dir, unsigned int offset)
1033 {
1034         static char blk[BLOCK_SIZE];
1035         struct minix_inode *inode;
1036         int ino;
1037         char *name;
1038         int block;
1039
1040         block = map_block(dir, offset / BLOCK_SIZE);
1041         read_block(block, blk);
1042         name = blk + (offset % BLOCK_SIZE) + 2;
1043         ino = *(unsigned short *) (name - 2);
1044         if (ino > INODES) {
1045                 print_current_name();
1046                 printf(" contains a bad inode number for file '");
1047                 printf("%.*s'.", namelen, name);
1048                 if (ask(" Remove", 1)) {
1049                         *(unsigned short *) (name - 2) = 0;
1050                         write_block(block, blk);
1051                 }
1052                 ino = 0;
1053         }
1054         if (name_depth < MAX_DEPTH)
1055                 strncpy(name_list[name_depth], name, namelen);
1056         name_depth++;
1057         inode = get_inode(ino);
1058         name_depth--;
1059         if (!offset) {
1060                 if (!inode || strcmp(".", name)) {
1061                         print_current_name();
1062                         printf(": bad directory: '.' isn't first\n");
1063                         errors_uncorrected = 1;
1064                 } else
1065                         return;
1066         }
1067         if (offset == dirsize) {
1068                 if (!inode || strcmp("..", name)) {
1069                         print_current_name();
1070                         printf(": bad directory: '..' isn't second\n");
1071                         errors_uncorrected = 1;
1072                 } else
1073                         return;
1074         }
1075         if (!inode)
1076                 return;
1077         if (name_depth < MAX_DEPTH)
1078                 strncpy(name_list[name_depth], name, namelen);
1079         name_depth++;
1080         if (list) {
1081                 if (verbose)
1082                         printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1083                 print_current_name();
1084                 if (S_ISDIR(inode->i_mode))
1085                         printf(":\n");
1086                 else
1087                         printf("\n");
1088         }
1089         check_zones(ino);
1090         if (inode && S_ISDIR(inode->i_mode))
1091                 recursive_check(ino);
1092         name_depth--;
1093         return;
1094 }
1095
1096 #ifdef HAVE_MINIX2
1097 static void check_file2(struct minix2_inode *dir, unsigned int offset)
1098 {
1099         static char blk[BLOCK_SIZE];
1100         struct minix2_inode *inode;
1101         int ino;
1102         char *name;
1103         int block;
1104
1105         block = map_block2(dir, offset / BLOCK_SIZE);
1106         read_block(block, blk);
1107         name = blk + (offset % BLOCK_SIZE) + 2;
1108         ino = *(unsigned short *) (name - 2);
1109         if (ino > INODES) {
1110                 print_current_name();
1111                 printf(" contains a bad inode number for file '");
1112                 printf("%.*s'.", namelen, name);
1113                 if (ask(" Remove", 1)) {
1114                         *(unsigned short *) (name - 2) = 0;
1115                         write_block(block, blk);
1116                 }
1117                 ino = 0;
1118         }
1119         if (name_depth < MAX_DEPTH)
1120                 strncpy(name_list[name_depth], name, namelen);
1121         name_depth++;
1122         inode = get_inode2(ino);
1123         name_depth--;
1124         if (!offset) {
1125                 if (!inode || strcmp(".", name)) {
1126                         print_current_name();
1127                         printf(": bad directory: '.' isn't first\n");
1128                         errors_uncorrected = 1;
1129                 } else
1130                         return;
1131         }
1132         if (offset == dirsize) {
1133                 if (!inode || strcmp("..", name)) {
1134                         print_current_name();
1135                         printf(": bad directory: '..' isn't second\n");
1136                         errors_uncorrected = 1;
1137                 } else
1138                         return;
1139         }
1140         if (!inode)
1141                 return;
1142         name_depth++;
1143         if (list) {
1144                 if (verbose)
1145                         printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1146                 print_current_name();
1147                 if (S_ISDIR(inode->i_mode))
1148                         printf(":\n");
1149                 else
1150                         printf("\n");
1151         }
1152         check_zones2(ino);
1153         if (inode && S_ISDIR(inode->i_mode))
1154                 recursive_check2(ino);
1155         name_depth--;
1156         return;
1157 }
1158 #endif
1159
1160 static void recursive_check(unsigned int ino)
1161 {
1162         struct minix_inode *dir;
1163         unsigned int offset;
1164
1165         dir = Inode + ino;
1166         if (!S_ISDIR(dir->i_mode))
1167                 die("internal error");
1168         if (dir->i_size < 2 * dirsize) {
1169                 print_current_name();
1170                 printf(": bad directory: size<32");
1171                 errors_uncorrected = 1;
1172         }
1173         for (offset = 0; offset < dir->i_size; offset += dirsize)
1174                 check_file(dir, offset);
1175 }
1176
1177 #ifdef HAVE_MINIX2
1178 static void recursive_check2(unsigned int ino)
1179 {
1180         struct minix2_inode *dir;
1181         unsigned int offset;
1182
1183         dir = Inode2 + ino;
1184         if (!S_ISDIR(dir->i_mode))
1185                 die("internal error");
1186         if (dir->i_size < 2 * dirsize) {
1187                 print_current_name();
1188                 printf(": bad directory: size < 32");
1189                 errors_uncorrected = 1;
1190         }
1191         for (offset = 0; offset < dir->i_size; offset += dirsize)
1192                 check_file2(dir, offset);
1193 }
1194 #endif
1195
1196 static int bad_zone(int i)
1197 {
1198         char buffer[1024];
1199
1200         if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
1201                 die("seek failed in bad_zone");
1202         return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1203 }
1204
1205 static void check_counts(void)
1206 {
1207         int i;
1208
1209         for (i = 1; i <= INODES; i++) {
1210                 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
1211                         printf("Inode %d mode not cleared.", i);
1212                         if (ask("Clear", 1)) {
1213                                 Inode[i].i_mode = 0;
1214                                 changed = 1;
1215                         }
1216                 }
1217                 if (!inode_count[i]) {
1218                         if (!inode_in_use(i))
1219                                 continue;
1220                         printf("Inode %d not used, marked used in the bitmap.", i);
1221                         if (ask("Clear", 1))
1222                                 unmark_inode(i);
1223                         continue;
1224                 }
1225                 if (!inode_in_use(i)) {
1226                         printf("Inode %d used, marked unused in the bitmap.", i);
1227                         if (ask("Set", 1))
1228                                 mark_inode(i);
1229                 }
1230                 if (Inode[i].i_nlinks != inode_count[i]) {
1231                         printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1232                                    i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1233                         if (ask("Set i_nlinks to count", 1)) {
1234                                 Inode[i].i_nlinks = inode_count[i];
1235                                 changed = 1;
1236                         }
1237                 }
1238         }
1239         for (i = FIRSTZONE; i < ZONES; i++) {
1240                 if (zone_in_use(i) == zone_count[i])
1241                         continue;
1242                 if (!zone_count[i]) {
1243                         if (bad_zone(i))
1244                                 continue;
1245                         printf("Zone %d: marked in use, no file uses it.", i);
1246                         if (ask("Unmark", 1))
1247                                 unmark_zone(i);
1248                         continue;
1249                 }
1250                 printf("Zone %d: %sin use, counted=%d\n",
1251                            i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1252         }
1253 }
1254
1255 #ifdef HAVE_MINIX2
1256 static void check_counts2(void)
1257 {
1258         int i;
1259
1260         for (i = 1; i <= INODES; i++) {
1261                 if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
1262                         printf("Inode %d mode not cleared.", i);
1263                         if (ask("Clear", 1)) {
1264                                 Inode2[i].i_mode = 0;
1265                                 changed = 1;
1266                         }
1267                 }
1268                 if (!inode_count[i]) {
1269                         if (!inode_in_use(i))
1270                                 continue;
1271                         printf("Inode %d not used, marked used in the bitmap.", i);
1272                         if (ask("Clear", 1))
1273                                 unmark_inode(i);
1274                         continue;
1275                 }
1276                 if (!inode_in_use(i)) {
1277                         printf("Inode %d used, marked unused in the bitmap.", i);
1278                         if (ask("Set", 1))
1279                                 mark_inode(i);
1280                 }
1281                 if (Inode2[i].i_nlinks != inode_count[i]) {
1282                         printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1283                                    i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1284                                    inode_count[i]);
1285                         if (ask("Set i_nlinks to count", 1)) {
1286                                 Inode2[i].i_nlinks = inode_count[i];
1287                                 changed = 1;
1288                         }
1289                 }
1290         }
1291         for (i = FIRSTZONE; i < ZONES; i++) {
1292                 if (zone_in_use(i) == zone_count[i])
1293                         continue;
1294                 if (!zone_count[i]) {
1295                         if (bad_zone(i))
1296                                 continue;
1297                         printf("Zone %d: marked in use, no file uses it.", i);
1298                         if (ask("Unmark", 1))
1299                                 unmark_zone(i);
1300                         continue;
1301                 }
1302                 printf("Zone %d: %sin use, counted=%d\n",
1303                            i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1304         }
1305 }
1306 #endif
1307
1308 static void check(void)
1309 {
1310         memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1311         memset(zone_count, 0, ZONES * sizeof(*zone_count));
1312         check_zones(ROOT_INO);
1313         recursive_check(ROOT_INO);
1314         check_counts();
1315 }
1316
1317 #ifdef HAVE_MINIX2
1318 static void check2(void)
1319 {
1320         memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1321         memset(zone_count, 0, ZONES * sizeof(*zone_count));
1322         check_zones2(ROOT_INO);
1323         recursive_check2(ROOT_INO);
1324         check_counts2();
1325 }
1326 #endif
1327
1328 /* Wed Feb  9 15:17:06 MST 2000 */
1329 /* dynamically allocate name_list (instead of making it static) */
1330 static void alloc_name_list(void)
1331 {
1332         int i;
1333
1334         name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
1335         for (i = 0; i < MAX_DEPTH; i++)
1336                 name_list[i] = xmalloc(sizeof(char) * BUFSIZ + 1);
1337 }
1338
1339 #if 0
1340 /* execute this atexit() to deallocate name_list[] */
1341 /* piptigger was here */
1342 static void free_name_list(void)
1343 {
1344         int i;
1345
1346         if (name_list) { 
1347                 for (i = 0; i < MAX_DEPTH; i++) {
1348                         if (name_list[i]) {
1349                                 free(name_list[i]);
1350                         }
1351                 }
1352                 free(name_list);
1353         }
1354 }
1355 #endif
1356
1357 extern int fsck_minix_main(int argc, char **argv)
1358 {
1359         struct termios tmp;
1360         int count;
1361         int retcode = 0;
1362
1363         alloc_name_list();
1364         /* Don't bother to free memory.  Exit does
1365          * that automagically, so we can save a few bytes */
1366         //atexit(free_name_list);
1367
1368         if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
1369                 die("bad inode size");
1370 #ifdef HAVE_MINIX2
1371         if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1372                 die("bad v2 inode size");
1373 #endif
1374         while (argc-- > 1) {
1375                 argv++;
1376                 if (argv[0][0] != '-') {
1377                         if (device_name)
1378                                 show_usage();
1379                         else
1380                                 device_name = argv[0];
1381                 } else
1382                         while (*++argv[0])
1383                                 switch (argv[0][0]) {
1384                                 case 'l':
1385                                         list = 1;
1386                                         break;
1387                                 case 'a':
1388                                         automatic = 1;
1389                                         repair = 1;
1390                                         break;
1391                                 case 'r':
1392                                         automatic = 0;
1393                                         repair = 1;
1394                                         break;
1395                                 case 'v':
1396                                         verbose = 1;
1397                                         break;
1398                                 case 's':
1399                                         show = 1;
1400                                         break;
1401                                 case 'm':
1402                                         warn_mode = 1;
1403                                         break;
1404                                 case 'f':
1405                                         force = 1;
1406                                         break;
1407                                 default:
1408                                         show_usage();
1409                                 }
1410         }
1411         if (!device_name)
1412                 show_usage();
1413         check_mount();                          /* trying to check a mounted filesystem? */
1414         if (repair && !automatic) {
1415                 if (!isatty(0) || !isatty(1))
1416                         die("need terminal for interactive repairs");
1417         }
1418         IN = open(device_name, repair ? O_RDWR : O_RDONLY);
1419         if (IN < 0)
1420                 die("unable to open '%s'");
1421         for (count = 0; count < 3; count++)
1422                 sync();
1423         read_superblock();
1424
1425         /*
1426          * Determine whether or not we should continue with the checking.
1427          * This is based on the status of the filesystem valid and error
1428          * flags and whether or not the -f switch was specified on the 
1429          * command line.
1430          */
1431         printf("%s, %s\n", applet_name, program_version);
1432         if (!(Super.s_state & MINIX_ERROR_FS) &&
1433                 (Super.s_state & MINIX_VALID_FS) && !force) {
1434                 if (repair)
1435                         printf("%s is clean, no check.\n", device_name);
1436                 return retcode;
1437         } else if (force)
1438                 printf("Forcing filesystem check on %s.\n", device_name);
1439         else if (repair)
1440                 printf("Filesystem on %s is dirty, needs checking.\n",
1441                            device_name);
1442
1443         read_tables();
1444
1445         if (repair && !automatic) {
1446                 tcgetattr(0, &termios);
1447                 tmp = termios;
1448                 tmp.c_lflag &= ~(ICANON | ECHO);
1449                 tcsetattr(0, TCSANOW, &tmp);
1450                 termios_set = 1;
1451         }
1452 #if HAVE_MINIX2
1453         if (version2) {
1454                 check_root2();
1455                 check2();
1456         } else
1457 #endif
1458         {
1459                 check_root();
1460                 check();
1461         }
1462         if (verbose) {
1463                 int i, free;
1464
1465                 for (i = 1, free = 0; i <= INODES; i++)
1466                         if (!inode_in_use(i))
1467                                 free++;
1468                 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free),
1469                            100 * (INODES - free) / INODES);
1470                 for (i = FIRSTZONE, free = 0; i < ZONES; i++)
1471                         if (!zone_in_use(i))
1472                                 free++;
1473                 printf("%6ld zones used (%ld%%)\n", (ZONES - free),
1474                            100 * (ZONES - free) / ZONES);
1475                 printf("\n%6d regular files\n"
1476                            "%6d directories\n"
1477                            "%6d character device files\n"
1478                            "%6d block device files\n"
1479                            "%6d links\n"
1480                            "%6d symbolic links\n"
1481                            "------\n"
1482                            "%6d files\n",
1483                            regular, directory, chardev, blockdev,
1484                            links - 2 * directory + 1, symlinks,
1485                            total - 2 * directory + 1);
1486         }
1487         if (changed) {
1488                 write_tables();
1489                 printf("----------------------------\n"
1490                            "FILE SYSTEM HAS BEEN CHANGED\n"
1491                            "----------------------------\n");
1492                 for (count = 0; count < 3; count++)
1493                         sync();
1494         } else if (repair)
1495                 write_super_block();
1496
1497         if (repair && !automatic)
1498                 tcsetattr(0, TCSANOW, &termios);
1499
1500         if (changed)
1501                 retcode += 3;
1502         if (errors_uncorrected)
1503                 retcode += 4;
1504         return retcode;
1505 }