Accomodate older glibc, which also lacks the module syscalls
[oweals/busybox.git] / fsck_minix.c
index 5807b9af5cd4dd8bd8e3728794dd4240e35e7bf5..952968d85696632859aa556f259a71b83d0269a1 100644 (file)
@@ -86,7 +86,6 @@
  * enforced (but it's not much fun on a character device :-). 
  */
 
-#include "internal.h"
 #include <stdio.h>
 #include <errno.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <termios.h>
 #include <mntent.h>
-#include <sys/stat.h>
 #include <sys/param.h>
+#include "busybox.h"
 
- typedef unsigned char u8;
-typedef unsigned short u16;
-typedef unsigned int u32;
+static const int MINIX_ROOT_INO = 1;
+static const int MINIX_LINK_MAX = 250;
+static const int MINIX2_LINK_MAX = 65530;
 
-
-#define MINIX_ROOT_INO 1
-#define MINIX_LINK_MAX 250
-#define MINIX2_LINK_MAX        65530
-
-#define MINIX_I_MAP_SLOTS      8
-#define MINIX_Z_MAP_SLOTS      64
-#define MINIX_SUPER_MAGIC      0x137F          /* original minix fs */
-#define MINIX_SUPER_MAGIC2     0x138F          /* minix fs, 30 char names */
-#define MINIX2_SUPER_MAGIC     0x2468          /* minix V2 fs */
-#define MINIX2_SUPER_MAGIC2    0x2478          /* minix V2 fs, 30 char names */
-#define MINIX_VALID_FS         0x0001          /* Clean fs. */
-#define MINIX_ERROR_FS         0x0002          /* fs has errors. */
+static const int MINIX_I_MAP_SLOTS = 8;
+static const int MINIX_Z_MAP_SLOTS = 64;
+static const int MINIX_SUPER_MAGIC = 0x137F;           /* original minix fs */
+static const int MINIX_SUPER_MAGIC2 = 0x138F;          /* minix fs, 30 char names */
+static const int MINIX2_SUPER_MAGIC = 0x2468;          /* minix V2 fs */
+static const int MINIX2_SUPER_MAGIC2 = 0x2478;         /* minix V2 fs, 30 char names */
+static const int MINIX_VALID_FS = 0x0001;              /* Clean fs. */
+static const int MINIX_ERROR_FS = 0x0002;              /* fs has errors. */
 
 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
 #define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
 
-#define MINIX_V1               0x0001          /* original minix fs */
-#define MINIX_V2               0x0002          /* minix V2 fs */
+static const int MINIX_V1 = 0x0001;            /* original minix fs */
+static const int MINIX_V2 = 0x0002;            /* minix V2 fs */
 
 #define INODE_VERSION(inode)   inode->i_sb->u.minix_sb.s_version
 
@@ -131,13 +124,13 @@ typedef unsigned int u32;
  * Note the 8-bit gid and atime and ctime.
  */
 struct minix_inode {
-       u16 i_mode;
-       u16 i_uid;
-       u32 i_size;
-       u32 i_time;
-       u8  i_gid;
-       u8  i_nlinks;
-       u16 i_zone[9];
+       u_int16_t i_mode;
+       u_int16_t i_uid;
+       u_int32_t i_size;
+       u_int32_t i_time;
+       u_int8_t  i_gid;
+       u_int8_t  i_nlinks;
+       u_int16_t i_zone[9];
 };
 
 /*
@@ -147,35 +140,35 @@ struct minix_inode {
  * now 16-bit. The inode is now 64 bytes instead of 32.
  */
 struct minix2_inode {
-       u16 i_mode;
-       u16 i_nlinks;
-       u16 i_uid;
-       u16 i_gid;
-       u32 i_size;
-       u32 i_atime;
-       u32 i_mtime;
-       u32 i_ctime;
-       u32 i_zone[10];
+       u_int16_t i_mode;
+       u_int16_t i_nlinks;
+       u_int16_t i_uid;
+       u_int16_t i_gid;
+       u_int32_t i_size;
+       u_int32_t i_atime;
+       u_int32_t i_mtime;
+       u_int32_t i_ctime;
+       u_int32_t i_zone[10];
 };
 
 /*
  * minix super-block data on disk
  */
 struct minix_super_block {
-       u16 s_ninodes;
-       u16 s_nzones;
-       u16 s_imap_blocks;
-       u16 s_zmap_blocks;
-       u16 s_firstdatazone;
-       u16 s_log_zone_size;
-       u32 s_max_size;
-       u16 s_magic;
-       u16 s_state;
-       u32 s_zones;
+       u_int16_t s_ninodes;
+       u_int16_t s_nzones;
+       u_int16_t s_imap_blocks;
+       u_int16_t s_zmap_blocks;
+       u_int16_t s_firstdatazone;
+       u_int16_t s_log_zone_size;
+       u_int32_t s_max_size;
+       u_int16_t s_magic;
+       u_int16_t s_state;
+       u_int32_t s_zones;
 };
 
 struct minix_dir_entry {
-       u16 inode;
+       u_int16_t inode;
        char name[0];
 };
 
@@ -186,29 +179,19 @@ struct minix_dir_entry {
 
 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
 
-#define MINIX_VALID_FS               0x0001          /* Clean fs. */
-#define MINIX_ERROR_FS               0x0002          /* fs has errors. */
-
-#define MINIX_SUPER_MAGIC    0x137F          /* original minix fs */
-#define MINIX_SUPER_MAGIC2   0x138F          /* minix fs, 30 char names */
-
 #ifndef BLKGETSIZE
 #define BLKGETSIZE _IO(0x12,96)    /* return device size */
 #endif
 
-#ifdef MINIX2_SUPER_MAGIC2
-#define HAVE_MINIX2 1
-#endif
-
 #ifndef __linux__
 #define volatile
 #endif
 
-#define ROOT_INO 1
+static const int ROOT_INO = 1;
 
 #define UPPER(size,n) ((size+((n)-1))/(n))
 #define INODE_SIZE (sizeof(struct minix_inode))
-#ifdef HAVE_MINIX2
+#ifdef BB_FEATURE_MINIX2
 #define INODE_SIZE2 (sizeof(struct minix2_inode))
 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
                                    : MINIX_INODES_PER_BLOCK))
