- don't free user-supplied string (via -e)
[oweals/busybox.git] / util-linux / fdisk.c
index 7f046edfb668b5df91ec14c0d721063c11a396f4..b1f0b65c636c9a57fc815344f83908c7a654bc1d 100644 (file)
@@ -75,20 +75,17 @@ struct partition {
        unsigned char head;             /* starting head */
        unsigned char sector;           /* starting sector */
        unsigned char cyl;              /* starting cylinder */
-       unsigned char sys_ind;          /* What partition type */
+       unsigned char sys_ind;          /* what partition type */
        unsigned char end_head;         /* end head */
        unsigned char end_sector;       /* end sector */
        unsigned char end_cyl;          /* end cylinder */
        unsigned char start4[4];        /* starting sector counting from 0 */
        unsigned char size4[4];         /* nr of sectors in partition */
-} ATTRIBUTE_PACKED;
+} PACKED;
 
-static const char unable_to_open[] ALIGN1 = "cannot open %s";
-static const char unable_to_read[] ALIGN1 = "cannot read from %s";
-static const char unable_to_seek[] ALIGN1 = "cannot seek on %s";
-static const char unable_to_write[] ALIGN1 = "cannot write to %s";
-static const char ioctl_error[] ALIGN1 = "BLKGETSIZE ioctl failed on %s";
-static void fdisk_fatal(const char *why) ATTRIBUTE_NORETURN;
+static const char unable_to_open[] ALIGN1 = "can't open %s";
+static const char unable_to_read[] ALIGN1 = "can't read from %s";
+static const char unable_to_seek[] ALIGN1 = "can't seek on %s";
 
 enum label_type {
        LABEL_DOS, LABEL_SUN, LABEL_SGI, LABEL_AIX, LABEL_OSF
@@ -320,7 +317,7 @@ struct globals {
        struct pte ptes[MAXIMUM_PARTS];
 };
 #define G (*ptr_to_globals)
-#define line_ptr        (G.line_ptr)
+#define line_ptr             (G.line_ptr            )
 #define disk_device          (G.disk_device         )
 #define g_partitions         (G.g_partitions        )
 #define units_per_sector     (G.units_per_sector    )
@@ -346,11 +343,11 @@ struct globals {
 #define kern_sectors            (G.kern_sectors           )
 #define extended_offset         (G.extended_offset        )
 #define total_number_of_sectors (G.total_number_of_sectors)
-#define listingbuf      (G.listingbuf)
-#define line_buffer     (G.line_buffer)
+#define listingbuf      (G.listingbuf     )
+#define line_buffer     (G.line_buffer    )
 #define partname_buffer (G.partname_buffer)
-#define MBRbuffer       (G.MBRbuffer)
-#define ptes            (G.ptes)
+#define MBRbuffer       (G.MBRbuffer      )
+#define ptes            (G.ptes           )
 #define INIT_G() do { \
        SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
        sector_size = DEFAULT_SECTOR_SIZE; \
@@ -369,7 +366,7 @@ static ullong bb_BLKGETSIZE_sectors(int fd)
        unsigned long longsectors;
 
        if (ioctl(fd, BLKGETSIZE64, &v64) == 0) {
-               /* got bytes, convert to 512 byte sectors */
+               /* Got bytes, convert to 512 byte sectors */
                return (v64 >> 9);
        }
        /* Needs temp of type long */
@@ -414,7 +411,7 @@ close_dev_fd(void)
 }
 
 #if ENABLE_FEATURE_FDISK_WRITABLE
-/* read line; return 0 or first printable char */
+/* Read line; return 0 or first printable char */
 static int
 read_line(const char *prompt)
 {
@@ -435,7 +432,7 @@ read_line(const char *prompt)
 #endif
 
 /*
- * return partition name - uses static storage
+ * Return partition name - uses static storage
  */
 static const char *
 partname(const char *dev, int pno, int lth)
@@ -520,7 +517,8 @@ write_part_table_flag(char *b)
 static char
 read_nonempty(const char *mesg)
 {
-       while (!read_line(mesg)) /* repeat */;
+       while (!read_line(mesg))
+               continue;
        return *line_ptr;
 }
 
@@ -554,6 +552,41 @@ read_hex(const char *const *sys)
 }
 #endif /* FEATURE_FDISK_WRITABLE */
 
+static void fdisk_fatal(const char *why)
+{
+       if (listing) {
+               close_dev_fd();
+               longjmp(listingbuf, 1);
+       }
+       bb_error_msg_and_die(why, disk_device);
+}
+
+static void
+seek_sector(ullong secno)
+{
+       secno *= sector_size;
+#if ENABLE_FDISK_SUPPORT_LARGE_DISKS
+       if (lseek64(dev_fd, (off64_t)secno, SEEK_SET) == (off64_t) -1)
+               fdisk_fatal(unable_to_seek);
+#else
+       if (secno > MAXINT(off_t)
+        || lseek(dev_fd, (off_t)secno, SEEK_SET) == (off_t) -1
+       ) {
+               fdisk_fatal(unable_to_seek);
+       }
+#endif
+}
+
+#if ENABLE_FEATURE_FDISK_WRITABLE
+static void
+write_sector(ullong secno, const void *buf)
+{
+       seek_sector(secno);
+       xwrite(dev_fd, buf, sector_size);
+}
+#endif
+
+
 #include "fdisk_aix.c"
 
 typedef struct {
@@ -642,6 +675,7 @@ STATIC_SUN void verify_sun(void);
 STATIC_SUN void sun_write_table(void);
 #include "fdisk_sun.c"
 
+
 #if ENABLE_FEATURE_FDISK_WRITABLE
 /* start_sect and nr_sects are stored little endian on all machines */
 /* moreover, they are not aligned correctly */
@@ -689,40 +723,6 @@ get_nr_sects(const struct partition *p)
        return read4_little_endian(p->size4);
 }
 
-static void fdisk_fatal(const char *why)
-{
-       if (listing) {
-               close_dev_fd();
-               longjmp(listingbuf, 1);
-       }
-       bb_error_msg_and_die(why, disk_device);
-}
-
-static void
-seek_sector(ullong secno)
-{
-       secno *= sector_size;
-#if ENABLE_FDISK_SUPPORT_LARGE_DISKS
-       if (lseek64(dev_fd, (off64_t)secno, SEEK_SET) == (off64_t) -1)
-               fdisk_fatal(unable_to_seek);
-#else
-       if (secno > MAXINT(off_t)
-        || lseek(dev_fd, (off_t)secno, SEEK_SET) == (off_t) -1
-       ) {
-               fdisk_fatal(unable_to_seek);
-       }
-#endif
-}
-
-#if ENABLE_FEATURE_FDISK_WRITABLE
-static void
-write_sector(ullong secno, char *buf)
-{
-       seek_sector(secno);
-       xwrite(dev_fd, buf, sector_size);
-}
-#endif
-
 /* Allocate a buffer and read a partition table sector */
 static void
 read_pte(struct pte *pe, ullong offset)
@@ -939,7 +939,8 @@ list_types(const char *const *sys)
        unsigned done, next, size;
        int i;
 
-       for (size = 0; sys[size]; size++) /* */;
+       for (size = 0; sys[size]; size++)
+               continue;
 
        done = 0;
        for (i = COLS-1; i >= 0; i--) {
@@ -2683,7 +2684,7 @@ is_ide_cdrom_or_tape(const char *device)
                return 0;
 
        snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
-       procf = fopen(buf, "r");
+       procf = fopen_for_read(buf);
        if (procf != NULL && fgets(buf, sizeof(buf), procf))
                is_ide = (!strncmp(buf, "cdrom", 5) ||
                          !strncmp(buf, "tape", 4));