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