- don't free user-supplied string (via -e)
[oweals/busybox.git] / util-linux / fdisk_osf.c
index dbb342886e6c0578413e4e9921f85264e1e503c2..c50ee9b1652111432c0814a8850f3b395a695b71 100644 (file)
@@ -149,7 +149,7 @@ struct xbsd_disklabel {
 #define BSD_DSTYPE_DOSPART(s)   ((s) & 3)       /* dos partition number */
 #define BSD_DSTYPE_GEOMETRY     0x10            /* drive params in label */
 
-static const char * const xbsd_dktypenames[] = {
+static const char *const xbsd_dktypenames[] = {
        "unknown",
        "SMD",
        "MSCP",
@@ -163,7 +163,7 @@ static const char * const xbsd_dktypenames[] = {
        "floppy",
        0
 };
-#define BSD_DKMAXTYPES  (sizeof(xbsd_dktypenames) / sizeof(xbsd_dktypenames[0]) - 1)
+
 
 /*
  * Filesystem type and version.
@@ -219,7 +219,6 @@ static const char *const xbsd_fstypes[] = {
        "\x10" "AdvFS",             /* BSD_FS_ADVFS   */
        NULL
 };
-#define BSD_FSMAXTYPES (SIZE(xbsd_fstypes)-1)
 
 
 /*
@@ -241,8 +240,6 @@ static const char *const xbsd_fstypes[] = {
    Also fixed unaligned accesses in alpha_bootblock_checksum()
 */
 
-static int possibly_osf_label;
-
 #define FREEBSD_PARTITION       0xa5
 #define NETBSD_PARTITION        0xa9
 
@@ -284,7 +281,7 @@ struct bsd_globals {
 static struct bsd_globals *bsd_globals_ptr;
 
 #define disklabelbuffer (bsd_globals_ptr->disklabelbuffer)
-#define xbsd_dlabel     (bsd_globals_ptr->xbsd_dlabel) 
+#define xbsd_dlabel     (bsd_globals_ptr->xbsd_dlabel)
 
 
 /* Code */
@@ -395,7 +392,7 @@ bsd_select(void)
 #endif
 
        while (1) {
-               putchar('\n');
+               bb_putchar('\n');
                switch (tolower(read_nonempty("BSD disklabel command (m for help): "))) {
                case 'd':
                        xbsd_delete_part();
@@ -416,7 +413,8 @@ bsd_select(void)
                        xbsd_print_disklabel(0);
                        break;
                case 'q':
-                       close(fd);
+                       if (ENABLE_FEATURE_CLEAN_UP)
+                               close_dev_fd();
                        exit(EXIT_SUCCESS);
                case 'r':
                        return;
@@ -504,25 +502,22 @@ xbsd_print_disklabel(int show_all)
        int i, j;
 
        if (show_all) {
+               static const int d_masks[] = { BSD_D_REMOVABLE, BSD_D_ECC, BSD_D_BADSECT };
+
 #if defined(__alpha__)
                printf("# %s:\n", disk_device);
 #else
                printf("# %s:\n", partname(disk_device, xbsd_part_index+1, 0));
 #endif
-               if ((unsigned) lp->d_type < BSD_DKMAXTYPES)
+               if ((unsigned) lp->d_type < ARRAY_SIZE(xbsd_dktypenames)-1)
                        printf("type: %s\n", xbsd_dktypenames[lp->d_type]);
                else
                        printf("type: %d\n", lp->d_type);
                printf("disk: %.*s\n", (int) sizeof(lp->d_typename), lp->d_typename);
                printf("label: %.*s\n", (int) sizeof(lp->d_packname), lp->d_packname);
-               printf("flags:");
-               if (lp->d_flags & BSD_D_REMOVABLE)
-                       printf(" removable");
-               if (lp->d_flags & BSD_D_ECC)
-                       printf(" ecc");
-               if (lp->d_flags & BSD_D_BADSECT)
-                       printf(" badsect");
-               puts("");
+               printf("flags: ");
+               print_flags_separated(d_masks, "removable\0""ecc\0""badsect\0", lp->d_flags, " ");
+               bb_putchar('\n');
                /* On various machines the fields of *lp are short/int/long */
                /* In order to avoid problems, we cast them all to long. */
                printf("bytes/sector: %ld\n", (long) lp->d_secsize);
@@ -571,7 +566,7 @@ xbsd_print_disklabel(int show_all)
                                );
                        }
 
-                       if ((unsigned) pp->p_fstype < BSD_FSMAXTYPES)
+                       if ((unsigned) pp->p_fstype < ARRAY_SIZE(xbsd_fstypes)-1)
                                printf("%8.8s", xbsd_fstypes[pp->p_fstype]);
                        else
                                printf("%8x", pp->p_fstype);
@@ -589,7 +584,7 @@ xbsd_print_disklabel(int show_all)
                                printf("%22.22s", "");
                                break;
                        }
-                       puts("");
+                       bb_putchar('\n');
                }
        }
 }