@@ -236,7 +219,7 @@ static struct termios termios;
 static int termios_set = 0;
 
 /* File-name data */
-#define MAX_DEPTH 32
+static const int MAX_DEPTH = 32;
 static int name_depth = 0;
 // static char name_list[MAX_DEPTH][BUFSIZ + 1];
 static char **name_list = NULL;
@@ -249,7 +232,7 @@ static char super_block_buffer[BLOCK_SIZE];
 
 #define Super (*(struct minix_super_block *)super_block_buffer)
 #define INODES ((unsigned long)Super.s_ninodes)
-#ifdef HAVE_MINIX2
+#ifdef BB_FEATURE_MINIX2
 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
 #else
 #define ZONES ((unsigned long)(Super.s_nzones))
@@ -269,10 +252,16 @@ static unsigned char *inode_count = NULL;
 static unsigned char *zone_count = NULL;
 
 static void recursive_check(unsigned int ino);
+#ifdef BB_FEATURE_MINIX2
 static void recursive_check2(unsigned int ino);
+#endif
 
-#define inode_in_use(x) (isset(inode_map,(x)))
-#define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1))
+static inline int bit(char * a,unsigned int i)
+{
+         return (a[i >> 3] & (1<<(i & 7))) != 0;
+}
+#define inode_in_use(x) (bit(inode_map,(x)))
+#define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
 
 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
@@ -288,14 +277,9 @@ static void leave(int status)
        exit(status);
 }
 
-static void show_usage(void)
-{
-       usage(fsck_minix_usage);
-}
-
 static void die(const char *str)
 {
-       errorMsg("%s\n", str);
+       error_msg("%s", str);
        leave(8);
 }
 
