Bump to version 1.22.1
[platform/upstream/busybox.git] / util-linux / fdisk_osf.c
index 17267d5..ff16389 100644 (file)
@@ -1,4 +1,3 @@
-#ifdef CONFIG_FEATURE_OSF_LABEL
 /*
  * Copyright (c) 1987, 1988 Regents of the University of California.
  * All rights reserved.
@@ -32,6 +31,7 @@
  * SUCH DAMAGE.
  */
 
+#if ENABLE_FEATURE_OSF_LABEL
 
 #ifndef BSD_DISKMAGIC
 #define BSD_DISKMAGIC     ((uint32_t) 0x82564557)
 
 #define BSD_LINUX_BOOTDIR "/usr/ucb/mdec"
 
-#if defined (i386) || defined (__sparc__) || defined (__arm__) || defined (__m68k__) || defined (__mips__) || defined (__s390__) || defined (__sh__) || defined(__x86_64__)
-#define BSD_LABELSECTOR   1
-#define BSD_LABELOFFSET   0
-#elif defined (__alpha__) || defined (__powerpc__) || defined (__ia64__) || defined (__hppa__)
-#define BSD_LABELSECTOR   0
-#define BSD_LABELOFFSET   64
-#elif defined (__s390__) || defined (__s390x__)
-#define BSD_LABELSECTOR   1
-#define BSD_LABELOFFSET   0
+#if defined(__alpha__) \
+ || defined(__powerpc__) \
+ || defined(__ia64__) \
+ || defined(__hppa__)
+# define BSD_LABELSECTOR   0
+# define BSD_LABELOFFSET   64
 #else
-#error unknown architecture
+# define BSD_LABELSECTOR   1
+# define BSD_LABELOFFSET   0
 #endif
 
 #define BSD_BBSIZE        8192          /* size of boot area, with label */
