Note how to find list of BusyBox libc dependancies, the ide being to
[oweals/busybox.git] / 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
101 #include <linux/fs.h>
102 #include <linux/minix_fs.h>
103
104 #ifdef MINIX2_SUPER_MAGIC2
105 #define HAVE_MINIX2 1
106 #endif
107
108 #ifndef __linux__
109 #define volatile
110 #endif
111
112 #define ROOT_INO 1
113
114 #define UPPER(size,n) ((size+((n)-1))/(n))
115 #define INODE_SIZE (sizeof(struct minix_inode))
116 #ifdef HAVE_MINIX2
117 #define INODE_SIZE2 (sizeof(struct minix2_inode))
118 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
119                                     : MINIX_INODES_PER_BLOCK))
120 #else
121 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
122 #endif
123 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
124
125 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
126
127 static char *program_name = "fsck.minix";
128 static char *program_version = "1.2 - 11/11/96";
129 static char *device_name = NULL;
130 static int IN;
131 static int repair = 0, automatic = 0, verbose = 0, list = 0, show =
132         0, warn_mode = 0, force = 0;
133 static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links =
134         0, symlinks = 0, total = 0;
135
136 static int changed = 0;                 /* flags if the filesystem has been changed */
137 static int errors_uncorrected = 0;      /* flag if some error was not corrected */
138 static int dirsize = 16;
139 static int namelen = 14;
140 static int version2 = 0;
141 static struct termios termios;
142 static int termios_set = 0;
143
144 /* File-name data */
145 #define MAX_DEPTH 32
146 static int name_depth = 0;
147 // static char name_list[MAX_DEPTH][BUFSIZ + 1];
148 static char **name_list = NULL;
149
150 static char *inode_buffer = NULL;
151
152 #define Inode (((struct minix_inode *) inode_buffer)-1)
153 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
154 static char super_block_buffer[BLOCK_SIZE];
155
156 #define Super (*(struct minix_super_block *)super_block_buffer)
157 #define INODES ((unsigned long)Super.s_ninodes)
158 #ifdef HAVE_MINIX2
159 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
160 #else
161 #define ZONES ((unsigned long)(Super.s_nzones))
162 #endif
163 #define IMAPS ((unsigned long)Super.s_imap_blocks)
164 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
165 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
166 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
167 #define MAXSIZE ((unsigned long)Super.s_max_size)
168 #define MAGIC (Super.s_magic)
169 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
170
171 static char *inode_map;
172 static char *zone_map;
173
174 static unsigned char *inode_count = NULL;
175 static unsigned char *zone_count = NULL;
176
177 static void recursive_check(unsigned int ino);
178 static void recursive_check2(unsigned int ino);
179
180 #define inode_in_use(x) (isset(inode_map,(x)))
181 #define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1))
182
183 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
184 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
185
186 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
187 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
188
189 static void leave(int) __attribute__ ((noreturn));
190 static void leave(int status)
191 {
192         if (termios_set)
193                 tcsetattr(0, TCSANOW, &termios);
194         exit(status);
195 }
196
197 static void show_usage(void)
198 {
199         fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
200                         BB_VER, BB_BT);
201         fprintf(stderr, "Usage: %s [-larvsmf] /dev/name\n\n", program_name);
202         fprintf(stderr,
203                         "Performs a consistency check for MINIX filesystems.\n\n");
204         fprintf(stderr, "OPTIONS:\n");
205         fprintf(stderr, "\t-l\tLists all filenames\n");
206         fprintf(stderr, "\t-r\tPerform interactive repairs\n");
207         fprintf(stderr, "\t-a\tPerform automatic repairs\n");
208         fprintf(stderr, "\t-v\tverbose\n");
209         fprintf(stderr, "\t-s\tOutputs super-block information\n");
210         fprintf(stderr,
211                         "\t-m\tActivates MINIX-like \"mode not cleared\" warnings\n");
212         fprintf(stderr, "\t-f\tForce file system check.\n\n");
213         leave(16);
214 }
215
216 static void die(const char *str)
217 {
218         fprintf(stderr, "%s: %s\n", program_name, str);
219         leave(8);
220 }
221
222 /*
223  * This simply goes through the file-name data and prints out the
224  * current file.
225  */
226 static void print_current_name(void)
227 {
228         int i = 0;
229
230         while (i < name_depth)
231                 printf("/%.*s", namelen, name_list[i++]);
232         if (i == 0)
233                 printf("/");
234 }
235
236 static int ask(const char *string, int def)
237 {
238         int c;
239
240         if (!repair) {
241                 printf("\n");
242                 errors_uncorrected = 1;
243                 return 0;
244         }
245         if (automatic) {
246                 printf("\n");
247                 if (!def)
248                         errors_uncorrected = 1;
249                 return def;
250         }
251         printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
252         for (;;) {
253                 fflush(stdout);
254                 if ((c = getchar()) == EOF) {
255                         if (!def)
256                                 errors_uncorrected = 1;
257                         return def;
258                 }
259                 c = toupper(c);
260                 if (c == 'Y') {
261                         def = 1;
262                         break;
263                 } else if (c == 'N') {
264                         def = 0;
265                         break;
266                 } else if (c == ' ' || c == '\n')
267                         break;
268         }
269         if (def)
270                 printf("y\n");
271         else {
272                 printf("n\n");
273                 errors_uncorrected = 1;
274         }
275         return def;
276 }
277
278 /*
279  * Make certain that we aren't checking a filesystem that is on a
280  * mounted partition.  Code adapted from e2fsck, Copyright (C) 1993,
281  * 1994 Theodore Ts'o.  Also licensed under GPL.
282  */
283 static void check_mount(void)
284 {
285         FILE *f;
286         struct mntent *mnt;
287         int cont;
288         int fd;
289
290         if ((f = setmntent(MOUNTED, "r")) == NULL)
291                 return;
292         while ((mnt = getmntent(f)) != NULL)
293                 if (strcmp(device_name, mnt->mnt_fsname) == 0)
294                         break;
295         endmntent(f);
296         if (!mnt)
297                 return;
298
299         /*
300          * If the root is mounted read-only, then /etc/mtab is
301          * probably not correct; so we won't issue a warning based on
302          * it.
303          */
304         fd = open(MOUNTED, O_RDWR);
305         if (fd < 0 && errno == EROFS)
306                 return;
307         else
308                 close(fd);
309
310         printf("%s is mounted.   ", device_name);
311         if (isatty(0) && isatty(1))
312                 cont = ask("Do you really want to continue", 0);
313         else
314                 cont = 0;
315         if (!cont) {
316                 printf("check aborted.\n");
317                 exit(0);
318         }
319         return;
320 }
321
322 /*
323  * check_zone_nr checks to see that *nr is a valid zone nr. If it
324  * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
325  * if an error was corrected, and returns the zone (0 for no zone
326  * or a bad zone-number).
327  */
328 static int check_zone_nr(unsigned short *nr, int *corrected)
329 {
330         if (!*nr)
331                 return 0;
332         if (*nr < FIRSTZONE)
333                 printf("Zone nr < FIRSTZONE in file `");
334         else if (*nr >= ZONES)
335                 printf("Zone nr >= ZONES in file `");
336         else
337                 return *nr;
338         print_current_name();
339         printf("'.");
340         if (ask("Remove block", 1)) {
341                 *nr = 0;
342                 *corrected = 1;
343         }
344         return 0;
345 }
346
347 #ifdef HAVE_MINIX2
348 static int check_zone_nr2(unsigned int *nr, int *corrected)
349 {
350         if (!*nr)
351                 return 0;
352         if (*nr < FIRSTZONE)
353                 printf("Zone nr < FIRSTZONE in file `");
354         else if (*nr >= ZONES)
355                 printf("Zone nr >= ZONES in file `");
356         else
357                 return *nr;
358         print_current_name();
359         printf("'.");
360         if (ask("Remove block", 1)) {
361                 *nr = 0;
362                 *corrected = 1;
363         }
364         return 0;
365 }
366 #endif
367
368 /*
369  * read-block reads block nr into the buffer at addr.
370  */
371 static void read_block(unsigned int nr, char *addr)
372 {
373         if (!nr) {
374                 memset(addr, 0, BLOCK_SIZE);
375                 return;
376         }
377         if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
378                 printf("Read error: unable to seek to block in file '");
379                 print_current_name();
380                 printf("'\n");
381                 memset(addr, 0, BLOCK_SIZE);
382                 errors_uncorrected = 1;
383         } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
384                 printf("Read error: bad block in file '");
385                 print_current_name();
386                 printf("'\n");
387                 memset(addr, 0, BLOCK_SIZE);
388                 errors_uncorrected = 1;
389         }
390 }
391
392 /*
393  * write_block writes block nr to disk.
394  */
395 static void write_block(unsigned int nr, char *addr)
396 {
397         if (!nr)
398                 return;
399         if (nr < FIRSTZONE || nr >= ZONES) {
400                 printf("Internal error: trying to write bad block\n"
401                            "Write request ignored\n");
402                 errors_uncorrected = 1;
403                 return;
404         }
405         if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
406                 die("seek failed in write_block");
407         if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
408                 printf("Write error: bad block in file '");
409                 print_current_name();
410                 printf("'\n");
411                 errors_uncorrected = 1;
412         }
413 }
414
415 /*
416  * map-block calculates the absolute block nr of a block in a file.
417  * It sets 'changed' if the inode has needed changing, and re-writes
418  * any indirect blocks with errors.
419  */
420 static int map_block(struct minix_inode *inode, unsigned int blknr)
421 {
422         unsigned short ind[BLOCK_SIZE >> 1];
423         unsigned short dind[BLOCK_SIZE >> 1];
424         int blk_chg, block, result;
425
426         if (blknr < 7)
427                 return check_zone_nr(inode->i_zone + blknr, &changed);
428         blknr -= 7;
429         if (blknr < 512) {
430                 block = check_zone_nr(inode->i_zone + 7, &changed);
431                 read_block(block, (char *) ind);
432                 blk_chg = 0;
433                 result = check_zone_nr(blknr + ind, &blk_chg);
434                 if (blk_chg)
435                         write_block(block, (char *) ind);
436                 return result;
437         }
438         blknr -= 512;
439         block = check_zone_nr(inode->i_zone + 8, &changed);
440         read_block(block, (char *) dind);
441         blk_chg = 0;
442         result = check_zone_nr(dind + (blknr / 512), &blk_chg);
443         if (blk_chg)
444                 write_block(block, (char *) dind);
445         block = result;
446         read_block(block, (char *) ind);
447         blk_chg = 0;
448         result = check_zone_nr(ind + (blknr % 512), &blk_chg);
449         if (blk_chg)
450                 write_block(block, (char *) ind);
451         return result;
452 }
453
454 #ifdef HAVE_MINIX2
455 static int map_block2(struct minix2_inode *inode, unsigned int blknr)
456 {
457         unsigned int ind[BLOCK_SIZE >> 2];
458         unsigned int dind[BLOCK_SIZE >> 2];
459         unsigned int tind[BLOCK_SIZE >> 2];
460         int blk_chg, block, result;
461
462         if (blknr < 7)
463                 return check_zone_nr2(inode->i_zone + blknr, &changed);
464         blknr -= 7;
465         if (blknr < 256) {
466                 block = check_zone_nr2(inode->i_zone + 7, &changed);
467                 read_block(block, (char *) ind);
468                 blk_chg = 0;
469                 result = check_zone_nr2(blknr + ind, &blk_chg);
470                 if (blk_chg)
471                         write_block(block, (char *) ind);
472                 return result;
473         }
474         blknr -= 256;
475         if (blknr >= 256 * 256) {
476                 block = check_zone_nr2(inode->i_zone + 8, &changed);
477                 read_block(block, (char *) dind);
478                 blk_chg = 0;
479                 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
480                 if (blk_chg)
481                         write_block(block, (char *) dind);
482                 block = result;
483                 read_block(block, (char *) ind);
484                 blk_chg = 0;
485                 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
486                 if (blk_chg)
487                         write_block(block, (char *) ind);
488                 return result;
489         }
490         blknr -= 256 * 256;
491         block = check_zone_nr2(inode->i_zone + 9, &changed);
492         read_block(block, (char *) tind);
493         blk_chg = 0;
494         result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
495         if (blk_chg)
496                 write_block(block, (char *) tind);
497         block = result;
498         read_block(block, (char *) dind);
499         blk_chg = 0;
500         result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
501         if (blk_chg)
502                 write_block(block, (char *) dind);
503         block = result;
504         read_block(block, (char *) ind);
505         blk_chg = 0;
506         result = check_zone_nr2(ind + blknr % 256, &blk_chg);
507         if (blk_chg)
508                 write_block(block, (char *) ind);
509         return result;
510 }
511 #endif
512
513 static void write_super_block(void)
514 {
515         /*
516          * Set the state of the filesystem based on whether or not there
517          * are uncorrected errors.  The filesystem valid flag is
518          * unconditionally set if we get this far.
519          */
520         Super.s_state |= MINIX_VALID_FS;
521         if (errors_uncorrected)
522                 Super.s_state |= MINIX_ERROR_FS;
523         else
524                 Super.s_state &= ~MINIX_ERROR_FS;
525
526         if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
527                 die("seek failed in write_super_block");
528         if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
529                 die("unable to write super-block");
530
531         return;
532 }
533
534 static void write_tables(void)
535 {
536         write_super_block();
537
538         if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
539                 die("Unable to write inode map");
540         if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
541                 die("Unable to write zone map");
542         if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
543                 die("Unable to write inodes");
544 }
545
546 static void get_dirsize(void)
547 {
548         int block;
549         char blk[BLOCK_SIZE];
550         int size;
551
552 #if HAVE_MINIX2
553         if (version2)
554                 block = Inode2[ROOT_INO].i_zone[0];
555         else
556 #endif
557                 block = Inode[ROOT_INO].i_zone[0];
558         read_block(block, blk);
559         for (size = 16; size < BLOCK_SIZE; size <<= 1) {
560                 if (strcmp(blk + size + 2, "..") == 0) {
561                         dirsize = size;
562                         namelen = size - 2;
563                         return;
564                 }
565         }
566         /* use defaults */
567 }
568
569 static void read_superblock(void)
570 {
571         if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
572                 die("seek failed");
573         if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
574                 die("unable to read super block");
575         if (MAGIC == MINIX_SUPER_MAGIC) {
576                 namelen = 14;
577                 dirsize = 16;
578                 version2 = 0;
579         } else if (MAGIC == MINIX_SUPER_MAGIC2) {
580                 namelen = 30;
581                 dirsize = 32;
582                 version2 = 0;
583 #ifdef HAVE_MINIX2
584         } else if (MAGIC == MINIX2_SUPER_MAGIC) {
585                 namelen = 14;
586                 dirsize = 16;
587                 version2 = 1;
588         } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
589                 namelen = 30;
590                 dirsize = 32;
591                 version2 = 1;
592 #endif
593         } else
594                 die("bad magic number in super-block");
595         if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
596                 die("Only 1k blocks/zones supported");
597         if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
598                 die("bad s_imap_blocks field in super-block");
599         if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
600                 die("bad s_zmap_blocks field in super-block");
601 }
602
603 static void read_tables(void)
604 {
605         inode_map = xmalloc(IMAPS * BLOCK_SIZE);
606         zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
607         memset(inode_map, 0, sizeof(inode_map));
608         memset(zone_map, 0, sizeof(zone_map));
609         inode_buffer = xmalloc(INODE_BUFFER_SIZE);
610         inode_count = xmalloc(INODES + 1);
611         zone_count = xmalloc(ZONES);
612         if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
613                 die("Unable to read inode map");
614         if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
615                 die("Unable to read zone map");
616         if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
617                 die("Unable to read inodes");
618         if (NORM_FIRSTZONE != FIRSTZONE) {
619                 printf("Warning: Firstzone != Norm_firstzone\n");
620                 errors_uncorrected = 1;
621         }
622         get_dirsize();
623         if (show) {
624                 printf("%ld inodes\n", INODES);
625                 printf("%ld blocks\n", ZONES);
626                 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
627                 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
628                 printf("Maxsize=%ld\n", MAXSIZE);
629                 printf("Filesystem state=%d\n", Super.s_state);
630                 printf("namelen=%d\n\n", namelen);
631         }
632 }
633
634 struct minix_inode *get_inode(unsigned int nr)
635 {
636         struct minix_inode *inode;
637
638         if (!nr || nr > INODES)
639                 return NULL;
640         total++;
641         inode = Inode + nr;
642         if (!inode_count[nr]) {
643                 if (!inode_in_use(nr)) {
644                         printf("Inode %d marked not used, but used for file '", nr);
645                         print_current_name();
646                         printf("'\n");
647                         if (repair) {
648                                 if (ask("Mark in use", 1))
649                                         mark_inode(nr);
650                         } else {
651                                 errors_uncorrected = 1;
652                         }
653                 }
654                 if (S_ISDIR(inode->i_mode))
655                         directory++;
656                 else if (S_ISREG(inode->i_mode))
657                         regular++;
658                 else if (S_ISCHR(inode->i_mode))
659                         chardev++;
660                 else if (S_ISBLK(inode->i_mode))
661                         blockdev++;
662                 else if (S_ISLNK(inode->i_mode))
663                         symlinks++;
664                 else if (S_ISSOCK(inode->i_mode));
665                 else if (S_ISFIFO(inode->i_mode));
666                 else {
667                         print_current_name();
668                         printf(" has mode %05o\n", inode->i_mode);
669                 }
670
671         } else
672                 links++;
673         if (!++inode_count[nr]) {
674                 printf("Warning: inode count too big.\n");
675                 inode_count[nr]--;
676                 errors_uncorrected = 1;
677         }
678         return inode;
679 }
680
681 #ifdef HAVE_MINIX2
682 struct minix2_inode *get_inode2(unsigned int nr)
683 {
684         struct minix2_inode *inode;
685
686         if (!nr || nr > INODES)
687                 return NULL;
688         total++;
689         inode = Inode2 + nr;
690         if (!inode_count[nr]) {
691                 if (!inode_in_use(nr)) {
692                         printf("Inode %d marked not used, but used for file '", nr);
693                         print_current_name();
694                         printf("'\n");
695                         if (repair) {
696                                 if (ask("Mark in use", 1))
697                                         mark_inode(nr);
698                                 else
699                                         errors_uncorrected = 1;
700                         }
701                 }
702                 if (S_ISDIR(inode->i_mode))
703                         directory++;
704                 else if (S_ISREG(inode->i_mode))
705                         regular++;
706                 else if (S_ISCHR(inode->i_mode))
707                         chardev++;
708                 else if (S_ISBLK(inode->i_mode))
709                         blockdev++;
710                 else if (S_ISLNK(inode->i_mode))
711                         symlinks++;
712                 else if (S_ISSOCK(inode->i_mode));
713                 else if (S_ISFIFO(inode->i_mode));
714                 else {
715                         print_current_name();
716                         printf(" has mode %05o\n", inode->i_mode);
717                 }
718         } else
719                 links++;
720         if (!++inode_count[nr]) {
721                 printf("Warning: inode count too big.\n");
722                 inode_count[nr]--;
723                 errors_uncorrected = 1;
724         }
725         return inode;
726 }
727 #endif
728
729 static void check_root(void)
730 {
731         struct minix_inode *inode = Inode + ROOT_INO;
732
733         if (!inode || !S_ISDIR(inode->i_mode))
734                 die("root inode isn't a directory");
735 }
736
737 #ifdef HAVE_MINIX2
738 static void check_root2(void)
739 {
740         struct minix2_inode *inode = Inode2 + ROOT_INO;
741
742         if (!inode || !S_ISDIR(inode->i_mode))
743                 die("root inode isn't a directory");
744 }
745 #endif
746
747 static int add_zone(unsigned short *znr, int *corrected)
748 {
749         int result;
750         int block;
751
752         result = 0;
753         block = check_zone_nr(znr, corrected);
754         if (!block)
755                 return 0;
756         if (zone_count[block]) {
757                 printf("Block has been used before. Now in file `");
758                 print_current_name();
759                 printf("'.");
760                 if (ask("Clear", 1)) {
761                         *znr = 0;
762                         block = 0;
763                         *corrected = 1;
764                 }
765         }
766         if (!block)
767                 return 0;
768         if (!zone_in_use(block)) {
769                 printf("Block %d in file `", block);
770                 print_current_name();
771                 printf("' is marked not in use.");
772                 if (ask("Correct", 1))
773                         mark_zone(block);
774         }
775         if (!++zone_count[block])
776                 zone_count[block]--;
777         return block;
778 }
779
780 #ifdef HAVE_MINIX2
781 static int add_zone2(unsigned int *znr, int *corrected)
782 {
783         int result;
784         int block;
785
786         result = 0;
787         block = check_zone_nr2(znr, corrected);
788         if (!block)
789                 return 0;
790         if (zone_count[block]) {
791                 printf("Block has been used before. Now in file `");
792                 print_current_name();
793                 printf("'.");
794                 if (ask("Clear", 1)) {
795                         *znr = 0;
796                         block = 0;
797                         *corrected = 1;
798                 }
799         }
800         if (!block)
801                 return 0;
802         if (!zone_in_use(block)) {
803                 printf("Block %d in file `", block);
804                 print_current_name();
805                 printf("' is marked not in use.");
806                 if (ask("Correct", 1))
807                         mark_zone(block);
808         }
809         if (!++zone_count[block])
810                 zone_count[block]--;
811         return block;
812 }
813 #endif
814
815 static void add_zone_ind(unsigned short *znr, int *corrected)
816 {
817         static char blk[BLOCK_SIZE];
818         int i, chg_blk = 0;
819         int block;
820
821         block = add_zone(znr, corrected);
822         if (!block)
823                 return;
824         read_block(block, blk);
825         for (i = 0; i < (BLOCK_SIZE >> 1); i++)
826                 add_zone(i + (unsigned short *) blk, &chg_blk);
827         if (chg_blk)
828                 write_block(block, blk);
829 }
830
831 #ifdef HAVE_MINIX2
832 static void add_zone_ind2(unsigned int *znr, int *corrected)
833 {
834         static char blk[BLOCK_SIZE];
835         int i, chg_blk = 0;
836         int block;
837
838         block = add_zone2(znr, corrected);
839         if (!block)
840                 return;
841         read_block(block, blk);
842         for (i = 0; i < BLOCK_SIZE >> 2; i++)
843                 add_zone2(i + (unsigned int *) blk, &chg_blk);
844         if (chg_blk)
845                 write_block(block, blk);
846 }
847 #endif
848
849 static void add_zone_dind(unsigned short *znr, int *corrected)
850 {
851         static char blk[BLOCK_SIZE];
852         int i, blk_chg = 0;
853         int block;
854
855         block = add_zone(znr, corrected);
856         if (!block)
857                 return;
858         read_block(block, blk);
859         for (i = 0; i < (BLOCK_SIZE >> 1); i++)
860                 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
861         if (blk_chg)
862                 write_block(block, blk);
863 }
864
865 #ifdef HAVE_MINIX2
866 static void add_zone_dind2(unsigned int *znr, int *corrected)
867 {
868         static char blk[BLOCK_SIZE];
869         int i, blk_chg = 0;
870         int block;
871
872         block = add_zone2(znr, corrected);
873         if (!block)
874                 return;
875         read_block(block, blk);
876         for (i = 0; i < BLOCK_SIZE >> 2; i++)
877                 add_zone_ind2(i + (unsigned int *) blk, &blk_chg);
878         if (blk_chg)
879                 write_block(block, blk);
880 }
881
882 static void add_zone_tind2(unsigned int *znr, int *corrected)
883 {
884         static char blk[BLOCK_SIZE];
885         int i, blk_chg = 0;
886         int block;
887
888         block = add_zone2(znr, corrected);
889         if (!block)
890                 return;
891         read_block(block, blk);
892         for (i = 0; i < BLOCK_SIZE >> 2; i++)
893                 add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
894         if (blk_chg)
895                 write_block(block, blk);
896 }
897 #endif
898
899 static void check_zones(unsigned int i)
900 {
901         struct minix_inode *inode;
902
903         if (!i || i > INODES)
904                 return;
905         if (inode_count[i] > 1)         /* have we counted this file already? */
906                 return;
907         inode = Inode + i;
908         if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
909                 !S_ISLNK(inode->i_mode)) return;
910         for (i = 0; i < 7; i++)
911                 add_zone(i + inode->i_zone, &changed);
912         add_zone_ind(7 + inode->i_zone, &changed);
913         add_zone_dind(8 + inode->i_zone, &changed);
914 }
915
916 #ifdef HAVE_MINIX2
917 static void check_zones2(unsigned int i)
918 {
919         struct minix2_inode *inode;
920
921         if (!i || i > INODES)
922                 return;
923         if (inode_count[i] > 1)         /* have we counted this file already? */
924                 return;
925         inode = Inode2 + i;
926         if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
927                 && !S_ISLNK(inode->i_mode))
928                 return;
929         for (i = 0; i < 7; i++)
930                 add_zone2(i + inode->i_zone, &changed);
931         add_zone_ind2(7 + inode->i_zone, &changed);
932         add_zone_dind2(8 + inode->i_zone, &changed);
933         add_zone_tind2(9 + inode->i_zone, &changed);
934 }
935 #endif
936
937 static void check_file(struct minix_inode *dir, unsigned int offset)
938 {
939         static char blk[BLOCK_SIZE];
940         struct minix_inode *inode;
941         int ino;
942         char *name;
943         int block;
944
945         block = map_block(dir, offset / BLOCK_SIZE);
946         read_block(block, blk);
947         name = blk + (offset % BLOCK_SIZE) + 2;
948         ino = *(unsigned short *) (name - 2);
949         if (ino > INODES) {
950                 print_current_name();
951                 printf(" contains a bad inode number for file '");
952                 printf("%.*s'.", namelen, name);
953                 if (ask(" Remove", 1)) {
954                         *(unsigned short *) (name - 2) = 0;
955                         write_block(block, blk);
956                 }
957                 ino = 0;
958         }
959         if (name_depth < MAX_DEPTH)
960                 strncpy(name_list[name_depth], name, namelen);
961         name_depth++;
962         inode = get_inode(ino);
963         name_depth--;
964         if (!offset) {
965                 if (!inode || strcmp(".", name)) {
966                         print_current_name();
967                         printf(": bad directory: '.' isn't first\n");
968                         errors_uncorrected = 1;
969                 } else
970                         return;
971         }
972         if (offset == dirsize) {
973                 if (!inode || strcmp("..", name)) {
974                         print_current_name();
975                         printf(": bad directory: '..' isn't second\n");
976                         errors_uncorrected = 1;
977                 } else
978                         return;
979         }
980         if (!inode)
981                 return;
982         if (name_depth < MAX_DEPTH)
983                 strncpy(name_list[name_depth], name, namelen);
984         name_depth++;
985         if (list) {
986                 if (verbose)
987                         printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
988                 print_current_name();
989                 if (S_ISDIR(inode->i_mode))
990                         printf(":\n");
991                 else
992                         printf("\n");
993         }
994         check_zones(ino);
995         if (inode && S_ISDIR(inode->i_mode))
996                 recursive_check(ino);
997         name_depth--;
998         return;
999 }
1000
1001 #ifdef HAVE_MINIX2
1002 static void check_file2(struct minix2_inode *dir, unsigned int offset)
1003 {
1004         static char blk[BLOCK_SIZE];
1005         struct minix2_inode *inode;
1006         int ino;
1007         char *name;
1008         int block;
1009
1010         block = map_block2(dir, offset / BLOCK_SIZE);
1011         read_block(block, blk);
1012         name = blk + (offset % BLOCK_SIZE) + 2;
1013         ino = *(unsigned short *) (name - 2);
1014         if (ino > INODES) {
1015                 print_current_name();
1016                 printf(" contains a bad inode number for file '");
1017                 printf("%.*s'.", namelen, name);
1018                 if (ask(" Remove", 1)) {
1019                         *(unsigned short *) (name - 2) = 0;
1020                         write_block(block, blk);
1021                 }
1022                 ino = 0;
1023         }
1024         if (name_depth < MAX_DEPTH)
1025                 strncpy(name_list[name_depth], name, namelen);
1026         name_depth++;
1027         inode = get_inode2(ino);
1028         name_depth--;
1029         if (!offset) {
1030                 if (!inode || strcmp(".", name)) {
1031                         print_current_name();
1032                         printf(": bad directory: '.' isn't first\n");
1033                         errors_uncorrected = 1;
1034                 } else
1035                         return;
1036         }
1037         if (offset == dirsize) {
1038                 if (!inode || strcmp("..", name)) {
1039                         print_current_name();
1040                         printf(": bad directory: '..' isn't second\n");
1041                         errors_uncorrected = 1;
1042                 } else
1043                         return;
1044         }
1045         if (!inode)
1046                 return;
1047         name_depth++;
1048         if (list) {
1049                 if (verbose)
1050                         printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1051                 print_current_name();
1052                 if (S_ISDIR(inode->i_mode))
1053                         printf(":\n");
1054                 else
1055                         printf("\n");
1056         }
1057         check_zones2(ino);
1058         if (inode && S_ISDIR(inode->i_mode))
1059                 recursive_check2(ino);
1060         name_depth--;
1061         return;
1062 }
1063 #endif
1064
1065 static void recursive_check(unsigned int ino)
1066 {
1067         struct minix_inode *dir;
1068         unsigned int offset;
1069
1070         dir = Inode + ino;
1071         if (!S_ISDIR(dir->i_mode))
1072                 die("internal error");
1073         if (dir->i_size < 2 * dirsize) {
1074                 print_current_name();
1075                 printf(": bad directory: size<32");
1076                 errors_uncorrected = 1;
1077         }
1078         for (offset = 0; offset < dir->i_size; offset += dirsize)
1079                 check_file(dir, offset);
1080 }
1081
1082 #ifdef HAVE_MINIX2
1083 static void recursive_check2(unsigned int ino)
1084 {
1085         struct minix2_inode *dir;
1086         unsigned int offset;
1087
1088         dir = Inode2 + ino;
1089         if (!S_ISDIR(dir->i_mode))
1090                 die("internal error");
1091         if (dir->i_size < 2 * dirsize) {
1092                 print_current_name();
1093                 printf(": bad directory: size < 32");
1094                 errors_uncorrected = 1;
1095         }
1096         for (offset = 0; offset < dir->i_size; offset += dirsize)
1097                 check_file2(dir, offset);
1098 }
1099 #endif
1100
1101 static int bad_zone(int i)
1102 {
1103         char buffer[1024];
1104
1105         if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
1106                 die("seek failed in bad_zone");
1107         return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1108 }
1109
1110 static void check_counts(void)
1111 {
1112         int i;
1113
1114         for (i = 1; i <= INODES; i++) {
1115                 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
1116                         printf("Inode %d mode not cleared.", i);
1117                         if (ask("Clear", 1)) {
1118                                 Inode[i].i_mode = 0;
1119                                 changed = 1;
1120                         }
1121                 }
1122                 if (!inode_count[i]) {
1123                         if (!inode_in_use(i))
1124                                 continue;
1125                         printf("Inode %d not used, marked used in the bitmap.", i);
1126                         if (ask("Clear", 1))
1127                                 unmark_inode(i);
1128                         continue;
1129                 }
1130                 if (!inode_in_use(i)) {
1131                         printf("Inode %d used, marked unused in the bitmap.", i);
1132                         if (ask("Set", 1))
1133                                 mark_inode(i);
1134                 }
1135                 if (Inode[i].i_nlinks != inode_count[i]) {
1136                         printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1137                                    i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1138                         if (ask("Set i_nlinks to count", 1)) {
1139                                 Inode[i].i_nlinks = inode_count[i];
1140                                 changed = 1;
1141                         }
1142                 }
1143         }
1144         for (i = FIRSTZONE; i < ZONES; i++) {
1145                 if (zone_in_use(i) == zone_count[i])
1146                         continue;
1147                 if (!zone_count[i]) {
1148                         if (bad_zone(i))
1149                                 continue;
1150                         printf("Zone %d: marked in use, no file uses it.", i);
1151                         if (ask("Unmark", 1))
1152                                 unmark_zone(i);
1153                         continue;
1154                 }
1155                 printf("Zone %d: %sin use, counted=%d\n",
1156                            i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1157         }
1158 }
1159
1160 #ifdef HAVE_MINIX2
1161 static void check_counts2(void)
1162 {
1163         int i;
1164
1165         for (i = 1; i <= INODES; i++) {
1166                 if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
1167                         printf("Inode %d mode not cleared.", i);
1168                         if (ask("Clear", 1)) {
1169                                 Inode2[i].i_mode = 0;
1170                                 changed = 1;
1171                         }
1172                 }
1173                 if (!inode_count[i]) {
1174                         if (!inode_in_use(i))
1175                                 continue;
1176                         printf("Inode %d not used, marked used in the bitmap.", i);
1177                         if (ask("Clear", 1))
1178                                 unmark_inode(i);
1179                         continue;
1180                 }
1181                 if (!inode_in_use(i)) {
1182                         printf("Inode %d used, marked unused in the bitmap.", i);
1183                         if (ask("Set", 1))
1184                                 mark_inode(i);
1185                 }
1186                 if (Inode2[i].i_nlinks != inode_count[i]) {
1187                         printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1188                                    i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1189                                    inode_count[i]);
1190                         if (ask("Set i_nlinks to count", 1)) {
1191                                 Inode2[i].i_nlinks = inode_count[i];
1192                                 changed = 1;
1193                         }
1194                 }
1195         }
1196         for (i = FIRSTZONE; i < ZONES; i++) {
1197                 if (zone_in_use(i) == zone_count[i])
1198                         continue;
1199                 if (!zone_count[i]) {
1200                         if (bad_zone(i))
1201                                 continue;
1202                         printf("Zone %d: marked in use, no file uses it.", i);
1203                         if (ask("Unmark", 1))
1204                                 unmark_zone(i);
1205                         continue;
1206                 }
1207                 printf("Zone %d: %sin use, counted=%d\n",
1208                            i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1209         }
1210 }
1211 #endif
1212
1213 static void check(void)
1214 {
1215         memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1216         memset(zone_count, 0, ZONES * sizeof(*zone_count));
1217         check_zones(ROOT_INO);
1218         recursive_check(ROOT_INO);
1219         check_counts();
1220 }
1221
1222 #ifdef HAVE_MINIX2
1223 static void check2(void)
1224 {
1225         memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1226         memset(zone_count, 0, ZONES * sizeof(*zone_count));
1227         check_zones2(ROOT_INO);
1228         recursive_check2(ROOT_INO);
1229         check_counts2();
1230 }
1231 #endif
1232
1233 /* Wed Feb  9 15:17:06 MST 2000 */
1234 /* dynamically allocate name_list (instead of making it static) */
1235 static void alloc_name_list(void)
1236 {
1237         int i;
1238
1239         name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
1240         for (i = 0; i < MAX_DEPTH; i++)
1241                 name_list[i] = xmalloc(sizeof(char) * BUFSIZ + 1);
1242 }
1243
1244 #if 0
1245 /* execute this atexit() to deallocate name_list[] */
1246 /* piptigger was here */
1247 static void free_name_list(void)
1248 {
1249         int i;
1250
1251         if (name_list) { 
1252                 for (i = 0; i < MAX_DEPTH; i++) {
1253                         if (name_list[i]) {
1254                                 free(name_list[i]);
1255                         }
1256                 }
1257                 free(name_list);
1258         }
1259 }
1260 #endif
1261
1262 extern int fsck_minix_main(int argc, char **argv)
1263 {
1264         struct termios tmp;
1265         int count;
1266         int retcode = 0;
1267
1268         alloc_name_list();
1269         /* Don't bother to free memory.  Exit does
1270          * that automagically, so we can save a few bytes */
1271         //atexit(free_name_list);
1272
1273         if (argc && *argv)
1274                 program_name = *argv;
1275         if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
1276                 die("bad inode size");
1277 #ifdef HAVE_MINIX2
1278         if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1279                 die("bad v2 inode size");
1280 #endif
1281         while (argc-- > 1) {
1282                 argv++;
1283                 if (argv[0][0] != '-') {
1284                         if (device_name)
1285                                 show_usage();
1286                         else
1287                                 device_name = argv[0];
1288                 } else
1289                         while (*++argv[0])
1290                                 switch (argv[0][0]) {
1291                                 case 'l':
1292                                         list = 1;
1293                                         break;
1294                                 case 'a':
1295                                         automatic = 1;
1296                                         repair = 1;
1297                                         break;
1298                                 case 'r':
1299                                         automatic = 0;
1300                                         repair = 1;
1301                                         break;
1302                                 case 'v':
1303                                         verbose = 1;
1304                                         break;
1305                                 case 's':
1306                                         show = 1;
1307                                         break;
1308                                 case 'm':
1309                                         warn_mode = 1;
1310                                         break;
1311                                 case 'f':
1312                                         force = 1;
1313                                         break;
1314                                 default:
1315                                         show_usage();
1316                                 }
1317         }
1318         if (!device_name)
1319                 show_usage();
1320         check_mount();                          /* trying to check a mounted filesystem? */
1321         if (repair && !automatic) {
1322                 if (!isatty(0) || !isatty(1))
1323                         die("need terminal for interactive repairs");
1324         }
1325         IN = open(device_name, repair ? O_RDWR : O_RDONLY);
1326         if (IN < 0)
1327                 die("unable to open '%s'");
1328         for (count = 0; count < 3; count++)
1329                 sync();
1330         read_superblock();
1331
1332         /*
1333          * Determine whether or not we should continue with the checking.
1334          * This is based on the status of the filesystem valid and error
1335          * flags and whether or not the -f switch was specified on the 
1336          * command line.
1337          */
1338         printf("%s, %s\n", program_name, program_version);
1339         if (!(Super.s_state & MINIX_ERROR_FS) &&
1340                 (Super.s_state & MINIX_VALID_FS) && !force) {
1341                 if (repair)
1342                         printf("%s is clean, no check.\n", device_name);
1343                 return retcode;
1344         } else if (force)
1345                 printf("Forcing filesystem check on %s.\n", device_name);
1346         else if (repair)
1347                 printf("Filesystem on %s is dirty, needs checking.\n",
1348                            device_name);
1349
1350         read_tables();
1351
1352         if (repair && !automatic) {
1353                 tcgetattr(0, &termios);
1354                 tmp = termios;
1355                 tmp.c_lflag &= ~(ICANON | ECHO);
1356                 tcsetattr(0, TCSANOW, &tmp);
1357                 termios_set = 1;
1358         }
1359 #if HAVE_MINIX2
1360         if (version2) {
1361                 check_root2();
1362                 check2();
1363         } else
1364 #endif
1365         {
1366                 check_root();
1367                 check();
1368         }
1369         if (verbose) {
1370                 int i, free;
1371
1372                 for (i = 1, free = 0; i <= INODES; i++)
1373                         if (!inode_in_use(i))
1374                                 free++;
1375                 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free),
1376                            100 * (INODES - free) / INODES);
1377                 for (i = FIRSTZONE, free = 0; i < ZONES; i++)
1378                         if (!zone_in_use(i))
1379                                 free++;
1380                 printf("%6ld zones used (%ld%%)\n", (ZONES - free),
1381                            100 * (ZONES - free) / ZONES);
1382                 printf("\n%6d regular files\n"
1383                            "%6d directories\n"
1384                            "%6d character device files\n"
1385                            "%6d block device files\n"
1386                            "%6d links\n"
1387                            "%6d symbolic links\n"
1388                            "------\n"
1389                            "%6d files\n",
1390                            regular, directory, chardev, blockdev,
1391                            links - 2 * directory + 1, symlinks,
1392                            total - 2 * directory + 1);
1393         }
1394         if (changed) {
1395                 write_tables();
1396                 printf("----------------------------\n"
1397                            "FILE SYSTEM HAS BEEN CHANGED\n"
1398                            "----------------------------\n");
1399                 for (count = 0; count < 3; count++)
1400                         sync();
1401         } else if (repair)
1402                 write_super_block();
1403
1404         if (repair && !automatic)
1405                 tcsetattr(0, TCSANOW, &termios);
1406
1407         if (changed)
1408                 retcode += 3;
1409         if (errors_uncorrected)
1410                 retcode += 4;
1411         return retcode;
1412 }