@@ -424,7 +408,7 @@ static int check_zone_nr(unsigned short *nr, int *corrected)
        return 0;
 }
 
-#ifdef HAVE_MINIX2
+#ifdef BB_FEATURE_MINIX2
 static int check_zone_nr2(unsigned int *nr, int *corrected)
 {
        if (!*nr)
@@ -531,7 +515,7 @@ static int map_block(struct minix_inode *inode, unsigned int blknr)
        return result;
 }
 
-#ifdef HAVE_MINIX2
+#ifdef BB_FEATURE_MINIX2
 static int map_block2(struct minix2_inode *inode, unsigned int blknr)
 {
        unsigned int ind[BLOCK_SIZE >> 2];
@@ -629,7 +613,7 @@ static void get_dirsize(void)
        char blk[BLOCK_SIZE];
        int size;
 
-#if HAVE_MINIX2
+#ifdef BB_FEATURE_MINIX2
        if (version2)
                block = Inode2[ROOT_INO].i_zone[0];
        else
@@ -660,7 +644,7 @@ static void read_superblock(void)
                namelen = 30;
                dirsize = 32;
                version2 = 0;
-#ifdef HAVE_MINIX2
+#ifdef BB_FEATURE_MINIX2
        } else if (MAGIC == MINIX2_SUPER_MAGIC) {
                namelen = 14;
                dirsize = 16;
@@ -711,7 +695,7 @@ static void read_tables(void)
        }
 }
 
-struct minix_inode *get_inode(unsigned int nr)
+static struct minix_inode *get_inode(unsigned int nr)
 {
        struct minix_inode *inode;
 
@@ -758,8 +742,8 @@ struct minix_inode *get_inode(unsigned int nr)
        return inode;
 }
 
-#ifdef HAVE_MINIX2
-struct minix2_inode *get_inode2(unsigned int nr)
+#ifdef BB_FEATURE_MINIX2
+static struct minix2_inode *get_inode2(unsigned int nr)
 {
        struct minix2_inode *inode;
 
@@ -814,7 +798,7 @@ static void check_root(void)
                die("root inode isn't a directory");
 }
 
-#ifdef HAVE_MINIX2
+#ifdef BB_FEATURE_MINIX2
 static void check_root2(void)
 {
        struct minix2_inode *inode = Inode2 + ROOT_INO;
@@ -857,7 +841,7 @@ static int add_zone(unsigned short *znr, int *corrected)
        return block;
 }
 
-#ifdef HAVE_MINIX2
+#ifdef BB_FEATURE_MINIX2
 static int add_zone2(unsigned int *znr, int *corrected)
 {
        int result;
@@ -908,7 +892,7 @@ static void add_zone_ind(unsigned short *znr, int *corrected)
                write_block(block, blk);
 }
 
-#ifdef HAVE_MINIX2
+#ifdef BB_FEATURE_MINIX2
 static void add_zone_ind2(unsigned int *znr, int *corrected)
 {
        static char blk[BLOCK_SIZE];
@@ -942,7 +926,7 @@ static void add_zone_dind(unsigned short *znr, int *corrected)
                write_block(block, blk);
 }
 
-#ifdef HAVE_MINIX2
+#ifdef BB_FEATURE_MINIX2
 static void add_zone_dind2(unsigned int *znr, int *corrected)
 {
        static char blk[BLOCK_SIZE];
@@ -993,7 +977,7 @@ static void check_zones(unsigned int i)
        add_zone_dind(8 + inode->i_zone, &changed);
 }
 
-#ifdef HAVE_MINIX2
+#ifdef BB_FEATURE_MINIX2
 static void check_zones2(unsigned int i)
 {
        struct minix2_inode *inode;
@@ -1078,7 +1062,7 @@ static void check_file(struct minix_inode *dir, unsigned int offset)
        return;
 }
 