@@ -64,8 +62,8 @@ struct xbsd_disklabel {
        int16_t    d_type;                 /* drive type */
        int16_t    d_subtype;              /* controller/d_type specific */
        char       d_typename[16];         /* type name, e.g. "eagle" */
-       char       d_packname[16];                 /* pack identifier */
-                       /* disk geometry: */
+       char       d_packname[16];         /* pack identifier */
+       /* disk geometry: */
        uint32_t   d_secsize;              /* # of bytes per sector */
        uint32_t   d_nsectors;             /* # of data sectors per track */
        uint32_t   d_ntracks;              /* # of tracks per cylinder */
@@ -86,7 +84,7 @@ struct xbsd_disklabel {
         */
        uint32_t   d_acylinders;           /* # of alt. cylinders per unit */
 
-                       /* hardware characteristics: */
+       /* hardware characteristics: */
        /*
         * d_interleave, d_trackskew and d_cylskew describe perturbations
         * in the media format used to compensate for a slow controller.
@@ -116,11 +114,11 @@ struct xbsd_disklabel {
        uint32_t   d_spare[NSPARE];        /* reserved for future use */
        uint32_t   d_magic2;               /* the magic number (again) */
        uint16_t   d_checksum;             /* xor of data incl. partitions */
-                       /* filesystem and partition information: */
+       /* filesystem and partition information: */
        uint16_t   d_npartitions;          /* number of partitions in following */
        uint32_t   d_bbsize;               /* size of boot area at sn0, bytes */
        uint32_t   d_sbsize;               /* max size of fs superblock, bytes */
-       struct xbsd_partition    {      /* the partition table */
+       struct xbsd_partition { /* the partition table */
                uint32_t   p_size;         /* number of sectors in partition */
                uint32_t   p_offset;       /* starting sector */
                uint32_t   p_fsize;        /* filesystem basic fragment size */
@@ -146,7 +144,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",
@@ -160,7 +158,7 @@ static const char * const xbsd_dktypenames[] = {
        "floppy",
        0
 };
-#define BSD_DKMAXTYPES  (sizeof(xbsd_dktypenames) / sizeof(xbsd_dktypenames[0]) - 1)
+
 
 /*
  * Filesystem type and version.
@@ -192,31 +190,30 @@ static const char * const xbsd_dktypenames[] = {
 #define BSD_FS_MSDOS    8               /* MS-DOS file system */
 #endif
 
-static const struct systypes xbsd_fstypes[] = {
-       { "\x00" "unused" },            /* BSD_FS_UNUSED  */
-       { "\x01" "swap" },              /* BSD_FS_SWAP    */
-       { "\x02" "Version 6" },         /* BSD_FS_V6      */
-       { "\x03" "Version 7" },         /* BSD_FS_V7      */
-       { "\x04" "System V" },          /* BSD_FS_SYSV    */
-       { "\x05" "4.1BSD" },            /* BSD_FS_V71K    */
-       { "\x06" "Eighth Edition" },    /* BSD_FS_V8      */
-       { "\x07" "4.2BSD" },            /* BSD_FS_BSDFFS  */
+static const char *const xbsd_fstypes[] = {
+       "\x00" "unused",            /* BSD_FS_UNUSED  */
+       "\x01" "swap",              /* BSD_FS_SWAP    */
+       "\x02" "Version 6",         /* BSD_FS_V6      */
+       "\x03" "Version 7",         /* BSD_FS_V7      */
+       "\x04" "System V",          /* BSD_FS_SYSV    */
+       "\x05" "4.1BSD",            /* BSD_FS_V71K    */
+       "\x06" "Eighth Edition",    /* BSD_FS_V8      */
+       "\x07" "4.2BSD",            /* BSD_FS_BSDFFS  */
 #ifdef __alpha__
-       { "\x08" "ext2" },              /* BSD_FS_EXT2    */
+       "\x08" "ext2",              /* BSD_FS_EXT2    */
 #else
-       { "\x08" "MS-DOS" },            /* BSD_FS_MSDOS   */
+       "\x08" "MS-DOS",            /* BSD_FS_MSDOS   */
 #endif
-       { "\x09" "4.4LFS" },            /* BSD_FS_BSDLFS  */
-       { "\x0a" "unknown" },           /* BSD_FS_OTHER   */
-       { "\x0b" "HPFS" },              /* BSD_FS_HPFS    */
-       { "\x0c" "ISO-9660" },          /* BSD_FS_ISO9660 */
-       { "\x0d" "boot" },              /* BSD_FS_BOOT    */
-       { "\x0e" "ADOS" },              /* BSD_FS_ADOS    */
-       { "\x0f" "HFS" },               /* BSD_FS_HFS     */
-       { "\x10" "AdvFS" },             /* BSD_FS_ADVFS   */
-       { NULL }
+       "\x09" "4.4LFS",            /* BSD_FS_BSDLFS  */
+       "\x0a" "unknown",           /* BSD_FS_OTHER   */
+       "\x0b" "HPFS",              /* BSD_FS_HPFS    */
+       "\x0c" "ISO-9660",          /* BSD_FS_ISO9660 */
+       "\x0d" "boot",              /* BSD_FS_BOOT    */
+       "\x0e" "ADOS",              /* BSD_FS_ADOS    */
+       "\x0f" "HFS",               /* BSD_FS_HFS     */
+       "\x10" "AdvFS",             /* BSD_FS_ADVFS   */
+       NULL
 };
-#define BSD_FSMAXTYPES (SIZE(xbsd_fstypes)-1)
 
 
 /*
@@ -238,8 +235,6 @@ static const struct systypes xbsd_fstypes[] = {
    Also fixed unaligned accesses in alpha_bootblock_checksum()
 */
 
-static int possibly_osf_label;
-
 #define FREEBSD_PARTITION       0xa5
 #define NETBSD_PARTITION        0xa9
 
@@ -253,30 +248,38 @@ static void xbsd_change_fstype(void);
 static int xbsd_get_part_index(int max);
 static int xbsd_check_new_partition(int *i);
 static void xbsd_list_types(void);
-static u_short xbsd_dkcksum(struct xbsd_disklabel *lp);
-static int xbsd_initlabel(struct partition *p, struct xbsd_disklabel *d);
-static int xbsd_readlabel(struct partition *p, struct xbsd_disklabel *d);
-static int xbsd_writelabel(struct partition *p, struct xbsd_disklabel *d);
+static uint16_t xbsd_dkcksum(struct xbsd_disklabel *lp);
+static int xbsd_initlabel(struct partition *p);
+static int xbsd_readlabel(struct partition *p);
+static int xbsd_writelabel(struct partition *p);
 
-#if defined (__alpha__)
+#if defined(__alpha__)
 static void alpha_bootblock_checksum(char *boot);
 #endif
 
-#if !defined (__alpha__)
+#if !defined(__alpha__)
 static int xbsd_translate_fstype(int linux_type);
 static void xbsd_link_part(void);
 static struct partition *xbsd_part;
 static int xbsd_part_index;
 #endif
 
-#if defined (__alpha__)
+
+/* Group big globals data and allocate it in one go */
+struct bsd_globals {
 /* We access this through a uint64_t * when checksumming */
-static char disklabelbuffer[BSD_BBSIZE] ATTRIBUTE_ALIGNED(8);
-#else
-static char disklabelbuffer[BSD_BBSIZE];
-#endif
+/* hopefully xmalloc gives us required alignment */
+       char disklabelbuffer[BSD_BBSIZE];
+       struct xbsd_disklabel xbsd_dlabel;
+};
+
+static struct bsd_globals *bsd_globals_ptr;
 
-static struct xbsd_disklabel xbsd_dlabel;
+#define disklabelbuffer (bsd_globals_ptr->disklabelbuffer)
+#define xbsd_dlabel     (bsd_globals_ptr->xbsd_dlabel)
+
+
+/* Code */
 
 #define bsd_cround(n) \
        (display_in_cyl_units ? ((n)/xbsd_dlabel.d_secpercyl) + 1 : (n))
@@ -290,44 +293,43 @@ static struct xbsd_disklabel xbsd_dlabel;
 static int
 check_osf_label(void)
 {
-       if (xbsd_readlabel(NULL, &xbsd_dlabel) == 0)
+       if (xbsd_readlabel(NULL) == 0)
                return 0;
        return 1;
 }
 
 static int
-btrydev(const char * dev)
+bsd_trydev(const char * dev)
 {
-       if (xbsd_readlabel (NULL, &xbsd_dlabel) == 0)
+       if (xbsd_readlabel(NULL) == 0)
                return -1;
-       printf(_("\nBSD label for device: %s\n"), dev);
-       xbsd_print_disklabel (0);
+       printf("\nBSD label for device: %s\n", dev);
+       xbsd_print_disklabel(0);
        return 0;
 }
 
 static void
-bmenu(void)
+bsd_menu(void)
 {
-       puts (_("Command action"));
-       puts (_("\td\tdelete a BSD partition"));
-       puts (_("\te\tedit drive data"));
-       puts (_("\ti\tinstall bootstrap"));
-       puts (_("\tl\tlist known filesystem types"));
-       puts (_("\tm\tprint this menu"));
-       puts (_("\tn\tadd a new BSD partition"));
-       puts (_("\tp\tprint BSD partition table"));
-       puts (_("\tq\tquit without saving changes"));
-       puts (_("\tr\treturn to main menu"));
-       puts (_("\ts\tshow complete disklabel"));
-       puts (_("\tt\tchange a partition's filesystem id"));
-       puts (_("\tu\tchange units (cylinders/sectors)"));
-       puts (_("\tw\twrite disklabel to disk"));
-#if !defined (__alpha__)
-       puts (_("\tx\tlink BSD partition to non-BSD partition"));
+       puts("Command Action");
+       puts("d\tdelete a BSD partition");
+       puts("e\tedit drive data");
+       puts("i\tinstall bootstrap");
+       puts("l\tlist known filesystem types");
+       puts("n\tadd a new BSD partition");
+       puts("p\tprint BSD partition table");
+       puts("q\tquit without saving changes");
+       puts("r\treturn to main menu");
+       puts("s\tshow complete disklabel");
+       puts("t\tchange a partition's filesystem id");
+       puts("u\tchange units (cylinders/sectors)");
+       puts("w\twrite disklabel to disk");
+#if !defined(__alpha__)
+       puts("x\tlink BSD partition to non-BSD partition");
 #endif
 }
 
-#if !defined (__alpha__)
+#if !defined(__alpha__)
 static int
 hidden(int type)
 {
@@ -347,7 +349,7 @@ is_bsd_partition_type(int type)
 static void
 bsd_select(void)
 {
-#if !defined (__alpha__)
+#if !defined(__alpha__)
        int t, ss;
        struct partition *p;
 
@@ -358,13 +360,13 @@ bsd_select(void)
                        xbsd_part_index = t;
                        ss = get_start_sect(xbsd_part);
                        if (ss == 0) {
-                               fprintf(stderr, _("Partition %s has invalid starting sector 0.\n"),
+                               printf("Partition %s has invalid starting sector 0\n",
                                        partname(disk_device, t+1, 0));
                                return;
                        }
-                               printf(_("Reading disklabel of %s at sector %d.\n"),
+                               printf("Reading disklabel of %s at sector %u\n",
                                        partname(disk_device, t+1, 0), ss + BSD_LABELSECTOR);
-                       if (xbsd_readlabel(xbsd_part, &xbsd_dlabel) == 0)
+                       if (xbsd_readlabel(xbsd_part) == 0)
                                if (xbsd_create_disklabel() == 0)
                                        return;
                                break;
@@ -372,21 +374,21 @@ bsd_select(void)
        }
 
        if (t == 4) {
-               printf(_("There is no *BSD partition on %s.\n"), disk_device);
+               printf("There is no *BSD partition on %s\n", disk_device);
                return;
        }
 
-#elif defined (__alpha__)
+#elif defined(__alpha__)
 
-       if (xbsd_readlabel(NULL, &xbsd_dlabel) == 0)
+       if (xbsd_readlabel(NULL) == 0)
                if (xbsd_create_disklabel() == 0)
-                       exit (EXIT_SUCCESS);
+                       exit(EXIT_SUCCESS);
 
 #endif
 
        while (1) {
-               putchar('\n');
-               switch (tolower(read_nonempty(_("BSD disklabel command (m for help): ")))) {
+               bb_putchar('\n');
+               switch (tolower(read_nonempty("BSD disklabel command (m for help): "))) {
                case 'd':
                        xbsd_delete_part();
                        break;
@@ -406,7 +408,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;
@@ -422,13 +425,13 @@ bsd_select(void)
                case 'w':
                        xbsd_write_disklabel();
                        break;
-#if !defined (__alpha__)
+#if !defined(__alpha__)
                case 'x':
                        xbsd_link_part();
                        break;
 #endif
                default:
-                       bmenu();
+                       bsd_menu();
                        break;
                }
        }
@@ -458,7 +461,7 @@ xbsd_new_part(void)
        if (!xbsd_check_new_partition(&i))
                return;
 
-#if !defined (__alpha__) && !defined (__powerpc__) && !defined (__hppa__)
+#if !defined(__alpha__) && !defined(__powerpc__) && !defined(__hppa__)
        begin = get_start_sect(xbsd_part);
        end = begin + get_nr_sects(xbsd_part) - 1;
 #else
@@ -466,17 +469,17 @@ xbsd_new_part(void)
        end = xbsd_dlabel.d_secperunit - 1;
 #endif
 
-       snprintf(mesg, sizeof(mesg), _("First %s"), str_units(SINGULAR));
+       snprintf(mesg, sizeof(mesg), "First %s", str_units(SINGULAR));
        begin = read_int(bsd_cround(begin), bsd_cround(begin), bsd_cround(end),
                0, mesg);
 
        if (display_in_cyl_units)
                begin = (begin - 1) * xbsd_dlabel.d_secpercyl;
 
-       snprintf(mesg, sizeof(mesg), _("Last %s or +size or +sizeM or +sizeK"),
+       snprintf(mesg, sizeof(mesg), "Last %s or +size or +sizeM or +sizeK",
                str_units(SINGULAR));
-       end = read_int(bsd_cround (begin), bsd_cround (end), bsd_cround (end),
-               bsd_cround (begin), mesg);
+       end = read_int(bsd_cround(begin), bsd_cround(end), bsd_cround(end),
+               bsd_cround(begin), mesg);
 
        if (display_in_cyl_units)
                end = end * xbsd_dlabel.d_secpercyl - 1;
@@ -494,66 +497,63 @@ xbsd_print_disklabel(int show_all)
        int i, j;
 
        if (show_all) {
-#if defined (__alpha__)
+               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)
-                       printf(_("type: %s\n"), xbsd_dktypenames[lp->d_type]);
+               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"));
-               printf("\n");
+                       printf("type: %u\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: ");
+               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);
-               printf(_("sectors/track: %ld\n"), (long) lp->d_nsectors);
-               printf(_("tracks/cylinder: %ld\n"), (long) lp->d_ntracks);
-               printf(_("sectors/cylinder: %ld\n"), (long) lp->d_secpercyl);
-               printf(_("cylinders: %ld\n"), (long) lp->d_ncylinders);
-               printf(_("rpm: %d\n"), lp->d_rpm);
-               printf(_("interleave: %d\n"), lp->d_interleave);
-               printf(_("trackskew: %d\n"), lp->d_trackskew);
-               printf(_("cylinderskew: %d\n"), lp->d_cylskew);
-               printf(_("headswitch: %ld\t\t# milliseconds\n"),
+               printf("bytes/sector: %lu\n", (long) lp->d_secsize);
+               printf("sectors/track: %lu\n", (long) lp->d_nsectors);
+               printf("tracks/cylinder: %lu\n", (long) lp->d_ntracks);
+               printf("sectors/cylinder: %lu\n", (long) lp->d_secpercyl);
+               printf("cylinders: %lu\n", (long) lp->d_ncylinders);
+               printf("rpm: %u\n", lp->d_rpm);
+               printf("interleave: %u\n", lp->d_interleave);
+               printf("trackskew: %u\n", lp->d_trackskew);
+               printf("cylinderskew: %u\n", lp->d_cylskew);
+               printf("headswitch: %lu\t\t# milliseconds\n",
                        (long) lp->d_headswitch);
-               printf(_("track-to-track seek: %ld\t# milliseconds\n"),
+               printf("track-to-track seek: %lu\t# milliseconds\n",
                        (long) lp->d_trkseek);
-               printf(_("drivedata: "));
+               printf("drivedata: ");
                for (i = NDDATA - 1; i >= 0; i--)
                        if (lp->d_drivedata[i])
                                break;
                if (i < 0)
                        i = 0;
                for (j = 0; j <= i; j++)
-                       printf("%ld ", (long) lp->d_drivedata[j]);
+                       printf("%lu ", (long) lp->d_drivedata[j]);
        }
-       printf(_("\n%d partitions:\n"), lp->d_npartitions);
-       printf(_("#       start       end      size     fstype   [fsize bsize   cpg]\n"));
+       printf("\n%u partitions:\n", lp->d_npartitions);
+       printf("#       start       end      size     fstype   [fsize bsize   cpg]\n");
        pp = lp->d_partitions;
        for (i = 0; i < lp->d_npartitions; i++, pp++) {
                if (pp->p_size) {
                        if (display_in_cyl_units && lp->d_secpercyl) {
-                               printf("  %c: %8ld%c %8ld%c %8ld%c  ",
+                               printf("  %c: %8lu%c %8lu%c %8lu%c  ",
                                        'a' + i,
-                                       (long) pp->p_offset / lp->d_secpercyl + 1,
+                                       (unsigned long) pp->p_offset / lp->d_secpercyl + 1,
                                        (pp->p_offset % lp->d_secpercyl) ? '*' : ' ',
-                                       (long) (pp->p_offset + pp->p_size + lp->d_secpercyl - 1) / lp->d_secpercyl,
+                                       (unsigned long) (pp->p_offset + pp->p_size + lp->d_secpercyl - 1) / lp->d_secpercyl,
                                        ((pp->p_offset + pp->p_size) % lp->d_secpercyl) ? '*' : ' ',
                                        (long) pp->p_size / lp->d_secpercyl,
                                        (pp->p_size % lp->d_secpercyl) ? '*' : ' '
                                );
                        } else {
-                               printf("  %c: %8ld  %8ld  %8ld   ",
+                               printf("  %c: %8lu  %8lu  %8lu   ",
                                        'a' + i,
                                        (long) pp->p_offset,
                                        (long) pp->p_offset + pp->p_size - 1,
@@ -561,25 +561,25 @@ xbsd_print_disklabel(int show_all)
                                );
                        }
 
-                       if ((unsigned) pp->p_fstype < BSD_FSMAXTYPES)
-                               printf("%8.8s", xbsd_fstypes[pp->p_fstype].name);
+                       if ((unsigned) pp->p_fstype < ARRAY_SIZE(xbsd_fstypes)-1)
+                               printf("%8.8s", xbsd_fstypes[pp->p_fstype]);
                        else
                                printf("%8x", pp->p_fstype);
 
                        switch (pp->p_fstype) {
                        case BSD_FS_UNUSED:
-                               printf("    %5ld %5ld %5.5s ",
+                               printf("    %5lu %5lu %5.5s ",
                                        (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, "");
                                break;
                        case BSD_FS_BSDFFS:
-                               printf("    %5ld %5ld %5d ",
+                               printf("    %5lu %5lu %5u ",
                                        (long) pp->p_fsize, (long) pp->p_fsize * pp->p_frag, pp->p_cpg);
                                break;
                        default:
                                printf("%22.22s", "");
                                break;
                        }
-                       printf("\n");
+                       bb_putchar('\n');
                }
        }
 }
@@ -587,13 +587,13 @@ xbsd_print_disklabel(int show_all)
 static void
 xbsd_write_disklabel(void)
 {
-#if defined (__alpha__)
-       printf(_("Writing disklabel to %s.\n"), disk_device);
-       xbsd_writelabel(NULL, &xbsd_dlabel);
+#if defined(__alpha__)
+       printf("Writing disklabel to %s\n", disk_device);
+       xbsd_writelabel(NULL);
 #else
-       printf(_("Writing disklabel to %s.\n"),
+       printf("Writing disklabel to %s\n",
                partname(disk_device, xbsd_part_index + 1, 0));
-       xbsd_writelabel(xbsd_part, &xbsd_dlabel);
+       xbsd_writelabel(xbsd_part);
 #endif
        reread_partition_table(0);      /* no exit yet */
 }
@@ -603,43 +603,46 @@ xbsd_create_disklabel(void)
 {
        char c;
 
-#if defined (__alpha__)
-       fprintf(stderr, _("%s contains no disklabel.\n"), disk_device);
+#if defined(__alpha__)
+       printf("%s contains no disklabel\n", disk_device);
 #else
-       fprintf(stderr, _("%s contains no disklabel.\n"),
+       printf("%s contains no disklabel\n",
                partname(disk_device, xbsd_part_index + 1, 0));
 #endif
 
        while (1) {
-               c = read_nonempty(_("Do you want to create a disklabel? (y/n) "));
-               if (c == 'y' || c == 'Y') {
+               c = read_nonempty("Do you want to create a disklabel? (y/n) ");
+               if ((c|0x20) == 'y') {
                        if (xbsd_initlabel(
-#if defined (__alpha__) || defined (__powerpc__) || defined (__hppa__) || \
-       defined (__s390__) || defined (__s390x__)
-                               NULL, &xbsd_dlabel
+#if defined(__alpha__) || defined(__powerpc__) || defined(__hppa__) || \
+       defined(__s390__) || defined(__s390x__)
+                               NULL
 #else
-                               xbsd_part, &xbsd_dlabel/* not used, xbsd_part_index*/
+                               xbsd_part
 #endif
-                               ) == 1) {
-                               xbsd_print_disklabel (1);
+                       ) == 1) {
+                               xbsd_print_disklabel(1);
                                return 1;
-                       } else
-                               return 0;
-               } else if (c == 'n')
+                       }
+                       return 0;
+               }
+               if ((c|0x20) == 'n')
                        return 0;
        }
 }
 
 static int
-edit_int(int def, char *mesg)
+edit_int(int def, const char *mesg)
 {
+       mesg = xasprintf("%s (%u): ", mesg, def);
        do {
-               fputs(mesg, stdout);
-               printf(" (%d): ", def);
-               if (!read_line())
-                       return def;
+               if (!read_line(mesg))
+                       goto ret;
        } while (!isdigit(*line_ptr));
-       return atoi(line_ptr);
+       def = atoi(line_ptr);
+ ret:
+       free((char*)mesg);
+       return def;
 }
 
 static void
@@ -649,43 +652,43 @@ xbsd_edit_disklabel(void)
 
        d = &xbsd_dlabel;
 
-#if defined (__alpha__) || defined (__ia64__)
-       d->d_secsize    = (u_long) edit_int((u_long) d->d_secsize     ,_("bytes/sector"));
-       d->d_nsectors   = (u_long) edit_int((u_long) d->d_nsectors    ,_("sectors/track"));
-       d->d_ntracks    = (u_long) edit_int((u_long) d->d_ntracks     ,_("tracks/cylinder"));
-       d->d_ncylinders = (u_long) edit_int((u_long) d->d_ncylinders  ,_("cylinders"));
+#if defined(__alpha__) || defined(__ia64__)
+       d->d_secsize    = edit_int(d->d_secsize     , "bytes/sector");
+       d->d_nsectors   = edit_int(d->d_nsectors    , "sectors/track");
+       d->d_ntracks    = edit_int(d->d_ntracks     , "tracks/cylinder");
+       d->d_ncylinders = edit_int(d->d_ncylinders  , "cylinders");
 #endif
 
-  /* d->d_secpercyl can be != d->d_nsectors * d->d_ntracks */
+       /* d->d_secpercyl can be != d->d_nsectors * d->d_ntracks */
        while (1) {
-               d->d_secpercyl = (u_long) edit_int((u_long) d->d_nsectors * d->d_ntracks,
-                               _("sectors/cylinder"));
+               d->d_secpercyl = edit_int(d->d_nsectors * d->d_ntracks,
+                               "sectors/cylinder");
                if (d->d_secpercyl <= d->d_nsectors * d->d_ntracks)
                        break;
 
-               printf(_("Must be <= sectors/track * tracks/cylinder (default).\n"));
+               printf("Must be <= sectors/track * tracks/cylinder (default)\n");
        }
-       d->d_rpm        = (u_short) edit_int((u_short) d->d_rpm       ,_("rpm"));
-       d->d_interleave = (u_short) edit_int((u_short) d->d_interleave,_("interleave"));
-       d->d_trackskew  = (u_short) edit_int((u_short) d->d_trackskew ,_("trackskew"));
-       d->d_cylskew    = (u_short) edit_int((u_short) d->d_cylskew   ,_("cylinderskew"));
-       d->d_headswitch = (u_long) edit_int((u_long) d->d_headswitch  ,_("headswitch"));
-       d->d_trkseek    = (u_long) edit_int((u_long) d->d_trkseek     ,_("track-to-track seek"));
+       d->d_rpm        = edit_int(d->d_rpm       , "rpm");
+       d->d_interleave = edit_int(d->d_interleave, "interleave");
+       d->d_trackskew  = edit_int(d->d_trackskew , "trackskew");
+       d->d_cylskew    = edit_int(d->d_cylskew   , "cylinderskew");
+       d->d_headswitch = edit_int(d->d_headswitch, "headswitch");
+       d->d_trkseek    = edit_int(d->d_trkseek   , "track-to-track seek");
 
        d->d_secperunit = d->d_secpercyl * d->d_ncylinders;
 }
 
 static int
-xbsd_get_bootstrap (char *path, void *ptr, int size)
+xbsd_get_bootstrap(char *path, void *ptr, int size)
 {
        int fdb;
 
-       if ((fdb = open (path, O_RDONLY)) < 0) {
-               perror(path);
+       fdb = open_or_warn(path, O_RDONLY);
+       if (fdb < 0) {
                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;
        }
@@ -697,17 +700,17 @@ xbsd_get_bootstrap (char *path, void *ptr, int size)
 static void
 sync_disks(void)
 {
-       printf(_("\nSyncing disks.\n"));
+       printf("Syncing disks\n");
        sync();
-       sleep(4); /* What? */
+       /* sleep(4); What? */
 }
 
 static void
 xbsd_write_bootstrap(void)
 {
-       char *bootdir = BSD_LINUX_BOOTDIR;
        char path[MAXPATHLEN];
-       char *dkbasename;
+       const char *bootdir = BSD_LINUX_BOOTDIR;
+       const char *dkbasename;
        struct xbsd_disklabel dl;
        char *d, *p, *e;
        int sector;
@@ -717,10 +720,9 @@ xbsd_write_bootstrap(void)
        else
                dkbasename = "wd";
 
-       printf(_("Bootstrap: %sboot -> boot%s (%s): "),
+       snprintf(path, sizeof(path), "Bootstrap: %sboot -> boot%s (%s): ",
                dkbasename, dkbasename, dkbasename);
-       if (read_line()) {
-               line_ptr[strlen(line_ptr)-1] = '\0';
+       if (read_line(path)) {
                dkbasename = line_ptr;
        }
        snprintf(path, sizeof(path), "%s/%sboot", bootdir, dkbasename);
@@ -735,38 +737,36 @@ xbsd_write_bootstrap(void)
        memset(d, 0, sizeof(struct xbsd_disklabel));
 
        snprintf(path, sizeof(path), "%s/boot%s", bootdir, dkbasename);
-       if (!xbsd_get_bootstrap (path, &disklabelbuffer[xbsd_dlabel.d_secsize],
-                         (int) xbsd_dlabel.d_bbsize - xbsd_dlabel.d_secsize))
+       if (!xbsd_get_bootstrap(path, &disklabelbuffer[xbsd_dlabel.d_secsize],
+                       (int) xbsd_dlabel.d_bbsize - xbsd_dlabel.d_secsize))
                return;
 
        e = d + sizeof(struct xbsd_disklabel);
        for (p = d; p < e; p++)
                if (*p) {
-                       fprintf(stderr, _("Bootstrap overlaps with disk label!\n"));
+                       printf("Bootstrap overlaps with disk label!\n");
                        exit(EXIT_FAILURE);
                }
 
        memmove(d, &dl, sizeof(struct xbsd_disklabel));
 
-#if defined (__powerpc__) || defined (__hppa__)
+#if defined(__powerpc__) || defined(__hppa__)
        sector = 0;
-#elif defined (__alpha__)
+#elif defined(__alpha__)
        sector = 0;
        alpha_bootblock_checksum(disklabelbuffer);
 #else
        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);
+#if defined(__alpha__)
+       printf("Bootstrap installed on %s\n", disk_device);
 #else
-       printf(_("Bootstrap installed on %s.\n"),
-               partname (disk_device, xbsd_part_index+1, 0));
+       printf("Bootstrap installed on %s\n",
+               partname(disk_device, xbsd_part_index+1, 0));
 #endif
 
        sync_disks();
@@ -784,10 +784,10 @@ xbsd_change_fstype(void)
 static int
 xbsd_get_part_index(int max)
 {
-       char prompt[256];
+       char prompt[sizeof("Partition (a-%c): ") + 16];
        char l;
 
-       snprintf(prompt, sizeof(prompt), _("Partition (a-%c): "), 'a' + max - 1);
+       snprintf(prompt, sizeof(prompt), "Partition (a-%c): ", 'a' + max - 1);
        do
                l = tolower(read_nonempty(prompt));
        while (l < 'a' || l > 'a' + max - 1);
@@ -806,19 +806,18 @@ xbsd_check_new_partition(int *i)
                                break;
 
                if (t == BSD_MAXPARTITIONS) {
-                       fprintf(stderr, _("The maximum number of partitions "
-                                          "has been created\n"));
+                       printf("The maximum number of partitions has been created\n");
                        return 0;
                }
        }
 
-       *i = xbsd_get_part_index (BSD_MAXPARTITIONS);
+       *i = xbsd_get_part_index(BSD_MAXPARTITIONS);
 
        if (*i >= xbsd_dlabel.d_npartitions)
                xbsd_dlabel.d_npartitions = (*i) + 1;
 
        if (xbsd_dlabel.d_partitions[*i].p_size != 0) {
-               fprintf(stderr, _("This partition already exists.\n"));
+               printf("This partition already exists\n");
                return 0;
        }
 
@@ -831,22 +830,23 @@ xbsd_list_types(void)
        list_types(xbsd_fstypes);
 }
 
-static u_short
+static uint16_t
 xbsd_dkcksum(struct xbsd_disklabel *lp)
 {
-       u_short *start, *end;
-       u_short sum = 0;
+       uint16_t *start, *end;
+       uint16_t sum = 0;
 
-       start = (u_short *) lp;
-       end = (u_short *) &lp->d_partitions[lp->d_npartitions];
+       start = (uint16_t *) lp;
+       end = (uint16_t *) &lp->d_partitions[lp->d_npartitions];
        while (start < end)
                sum ^= *start++;
        return sum;
 }
 
 static int
-xbsd_initlabel(struct partition *p, struct xbsd_disklabel *d)
+xbsd_initlabel(struct partition *p)
 {
+       struct xbsd_disklabel *d = &xbsd_dlabel;
        struct xbsd_partition *pp;
 
        get_geometry();
@@ -859,16 +859,16 @@ xbsd_initlabel(struct partition *p, struct xbsd_disklabel *d)
        else
                d->d_type = BSD_DTYPE_ST506;
 
-#if !defined (__alpha__)
+#if !defined(__alpha__)
        d->d_flags = BSD_D_DOSPART;
 #else
        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;
@@ -884,22 +884,21 @@ xbsd_initlabel(struct partition *p, struct xbsd_disklabel *d)
        d->d_bbsize = BSD_BBSIZE;
        d->d_sbsize = BSD_SBSIZE;
 
-#if !defined (__alpha__)
+#if !defined(__alpha__)
        d->d_npartitions = 4;
-       pp = &d->d_partitions[2];             /* Partition C should be
-                                                  the NetBSD partition */
+       pp = &d->d_partitions[2]; /* Partition C should be NetBSD partition */
+
        pp->p_offset = get_start_sect(p);
        pp->p_size   = get_nr_sects(p);
        pp->p_fstype = BSD_FS_UNUSED;
-       pp = &d->d_partitions[3];             /* Partition D should be
-                                                  the whole disk */
+       pp = &d->d_partitions[3]; /* Partition D should be whole disk */
+
        pp->p_offset = 0;
        pp->p_size   = d->d_secperunit;
        pp->p_fstype = BSD_FS_UNUSED;
-#elif defined (__alpha__)
+#else
        d->d_npartitions = 3;
-       pp = &d->d_partitions[2];             /* Partition C should be
-                                                  the whole disk */
+       pp = &d->d_partitions[2]; /* Partition C should be the whole disk */
        pp->p_offset = 0;
        pp->p_size   = d->d_secperunit;
        pp->p_fstype = BSD_FS_UNUSED;
@@ -913,24 +912,29 @@ xbsd_initlabel(struct partition *p, struct xbsd_disklabel *d)
  * If it has the right magic, return 1.
  */
 static int
-xbsd_readlabel (struct partition *p, struct xbsd_disklabel *d)
+xbsd_readlabel(struct partition *p)
 {
+       struct xbsd_disklabel *d;
        int t, sector;
 
+       if (!bsd_globals_ptr)
+               bsd_globals_ptr = xzalloc(sizeof(*bsd_globals_ptr));
+
+       d = &xbsd_dlabel;
+
        /* p is used only to get the starting sector */
-#if !defined (__alpha__)
+#if !defined(__alpha__)
        sector = (p ? get_start_sect(p) : 0);
-#elif defined (__alpha__)
+#else
        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],
-                  sizeof(struct xbsd_disklabel));
+                       sizeof(struct xbsd_disklabel));
 
        if (d->d_magic != BSD_DISKMAGIC || d->d_magic2 != BSD_DISKMAGIC)
                return 0;
@@ -942,25 +946,26 @@ xbsd_readlabel (struct partition *p, struct xbsd_disklabel *d)
        }
 
        if (d->d_npartitions > BSD_MAXPARTITIONS)
-               fprintf(stderr, _("Warning: too many partitions "
-                               "(%d, maximum is %d).\n"),
+               printf("Warning: too many partitions (%u, maximum is %u)\n",
                        d->d_npartitions, BSD_MAXPARTITIONS);
        return 1;
 }
 
 static int
-xbsd_writelabel (struct partition *p, struct xbsd_disklabel *d)
+xbsd_writelabel(struct partition *p)
 {
+       struct xbsd_disklabel *d = &xbsd_dlabel;
        unsigned int sector;
 
-#if !defined (__alpha__) && !defined (__powerpc__) && !defined (__hppa__)
+#if !defined(__alpha__) && !defined(__powerpc__) && !defined(__hppa__)
        sector = get_start_sect(p) + BSD_LABELSECTOR;
 #else
+       (void)p; /* silence warning */
        sector = BSD_LABELSECTOR;
 #endif
 
        d->d_checksum = 0;
-       d->d_checksum = xbsd_dkcksum (d);
+       d->d_checksum = xbsd_dkcksum(d);
 
        /* This is necessary if we want to write the bootstrap later,
           otherwise we'd write the old disklabel with the bootstrap.
@@ -968,24 +973,21 @@ xbsd_writelabel (struct partition *p, struct xbsd_disklabel *d)
        memmove(&disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
                d, sizeof(struct xbsd_disklabel));
 
-#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);
+#if defined(__alpha__) && BSD_LABELSECTOR == 0
+       alpha_bootblock_checksum(disklabelbuffer);
+       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;
 }
 
 
-#if !defined (__alpha__)
+#if !defined(__alpha__)
 static int
 xbsd_translate_fstype(int linux_type)
 {
@@ -1010,7 +1012,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;
@@ -1023,12 +1025,7 @@ xbsd_link_part(void)
 }
 #endif
 
-#if defined (__alpha__)
-
-#if !defined(__GLIBC__)
-typedef unsigned long long uint64_t;
-#endif
-
+#if defined(__alpha__)
 static void
 alpha_bootblock_checksum(char *boot)
 {
@@ -1043,4 +1040,8 @@ alpha_bootblock_checksum(char *boot)
 }
 #endif /* __alpha__ */
 
+/* Undefine 'global' tricks */
+#undef disklabelbuffer
+#undef xbsd_dlabel
+
 #endif /* OSF_LABEL */