@@ -630,12 +625,13 @@ xbsd_create_disklabel(void)
 #else
                                xbsd_part
 #endif
-                               ) == 1) {
+                       ) == 1) {
                                xbsd_print_disklabel(1);
                                return 1;
-                       } else
-                               return 0;
-               } else if (c == 'n')
+                       }
+                       return 0;
+               }
+               if (c == 'n')
                        return 0;
        }
 }
@@ -692,13 +688,12 @@ xbsd_get_bootstrap(char *path, void *ptr, int size)
 {
        int fdb;
 
-       fdb = open(path, O_RDONLY);
+       fdb = open_or_warn(path, O_RDONLY);
        if (fdb < 0) {
-               perror(path);
                return 0;
        }
-       if (read(fdb, ptr, size) < 0) {
-               perror(path);
+       if (full_read(fdb, ptr, size) < 0) {
+               bb_simple_perror_msg(path);
                close(fdb);
                return 0;
        }
@@ -769,10 +764,8 @@ xbsd_write_bootstrap(void)
        sector = get_start_sect(xbsd_part);
 #endif
 
-       if (lseek(fd, sector * SECTOR_SIZE, SEEK_SET) == -1)
-               fdisk_fatal(unable_to_seek);
-       if (BSD_BBSIZE != write(fd, disklabelbuffer, BSD_BBSIZE))
-               fdisk_fatal(unable_to_write);
+       seek_sector(sector);
+       xwrite(dev_fd, disklabelbuffer, BSD_BBSIZE);
 
 #if defined(__alpha__)
        printf("Bootstrap installed on %s\n", disk_device);
@@ -877,10 +870,10 @@ xbsd_initlabel(struct partition *p)
        d->d_flags = 0;
 #endif
        d->d_secsize = SECTOR_SIZE;           /* bytes/sector  */
-       d->d_nsectors = sectors;              /* sectors/track */
-       d->d_ntracks = heads;                 /* tracks/cylinder (heads) */
-       d->d_ncylinders = cylinders;
-       d->d_secpercyl  = sectors * heads;    /* sectors/cylinder */
+       d->d_nsectors = g_sectors;            /* sectors/track */
+       d->d_ntracks = g_heads;               /* tracks/cylinder (heads) */
+       d->d_ncylinders = g_cylinders;
+       d->d_secpercyl  = g_sectors * g_heads;/* sectors/cylinder */
        if (d->d_secpercyl == 0)
                d->d_secpercyl = 1;           /* avoid segfaults */
        d->d_secperunit = d->d_secpercyl * d->d_ncylinders;
@@ -942,9 +935,8 @@ xbsd_readlabel(struct partition *p)
        sector = 0;
 #endif
 
-       if (lseek(fd, sector * SECTOR_SIZE, SEEK_SET) == -1)
-               fdisk_fatal(unable_to_seek);
-       if (BSD_BBSIZE != read(fd, disklabelbuffer, BSD_BBSIZE))
+       seek_sector(sector);
+       if (BSD_BBSIZE != full_read(dev_fd, disklabelbuffer, BSD_BBSIZE))
                fdisk_fatal(unable_to_read);
 
        memmove(d, &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
@@ -988,15 +980,12 @@ xbsd_writelabel(struct partition *p)
 
 #if defined(__alpha__) && BSD_LABELSECTOR == 0
        alpha_bootblock_checksum(disklabelbuffer);
-       if (lseek(fd, 0, SEEK_SET) == -1)
-               fdisk_fatal(unable_to_seek);
-       if (BSD_BBSIZE != write(fd, disklabelbuffer, BSD_BBSIZE))
-               fdisk_fatal(unable_to_write);
+       seek_sector(0);
+       xwrite(dev_fd, disklabelbuffer, BSD_BBSIZE);
 #else
-       if (lseek(fd, sector * SECTOR_SIZE + BSD_LABELOFFSET, SEEK_SET) == -1)
-               fdisk_fatal(unable_to_seek);
-       if (sizeof(struct xbsd_disklabel) != write(fd, d, sizeof(struct xbsd_disklabel)))
-               fdisk_fatal(unable_to_write);
+       seek_sector(sector);
+       lseek(dev_fd, BSD_LABELOFFSET, SEEK_CUR);
+       xwrite(dev_fd, d, sizeof(*d));
 #endif
        sync_disks();
        return 1;
@@ -1028,7 +1017,7 @@ xbsd_link_part(void)
        int k, i;
        struct partition *p;
 
-       k = get_partition(1, partitions);
+       k = get_partition(1, g_partitions);
 
        if (!xbsd_check_new_partition(&i))
                return;