just include fcntl.h not sys/fcntl.h
[oweals/busybox.git] / util-linux / mkfs_minix.c
index 767f998cc4fd63d5695cab1139382f25f311643d..d9388b1d7ef05cd37661a9783d2111079d2400d8 100644 (file)
@@ -35,7 +35,7 @@
  *
  * 30.10.94 - added support for v2 filesystem
  *           (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
- * 
+ *
  * 09.11.94  - Added test to prevent overwrite of mounted fs adapted
  *             from Theodore Ts'o's (tytso@athena.mit.edu) mke2fs
  *             program.  (Daniel Quinlan, quinlan@yggdrasil.com)
  *
  * Usage:  mkfs [-c | -l filename ] [-v] [-nXX] [-iXX] device [size-in-blocks]
  *
- *     -c for readablility checking (SLOW!)
+ *     -c for readability checking (SLOW!)
  *      -l for getting a list of bad blocks from a file.
  *     -n for namelength (currently the kernel only uses 14 or 30)
  *     -i for number of inodes
  *     -v for v2 filesystem
  *
  * The device may be a block device or a image of one, but this isn't
- * enforced (but it's not much fun on a character device :-). 
+ * enforced (but it's not much fun on a character device :-).
  *
  * Modified for BusyBox by Erik Andersen <andersen@debian.org> --
  *     removed getopt based parser and added a hand rolled one.
@@ -70,6 +70,7 @@
 #include <fcntl.h>
 #include <ctype.h>
 #include <stdlib.h>
+#include <stdint.h>
 #include <termios.h>
 #include <sys/ioctl.h>
 #include <sys/param.h>
  * Note the 8-bit gid and atime and ctime.
  */
 struct minix_inode {
-       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];
+       uint16_t i_mode;
+       uint16_t i_uid;
+       uint32_t i_size;
+       uint32_t i_time;
+       uint8_t  i_gid;
+       uint8_t  i_nlinks;
+       uint16_t i_zone[9];
 };
 
 /*
@@ -118,35 +119,35 @@ struct minix_inode {
  * now 16-bit. The inode is now 64 bytes instead of 32.
  */
 struct minix2_inode {
-       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];
+       uint16_t i_mode;
+       uint16_t i_nlinks;
+       uint16_t i_uid;
+       uint16_t i_gid;
+       uint32_t i_size;
+       uint32_t i_atime;
+       uint32_t i_mtime;
+       uint32_t i_ctime;
+       uint32_t i_zone[10];
 };
 
 /*
  * minix super-block data on disk
  */
 struct minix_super_block {
-       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;
+       uint16_t s_ninodes;
+       uint16_t s_nzones;
+       uint16_t s_imap_blocks;
+       uint16_t s_zmap_blocks;
+       uint16_t s_firstdatazone;
+       uint16_t s_log_zone_size;
+       uint32_t s_max_size;
+       uint16_t s_magic;
+       uint16_t s_state;
+       uint32_t s_zones;
 };
 
 struct minix_dir_entry {
-       u_int16_t inode;
+       uint16_t inode;
        char name[0];
 };
 
@@ -191,21 +192,21 @@ struct minix_dir_entry {
 
 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
 
-static char *device_name = NULL;
+static char *device_name;
 static int DEV = -1;
-static long BLOCKS = 0;
-static int check = 0;
-static int badblocks = 0;
+static uint32_t BLOCKS;
+static int check;
+static int badblocks;
 static int namelen = 30;               /* default (changed to 30, per Linus's
 
                                                                   suggestion, Sun Nov 21 08:05:07 1993) */
 static int dirsize = 32;
 static int magic = MINIX_SUPER_MAGIC2;
-static int version2 = 0;
+static int version2;
 
-static char root_block[BLOCK_SIZE] = "\0";
+static char root_block[BLOCK_SIZE];
 
-static char *inode_buffer = NULL;
+static char *inode_buffer;
 
 #define Inode (((struct minix_inode *) inode_buffer)-1)
 #ifdef CONFIG_FEATURE_MINIX2
@@ -215,17 +216,17 @@ static char super_block_buffer[BLOCK_SIZE];
 static char boot_block_buffer[512];
 
 #define Super (*(struct minix_super_block *)super_block_buffer)
-#define INODES ((unsigned long)Super.s_ninodes)
+#define INODES (Super.s_ninodes)
 #ifdef CONFIG_FEATURE_MINIX2
-#define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
+#define ZONES (version2 ? Super.s_zones : Super.s_nzones)
 #else
-#define ZONES ((unsigned long)(Super.s_nzones))
+#define ZONES (Super.s_nzones)
 #endif
-#define IMAPS ((unsigned long)Super.s_imap_blocks)
-#define ZMAPS ((unsigned long)Super.s_zmap_blocks)
-#define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
-#define ZONESIZE ((unsigned long)Super.s_log_zone_size)
-#define MAXSIZE ((unsigned long)Super.s_max_size)
+#define IMAPS (Super.s_imap_blocks)
+#define ZMAPS (Super.s_zmap_blocks)
+#define FIRSTZONE (Super.s_firstdatazone)
+#define ZONESIZE (Super.s_log_zone_size)
+#define MAXSIZE (Super.s_max_size)
 #define MAGIC (Super.s_magic)
 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
 
@@ -233,8 +234,8 @@ static char *inode_map;
 static char *zone_map;
 
 static unsigned short good_blocks_table[MAX_GOOD_BLOCKS];
-static int used_good_blocks = 0;
-static unsigned long req_nr_inodes = 0;
+static int used_good_blocks;
+static unsigned long req_nr_inodes;
 
 static inline int bit(char * a,unsigned int i)
 {
@@ -306,8 +307,7 @@ static inline int get_size(const char *file)
        int fd;
        long size;
 
-       if ((fd = open(file, O_RDWR)) < 0)
-               bb_perror_msg_and_die("%s", file);
+       fd = bb_xopen3(file, O_RDWR, 0);
        if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
                close(fd);
                return (size * 512);
@@ -543,7 +543,13 @@ static inline void setup_tables(void)
        MAGIC = magic;
        ZONESIZE = 0;
        MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024;
-       ZONES = BLOCKS;
+#ifdef CONFIG_FEATURE_MINIX2
+       if (version2) {
+               Super.s_zones =  BLOCKS;
+       } else
+#endif
+               Super.s_nzones = BLOCKS;
+
 /* some magic nrs: 1 inode / 3 blocks */
        if (req_nr_inodes == 0)
                inodes = BLOCKS / 3;
@@ -592,16 +598,16 @@ static inline void setup_tables(void)
                unmark_inode(i);
        inode_buffer = xmalloc(INODE_BUFFER_SIZE);
        memset(inode_buffer, 0, INODE_BUFFER_SIZE);
-       printf("%ld inodes\n", INODES);
-       printf("%ld blocks\n", ZONES);
-       printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
+       printf("%ld inodes\n", (long)INODES);
+       printf("%ld blocks\n", (long)ZONES);
+       printf("Firstdatazone=%ld (%ld)\n", (long)FIRSTZONE, (long)NORM_FIRSTZONE);
        printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
-       printf("Maxsize=%ld\n\n", MAXSIZE);
+       printf("Maxsize=%ld\n\n", (long)MAXSIZE);
 }
 
 /*
  * Perform a test of a block; return the number of
- * blocks readable/writeable.
+ * blocks readable/writable.
  */
 static inline long do_check(char *buffer, int try, unsigned int current_block)
 {
@@ -625,7 +631,7 @@ static inline long do_check(char *buffer, int try, unsigned int current_block)
        return got;
 }
 
-static unsigned int currently_testing = 0;
+static unsigned int currently_testing;
 
 static void alarm_intr(int alnum)
 {
@@ -687,7 +693,7 @@ static void get_list_blocks(char *filename)
                printf("one bad block\n");
 }
 
-extern int mkfs_minix_main(int argc, char **argv)
+int mkfs_minix_main(int argc, char **argv)
 {
        int i=1;
        char *tmp;
@@ -701,7 +707,7 @@ extern int mkfs_minix_main(int argc, char **argv)
        if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
                bb_error_msg_and_die("bad inode size");
 #endif
-       
+
        /* Parse options */
        argv++;
        while (--argc >= 0 && *argv && **argv) {
@@ -754,7 +760,7 @@ extern int mkfs_minix_main(int argc, char **argv)
                                                                magic = MINIX_SUPER_MAGIC;
                                                        else if (i == 30)
                                                                magic = MINIX_SUPER_MAGIC2;
-                                                       else 
+                                                       else
                                                                bb_show_usage();
                                                        namelen = i;
                                                        dirsize = i + 2;
@@ -814,9 +820,7 @@ goodbye:
        tmp += dirsize;
        *(short *) tmp = 2;
        strcpy(tmp + 2, ".badblocks");
-       DEV = open(device_name, O_RDWR);
-       if (DEV < 0)
-               bb_error_msg_and_die("unable to open %s", device_name);
+       DEV = bb_xopen3(device_name, O_RDWR, 0);
        if (fstat(DEV, &statbuf) < 0)
                bb_error_msg_and_die("unable to stat %s", device_name);
        if (!S_ISBLK(statbuf.st_mode))
@@ -840,6 +844,6 @@ goodbye:
        }
        mark_good_blocks();
        write_tables();
-       return( 0);
+       return 0;
 
 }