-#ifdef HAVE_MINIX2
+#ifdef BB_FEATURE_MINIX2
 static void check_file2(struct minix2_inode *dir, unsigned int offset)
 {
        static char blk[BLOCK_SIZE];
@@ -1159,7 +1143,7 @@ static void recursive_check(unsigned int ino)
                check_file(dir, offset);
 }
 
-#ifdef HAVE_MINIX2
+#ifdef BB_FEATURE_MINIX2
 static void recursive_check2(unsigned int ino)
 {
        struct minix2_inode *dir;
@@ -1237,7 +1221,7 @@ static void check_counts(void)
        }
 }
 
-#ifdef HAVE_MINIX2
+#ifdef BB_FEATURE_MINIX2
 static void check_counts2(void)
 {
        int i;
@@ -1299,7 +1283,7 @@ static void check(void)
        check_counts();
 }
 
-#ifdef HAVE_MINIX2
+#ifdef BB_FEATURE_MINIX2
 static void check2(void)
 {
        memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
@@ -1321,7 +1305,7 @@ static void alloc_name_list(void)
                name_list[i] = xmalloc(sizeof(char) * BUFSIZ + 1);
 }
 
-#if 0
+#ifdef BB_FEATURE_CLEAN_UP
 /* execute this atexit() to deallocate name_list[] */
 /* piptigger was here */
 static void free_name_list(void)
@@ -1346,13 +1330,15 @@ extern int fsck_minix_main(int argc, char **argv)
        int retcode = 0;
 
        alloc_name_list();
+#ifdef BB_FEATURE_CLEAN_UP
        /* Don't bother to free memory.  Exit does
         * that automagically, so we can save a few bytes */
-       //atexit(free_name_list);
+       atexit(free_name_list);
+#endif
 
        if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
                die("bad inode size");
-#ifdef HAVE_MINIX2
+#ifdef BB_FEATURE_MINIX2
        if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
                die("bad v2 inode size");
 #endif
@@ -1401,8 +1387,10 @@ extern int fsck_minix_main(int argc, char **argv)
                        die("need terminal for interactive repairs");
        }
        IN = open(device_name, repair ? O_RDWR : O_RDONLY);
-       if (IN < 0)
-               die("unable to open '%s'");
+       if (IN < 0){
+               fprintf(stderr,"unable to open device '%s'.\n",device_name);
+               leave(8);
+       }
        for (count = 0; count < 3; count++)
                sync();
        read_superblock();
@@ -1434,7 +1422,7 @@ extern int fsck_minix_main(int argc, char **argv)
                tcsetattr(0, TCSANOW, &tmp);
                termios_set = 1;
        }
-#if HAVE_MINIX2
+#ifdef BB_FEATURE_MINIX2
        if (version2) {
                check_root2();
                check2();
@@ -1445,18 +1433,18 @@ extern int fsck_minix_main(int argc, char **argv)
                check();
        }
        if (verbose) {
-               int i, free;
+               int i, free_cnt;
 
-               for (i = 1, free = 0; i <= INODES; i++)
+               for (i = 1, free_cnt = 0; i <= INODES; i++)
                        if (!inode_in_use(i))
-                               free++;
-               printf("\n%6ld inodes used (%ld%%)\n", (INODES - free),
-                          100 * (INODES - free) / INODES);
-               for (i = FIRSTZONE, free = 0; i < ZONES; i++)
+                               free_cnt++;
+               printf("\n%6ld inodes used (%ld%%)\n", (INODES - free_cnt),
+                          100 * (INODES - free_cnt) / INODES);
+               for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
                        if (!zone_in_use(i))
-                               free++;
-               printf("%6ld zones used (%ld%%)\n", (ZONES - free),
-                          100 * (ZONES - free) / ZONES);
+                               free_cnt++;
+               printf("%6ld zones used (%ld%%)\n", (ZONES - free_cnt),
+                          100 * (ZONES - free_cnt) / ZONES);
                printf("\n%6d regular files\n"
                           "%6d directories\n"
                           "%6d character device files\n"