remove unused files
[oweals/busybox.git] / util-linux / fdisk.c
index 11da7717933ae16e976d5bc924aecf40587f5ce2..8580bec7036b755606f532dbc1c500afc2ea2d2a 100644 (file)
@@ -7,22 +7,14 @@
  * published by the Free Software Foundation: either version 1 or
  * (at your option) any later version.
  *
- * For detailed old history, see older versions.
- * Contributions before 2001 by faith@cs.unc.edu, Michael Bischoff,
- * LeBlanc@mcc.ac.uk, martin@cs.unc.edu, leisner@sdsp.mc.xerox.com,
- * esr@snark.thyrsus.com, aeb@cwi.nl, quinlan@yggdrasil.com,
- * fasten@cs.bonn.edu, orschaer@cip.informatik.uni-erlangen.de,
- * jj@sunsite.mff.cuni.cz, fasten@shw.com, ANeuper@GUUG.de,
- * kgw@suse.de, kalium@gmx.de, dhuggins@linuxcare.com,
- * michal@ellpspace.math.ualberta.ca and probably others.
- *
  * Vladimir Oleynik <dzo@simtreas.ru> 2001,2002 Busybox port
  */
 
-#define UTIL_LINUX_VERSION "2.11z"
+#define UTIL_LINUX_VERSION "2.12"
 
 #define PROC_PARTITIONS "/proc/partitions"
 
+#include <features.h>
 #include <sys/types.h>
 #include <sys/stat.h>           /* stat */
 #include <ctype.h>
 #include <setjmp.h>
 #include <assert.h>             /* assert */
 #include <getopt.h>
-
 #include <endian.h>
-#define u_char  unsigned char
-#include <scsi/scsi.h>          /* SCSI_IOCTL_GET_IDLUN */
-#undef u_char
-
 #include <sys/ioctl.h>
 #include <sys/param.h>
+#include <sys/sysmacros.h>     /* major */
 
 #include <stdint.h>        /* for uint32_t, uint16_t, uint8_t, int16_t, etc */
 
 #define BLKFLSBUF  _IO(0x12,97)    /* flush buffer cache */
 #define BLKSSZGET  _IO(0x12,104)   /* get block device sector size */
 
+/* Avoid conflicts with the 2.6 kernel headers, which define
+ * _IOR rather differently */
+#undef _IOR
+#define _IOR(type,nr,size)      _IOC(_IOC_READ,(type),(nr),sizeof(size))
+#define BLKGETSIZE64 _IOR(0x12,114,uint64_t)
 
 /*
    fdisk.h
 #define cround(n)       (display_in_cyl_units ? ((n)/units_per_sector)+1 : (n))
 #define scround(x)      (((x)+units_per_sector-1)/units_per_sector)
 
-#if defined(__GNUC__) || defined(HAS_LONG_LONG)
-typedef long long ext2_loff_t;
-#else
-typedef long      ext2_loff_t;
+#ifdef CONFIG_FEATURE_SUN_LABEL
+#define SCSI_IOCTL_GET_IDLUN 0x5382
 #endif
 
+
 /* including <linux/hdreg.h> also fails */
 struct hd_geometry {
       unsigned char heads;
@@ -117,6 +109,9 @@ struct systypes {
        const unsigned char *name;
 };
 
+static uint    sector_size = DEFAULT_SECTOR_SIZE,
+       user_set_sector_size,
+       sector_offset = 1;
 
 /*
  * Raw disk label. For DOS-type partition tables the MBR,
@@ -201,7 +196,7 @@ struct partition {
        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__));
 
 enum failure {
        ioctl_error, unable_to_open, unable_to_read, unable_to_seek,
@@ -257,8 +252,8 @@ static int get_boot(enum action what);
                        }
 
 
-static unsigned int get_start_sect(const struct partition *p);
-static unsigned int get_nr_sects(const struct partition *p);
+static int32_t get_start_sect(const struct partition *p);
+static int32_t get_nr_sects(const struct partition *p);
 
 /*
  * per partition table entry data
@@ -274,7 +269,7 @@ static struct pte {
 #ifdef CONFIG_FEATURE_FDISK_WRITABLE
        char changed;           /* boolean */
 #endif
-       uint offset;            /* disk sector number */
+       off_t offset;            /* disk sector number */
        char *sectorbuffer;     /* disk sector contents */
 } ptes[MAXIMUM_PARTS];
 
@@ -405,8 +400,11 @@ typedef struct {
 
 /*
   Changes:
-  Sat Mar 20 09:51:38 EST 1999 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
-       Internationalization
+  * 1999-03-20 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
+  *     Internationalization
+  *
+  * 2003-03-20 Phillip Kesling <pkesling@sgi.com>
+  *      Some fixes
 */
 
 static  int     aix_other_endian;
@@ -478,7 +476,7 @@ check_aix_label( void )
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
+ *    must display the following acknowledgment:
  *      This product includes software developed by the University of
  *      California, Berkeley and its contributors.
  * 4. Neither the name of the University nor the names of its contributors
@@ -515,6 +513,9 @@ check_aix_label( void )
 #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
 #else
 #error unknown architecture
 #endif
@@ -830,6 +831,7 @@ typedef struct {
        unsigned short csum;       /* Label xor'd checksum */
 } sun_partition;
 
+
 #define SUN_LABEL_MAGIC          0xDABE
 #define SUN_LABEL_MAGIC_SWAPPED  0xBEDA
 #define sunlabel ((sun_partition *)MBRbuffer)
@@ -838,109 +840,6 @@ typedef struct {
 #define SUN_SSWAP32(x) (sun_other_endian ? __swap32(x) \
                                 : (uint32_t)(x))
 
-/*
- * llseek.c -- stub calling the llseek system call
- *
- * Copyright (C) 1994 Remy Card.  This file may be redistributed
- * under the terms of the GNU Public License.
- */
-
-
-#ifdef __linux__
-
-#ifdef HAVE_LLSEEK
-#include <syscall.h>
-
-#else   /* HAVE_LLSEEK */
-
-#if defined(__alpha__) || defined(__ia64__)
-
-#define my_llseek lseek
-
-#else
-#include <asm/unistd.h>       /* for __NR__llseek */
-
-static int _llseek (unsigned int, unsigned long,
-                  unsigned long, ext2_loff_t *, unsigned int);
-
-#ifdef __NR__llseek
-
-static _syscall5(int,_llseek,unsigned int,f_d,unsigned long,offset_high,
-                unsigned long, offset_low,ext2_loff_t *,result,
-                unsigned int, origin)
-
-#else
-
-/* no __NR__llseek on compilation machine - might give it explicitly */
-static int _llseek (unsigned int f_d, unsigned long oh,
-                   unsigned long ol, ext2_loff_t *result,
-                   unsigned int origin) {
-       errno = ENOSYS;
-       return -1;
-}
-
-#endif
-
-static ext2_loff_t my_llseek (unsigned int f_d, ext2_loff_t offset,
-               unsigned int origin)
-{
-       ext2_loff_t result;
-       int retval;
-
-       retval = _llseek (f_d, ((unsigned long long) offset) >> 32,
-                       ((unsigned long long) offset) & 0xffffffff,
-                       &result, origin);
-       return (retval == -1 ? (ext2_loff_t) retval : result);
-}
-
-#endif /* __alpha__ */
-
-#endif  /* HAVE_LLSEEK */
-
-static ext2_loff_t ext2_llseek (unsigned int f_d, ext2_loff_t offset,
-                        unsigned int origin)
-{
-       ext2_loff_t result;
-       static int do_compat = 0;
-
-       if (!do_compat) {
-               result = my_llseek (f_d, offset, origin);
-               if (!(result == -1 && errno == ENOSYS))
-                       return result;
-
-               /*
-                * Just in case this code runs on top of an old kernel
-                * which does not support the llseek system call
-                */
-               do_compat = 1;
-               /*
-                * Now try ordinary lseek.
-                */
-       }
-
-       if ((sizeof(off_t) >= sizeof(ext2_loff_t)) ||
-           (offset < ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1))))
-               return lseek(f_d, (off_t) offset, origin);
-
-       errno = EINVAL;
-       return -1;
-}
-
-#else /* !linux */
-
-static ext2_loff_t ext2_llseek (unsigned int f_d, ext2_loff_t offset,
-                        unsigned int origin)
-{
-       if ((sizeof(off_t) < sizeof(ext2_loff_t)) &&
-           (offset >= ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1)))) {
-               errno = EINVAL;
-               return -1;
-       }
-       return lseek (f_d, (off_t) offset, origin);
-}
-
-#endif  /* linux */
-
 
 #ifdef CONFIG_FEATURE_OSF_LABEL
 /*
@@ -983,7 +882,7 @@ static int xbsd_part_index;
 #endif
 
 #if defined (__alpha__)
-/* We access this through a u_int64_t * when checksumming */
+/* We access this through a uint64_t * when checksumming */
 static char disklabelbuffer[BSD_BBSIZE] __attribute__((aligned(8)));
 #else
 static char disklabelbuffer[BSD_BBSIZE];
@@ -1160,7 +1059,7 @@ xbsd_delete_part (void)
 static void
 xbsd_new_part (void)
 {
-  uint begin, end;
+  off_t begin, end;
   char mesg[256];
   int i;
 
@@ -1320,7 +1219,8 @@ xbsd_create_disklabel (void) {
                c = read_char (_("Do you want to create a disklabel? (y/n) "));
                if (c == 'y' || c == 'Y') {
                        if (xbsd_initlabel (
-#if defined (__alpha__) || defined (__powerpc__) || defined (__hppa__)
+#if defined (__alpha__) || defined (__powerpc__) || defined (__hppa__) || \
+    defined (__s390__) || defined (__s390x__)
                                NULL, &xbsd_dlabel, 0
 #else
                                xbsd_part, &xbsd_dlabel, xbsd_part_index
@@ -1466,7 +1366,7 @@ xbsd_write_bootstrap (void)
   sector = get_start_sect(xbsd_part);
 #endif
 
-  if (ext2_llseek (fd, (ext2_loff_t) sector * SECTOR_SIZE, SEEK_SET) == -1)
+  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);
@@ -1634,7 +1534,7 @@ xbsd_readlabel (struct partition *p, struct xbsd_disklabel *d)
        sector = 0;
 #endif
 
-       if (ext2_llseek (fd, (ext2_loff_t) sector * SECTOR_SIZE, SEEK_SET) == -1)
+       if (lseek (fd, sector * SECTOR_SIZE, SEEK_SET) == -1)
                fdisk_fatal (unable_to_seek);
        if (BSD_BBSIZE != read (fd, disklabelbuffer, BSD_BBSIZE))
                fdisk_fatal (unable_to_read);
@@ -1661,7 +1561,7 @@ xbsd_readlabel (struct partition *p, struct xbsd_disklabel *d)
 static int
 xbsd_writelabel (struct partition *p, struct xbsd_disklabel *d)
 {
-  int sector;
+  unsigned int sector;
 
 #if !defined (__alpha__) && !defined (__powerpc__) && !defined (__hppa__)
   sector = get_start_sect(p) + BSD_LABELSECTOR;
@@ -1680,12 +1580,12 @@ xbsd_writelabel (struct partition *p, struct xbsd_disklabel *d)
 
 #if defined (__alpha__) && BSD_LABELSECTOR == 0
   alpha_bootblock_checksum (disklabelbuffer);
-  if (ext2_llseek (fd, (ext2_loff_t) 0, SEEK_SET) == -1)
+  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);
 #else
-  if (ext2_llseek (fd, (ext2_loff_t) sector * SECTOR_SIZE + BSD_LABELOFFSET,
+  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)))
@@ -1740,16 +1640,16 @@ xbsd_link_part (void)
 #if defined (__alpha__)
 
 #if !defined(__GLIBC__)
-typedef unsigned long long u_int64_t;
+typedef unsigned long long uint64_t;
 #endif
 
 static void
 alpha_bootblock_checksum (char *boot)
 {
-  u_int64_t *dp, sum;
+  uint64_t *dp, sum;
   int i;
 
-  dp = (u_int64_t *)boot;
+  dp = (uint64_t *)boot;
   sum = 0;
   for (i = 0; i < 63; i++)
     sum += dp[i];
@@ -1767,7 +1667,10 @@ __swap16(unsigned short x) {
 
 static inline uint32_t
 __swap32(uint32_t x) {
-       return (((uint32_t)(x) & 0xFF) << 24) | (((uint32_t)(x) & 0xFF00) << 8) | (((uint32_t)(x) & 0xFF0000) >> 8) | (((uint32_t)(x) & 0xFF000000) >> 24);
+        return (((x & 0xFF) << 24) |
+               ((x & 0xFF00) << 8) |
+               ((x & 0xFF0000) >> 8) |
+               ((x & 0xFF000000) >> 24));
 }
 #endif
 
@@ -1793,40 +1696,39 @@ static  short   sgi_volumes=1;
  * only dealing with free blocks here
  */
 
-typedef struct { int first; int last; } freeblocks;
+typedef struct { unsigned int first; unsigned int last; } freeblocks;
 static freeblocks freelist[17]; /* 16 partitions can produce 17 vacant slots */
 
 static void
-setfreelist( int i, int f, int l ) {
+setfreelist(int i, unsigned int f, unsigned int l) {
        freelist[i].first = f;
        freelist[i].last = l;
 }
 
 static void
-add2freelist( int f, int l ) {
+add2freelist(unsigned int f, unsigned int l) {
        int i = 0;
-       for( ; i<17 ; i++ ) {
-               if(freelist[i].last==0) break;
-       }
-       setfreelist( i, f, l );
+       for ( ; i < 17 ; i++)
+               if (freelist[i].last == 0)
+                       break;
+       setfreelist(i, f, l);
 }
 
 static void
 clearfreelist(void) {
-       int i = 0;
-       for( ; i<17 ; i++ ) {
-               setfreelist( i, 0, 0 );
-       }
+       int i;
+
+       for (i = 0; i < 17 ; i++)
+               setfreelist(i, 0, 0);
 }
 
-static int
-isinfreelist( int b ) {
-       int i = 0;
-       for( ; i<17 ; i++ ) {
-               if (freelist[i].first <= b && freelist[i].last >= b) {
+static unsigned int
+isinfreelist(unsigned int b) {
+       int i;
+
+       for (i = 0; i < 17 ; i++)
+               if (freelist[i].first <= b && freelist[i].last >= b)
                        return freelist[i].last;
-               }
-       }
        return 0;
 }
        /* return last vacant block of this stride (never 0). */
@@ -1868,11 +1770,6 @@ sgi_get_ntrks(void) {
     return SGI_SSWAP16(sgilabel->devparam.ntrks);
 }
 
-static int
-sgi_get_pcylcount(void) {
-    return SGI_SSWAP16(sgilabel->devparam.pcylcount);
-}
-
 static void
 sgi_nolabel(void) {
     sgilabel->magic = 0;
@@ -1881,13 +1778,13 @@ sgi_nolabel(void) {
 }
 
 static unsigned int
-two_s_complement_32bit_sum(unsigned int* base, int size /* in bytes */ ) {
+two_s_complement_32bit_sum(unsigned int* base, int size /* in bytes */) {
     int i=0;
     unsigned int sum=0;
 
-    size = size / sizeof( unsigned int );
-    for( i=0; i<size; i++ )
-       sum = sum - SGI_SSWAP32(base[i]);
+    size /= sizeof(unsigned int);
+    for (i = 0; i < size; i++)
+           sum -= SGI_SSWAP32(base[i]);
     return sum;
 }
 
@@ -1911,15 +1808,10 @@ check_sgi_label(void) {
     /*
      * test for correct checksum
      */
-    if( two_s_complement_32bit_sum( (unsigned int*)sgilabel,
-                       sizeof(*sgilabel) ) )
-    {
-       fprintf( stderr, _("Detected sgi disklabel with wrong checksum.\n"));
-    } else
-    {
-       heads = sgi_get_ntrks();
-       cylinders = sgi_get_pcylcount();
-       sectors = sgi_get_nsect();
+    if (two_s_complement_32bit_sum((unsigned int*)sgilabel,
+                                      sizeof(*sgilabel))) {
+               fprintf(stderr,
+                       _("Detected sgi disklabel with wrong checksum.\n"));
     }
     update_units();
     sgi_label = 1;
@@ -1928,18 +1820,18 @@ check_sgi_label(void) {
     return 1;
 }
 
-static int
-sgi_get_start_sector( int i ) {
+static unsigned int
+sgi_get_start_sector(int i) {
     return SGI_SSWAP32(sgilabel->partitions[i].start_sector);
 }
 
-static int
-sgi_get_num_sectors( int i ) {
+static unsigned int
+sgi_get_num_sectors(int i) {
     return SGI_SSWAP32(sgilabel->partitions[i].num_sectors);
 }
 
 static int
-sgi_get_sysid( int i )
+sgi_get_sysid(int i)
 {
     return SGI_SSWAP32(sgilabel->partitions[i].id);
 }
@@ -1957,13 +1849,11 @@ sgi_get_swappartition(void)
 }
 
 static void
-sgi_list_table( int xtra ) {
-    int i, w;
+sgi_list_table(int xtra) {
+    int i, w, wd;
     int kpi = 0;                /* kernel partition ID */
 
-    w = strlen( disk_device );
-
-    if( xtra ) {
+    if(xtra) {
        printf(_("\nDisk %s (SGI disk label): %d heads, %d sectors\n"
               "%d cylinders, %d physical cylinders\n"
               "%d extra sects/cyl, interleave %d:1\n"
@@ -1982,9 +1872,15 @@ sgi_list_table( int xtra ) {
                disk_device, heads, sectors, cylinders,
                str_units(PLURAL), units_per_sector );
     }
+
+    w = strlen(disk_device);
+    wd = strlen(_("Device"));
+    if (w < wd)
+       w = wd;
+
     printf(_("----- partitions -----\n"
           "Pt# %*s  Info     Start       End   Sectors  Id  System\n"),
-          w + 1, _("Device"));
+           w + 2, _("Device"));
     for (i = 0 ; i < partitions; i++) {
            if( sgi_get_num_sectors(i) || debug ) {
            uint32_t start = sgi_get_start_sector(i);
@@ -1993,7 +1889,7 @@ sgi_list_table( int xtra ) {
            printf(
                "%2d: %s %4s %9ld %9ld %9ld  %2x  %s\n",
 /* fdisk part number */   i+1,
-/* device */              partname(disk_device, kpi, w+2),
+/* device */              partname(disk_device, kpi, w+3),
 /* flags */               (sgi_get_swappartition() == i) ? "swap" :
 /* flags */               (sgi_get_bootpartition() == i) ? "boot" : "    ",
 /* start */               (long) scround(start),
@@ -2005,14 +1901,13 @@ sgi_list_table( int xtra ) {
     }
     printf(_("----- Bootinfo -----\nBootfile: %s\n"
             "----- Directory Entries -----\n"),
-          sgilabel->boot_file );
-    for (i = 0 ; i < sgi_volumes; i++)
-    {
-       if (sgilabel->directory[i].vol_file_size)
-       {
+              sgilabel->boot_file);
+       for (i = 0 ; i < sgi_volumes; i++) {
+           if (sgilabel->directory[i].vol_file_size) {
            uint32_t start = SGI_SSWAP32(sgilabel->directory[i].vol_file_start);
            uint32_t len = SGI_SSWAP32(sgilabel->directory[i].vol_file_size);
            char*name = sgilabel->directory[i].vol_file_name;
+
            printf(_("%2d: %-10s sector%5u size%8u\n"),
                    i, name, (unsigned int) start, (unsigned int) len);
        }
@@ -2025,7 +1920,7 @@ sgi_set_bootpartition( int i )
     sgilabel->boot_part = SGI_SSWAP16(((short)i));
 }
 
-static int
+static unsigned int
 sgi_get_lastblock(void) {
     return heads * sectors * cylinders;
 }
@@ -2036,28 +1931,29 @@ sgi_set_swappartition( int i ) {
 }
 
 static int
-sgi_check_bootfile( const char* aFile ) {
-    if( strlen( aFile ) < 3 ) /* "/a\n" is minimum */
-    {
-       printf( _("\nInvalid Bootfile!\n"
+sgi_check_bootfile(const char* aFile) {
+
+       if (strlen(aFile) < 3) /* "/a\n" is minimum */ {
+               printf(_("\nInvalid Bootfile!\n"
                "\tThe bootfile must be an absolute non-zero pathname,\n"
-               "\te.g. \"/unix\" or \"/unix.save\".\n") );
+                        "\te.g. \"/unix\" or \"/unix.save\".\n"));
        return 0;
-    } else
-    if( strlen( aFile ) > 16 )
-    {
-       printf( _("\n\tName of Bootfile too long:  16 bytes maximum.\n") );
+       } else {
+               if (strlen(aFile) > 16) {
+                       printf(_("\n\tName of Bootfile too long:  "
+                                "16 bytes maximum.\n"));
        return 0;
-    } else
-    if( aFile[0] != '/' )
-    {
-       printf( _("\n\tBootfile must have a fully qualified pathname.\n") );
+               } else {
+                       if (aFile[0] != '/') {
+                               printf(_("\n\tBootfile must have a "
+                                        "fully qualified pathname.\n"));
        return 0;
     }
-    if( strncmp( aFile, sgilabel->boot_file, 16 ) )
-    {
-       printf( _("\n\tBe aware, that the bootfile is not checked for existence.\n\t"
-               "SGI's default is \"/unix\" and for backup \"/unix.save\".\n") );
+               }
+       }
+       if (strncmp(aFile, sgilabel->boot_file, 16)) {
+               printf(_("\n\tBe aware, that the bootfile is not checked for existence.\n\t"
+                        "SGI's default is \"/unix\" and for backup \"/unix.save\".\n"));
        /* filename is correct and did change */
        return 1;
     }
@@ -2070,21 +1966,19 @@ sgi_get_bootfile(void) {
 }
 
 static void
-sgi_set_bootfile( const char* aFile )
-{
+sgi_set_bootfile(const char* aFile) {
     int i = 0;
-    if( sgi_check_bootfile( aFile ) )
-    {
-       while( i<16 )
-       {
-           if( (aFile[i] != '\n')      /* in principle caught again by next line */
-           &&  (strlen( aFile ) > i ) )
+
+    if (sgi_check_bootfile(aFile)) {
+       while (i < 16) {
+           if ((aFile[i] != '\n')  /* in principle caught again by next line */
+                           &&  (strlen(aFile) > i))
                sgilabel->boot_file[i] = aFile[i];
            else
                sgilabel->boot_file[i] = 0;
            i++;
        }
-       printf( _("\n\tBootfile is changed to \"%s\".\n"), sgilabel->boot_file );
+       printf(_("\n\tBootfile is changed to \"%s\".\n"), sgilabel->boot_file);
     }
 }
 
@@ -2092,200 +1986,178 @@ static void
 create_sgiinfo(void)
 {
     /* I keep SGI's habit to write the sgilabel to the second block */
-    sgilabel->directory[0].vol_file_start = SGI_SSWAP32( 2 );
-    sgilabel->directory[0].vol_file_size = SGI_SSWAP32( sizeof( sgiinfo ) );
-    strncpy( sgilabel->directory[0].vol_file_name, "sgilabel", 8 );
+    sgilabel->directory[0].vol_file_start = SGI_SSWAP32(2);
+    sgilabel->directory[0].vol_file_size = SGI_SSWAP32(sizeof(sgiinfo));
+    strncpy(sgilabel->directory[0].vol_file_name, "sgilabel", 8);
 }
 
-static sgiinfo * fill_sgiinfo(void);
+static sgiinfo *fill_sgiinfo(void);
 
 static void
-sgi_write_table(void)
-{
+sgi_write_table(void) {
     sgilabel->csum = 0;
-    sgilabel->csum = SGI_SSWAP32( two_s_complement_32bit_sum(
+     sgilabel->csum = SGI_SSWAP32(two_s_complement_32bit_sum(
                                 (unsigned int*)sgilabel,
-                                sizeof(*sgilabel) ) );
-    assert( two_s_complement_32bit_sum(
-           (unsigned int*)sgilabel, sizeof(*sgilabel) ) == 0 );
-    if( lseek(fd, 0, SEEK_SET) < 0 )
+                                       sizeof(*sgilabel)));
+     assert(two_s_complement_32bit_sum(
+               (unsigned int*)sgilabel, sizeof(*sgilabel)) == 0);
+     if (lseek(fd, 0, SEEK_SET) < 0)
        fdisk_fatal(unable_to_seek);
-    if( write(fd, sgilabel, SECTOR_SIZE) != SECTOR_SIZE )
+     if (write(fd, sgilabel, SECTOR_SIZE) != SECTOR_SIZE)
        fdisk_fatal(unable_to_write);
-    if( ! strncmp( sgilabel->directory[0].vol_file_name, "sgilabel",8 ) )
-    {
+     if (! strncmp(sgilabel->directory[0].vol_file_name, "sgilabel", 8)) {
        /*
-        * keep this habbit of first writing the "sgilabel".
+        * keep this habit of first writing the "sgilabel".
         * I never tested whether it works without (AN 981002).
         */
-       sgiinfo*info = fill_sgiinfo();  /* fills the block appropriately */
-       int infostartblock = SGI_SSWAP32( sgilabel->directory[0].vol_file_start );
-       if( ext2_llseek(fd, (ext2_loff_t)infostartblock*
-           SECTOR_SIZE, SEEK_SET) < 0 )
+        sgiinfo *info = fill_sgiinfo();
+        int infostartblock = SGI_SSWAP32(sgilabel->directory[0].vol_file_start);
+        if (lseek(fd, infostartblock*SECTOR_SIZE, SEEK_SET) < 0)
            fdisk_fatal(unable_to_seek);
-       if( write(fd, info, SECTOR_SIZE) != SECTOR_SIZE )
+        if (write(fd, info, SECTOR_SIZE) != SECTOR_SIZE)
            fdisk_fatal(unable_to_write);
-       free( info );
+        free(info);
     }
 }
 
 static int
-compare_start( int *x, int *y ) {
+compare_start(int *x, int *y) {
     /*
      * sort according to start sectors
      * and prefers largest partition:
      * entry zero is entire disk entry
      */
-    int i = *x;
-    int j = *y;
-    int a = sgi_get_start_sector(i);
-    int b = sgi_get_start_sector(j);
-    int c = sgi_get_num_sectors(i);
-    int d = sgi_get_num_sectors(j);
-    if( a == b )
-    {
-       return( d - c );
-    }
-    return( a - b );
+    unsigned int i = *x;
+    unsigned int j = *y;
+    unsigned int a = sgi_get_start_sector(i);
+    unsigned int b = sgi_get_start_sector(j);
+    unsigned int c = sgi_get_num_sectors(i);
+    unsigned int d = sgi_get_num_sectors(j);
+
+    if (a == b)
+       return (d > c) ? 1 : (d == c) ? 0 : -1;
+    return (a > b) ? 1 : -1;
 }
 
 
 static int
-verify_sgi( int verbose )
+verify_sgi(int verbose)
 {
     int Index[16];      /* list of valid partitions */
     int sortcount = 0;  /* number of used partitions, i.e. non-zero lengths */
-    int entire = 0, i = 0;      /* local counters */
-    int start = 0;
-    int gap = 0;        /* count unused blocks */
-    int lastblock = sgi_get_lastblock();
-    /*
-     */
+    int entire = 0, i = 0;
+    unsigned int start = 0;
+    long long gap = 0;      /* count unused blocks */
+    unsigned int lastblock = sgi_get_lastblock();
+
     clearfreelist();
-    for( i=0; i<16; i++ )
-    {
-       if( sgi_get_num_sectors(i)!=0 )
-       {
+    for (i=0; i<16; i++) {
+       if (sgi_get_num_sectors(i) != 0) {
            Index[sortcount++]=i;
-           if( sgi_get_sysid(i) == ENTIRE_DISK )
-           {
-               if( entire++ == 1 )
-               {
-                   if(verbose)
+           if (sgi_get_sysid(i) == ENTIRE_DISK) {
+               if (entire++ == 1) {
+                   if (verbose)
                        printf(_("More than one entire disk entry present.\n"));
                }
            }
        }
     }
-    if( sortcount == 0 )
-    {
-       if(verbose)
+    if (sortcount == 0) {
+       if (verbose)
            printf(_("No partitions defined\n"));
-       return lastblock;
+              return (lastblock > 0) ? 1 : (lastblock == 0) ? 0 : -1;
     }
-    qsort( Index, sortcount, sizeof(Index[0]), (void*)compare_start );
-    if( sgi_get_sysid( Index[0] ) == ENTIRE_DISK )
-    {
-       if( ( Index[0] != 10 ) && verbose )
-           printf( _("IRIX likes when Partition 11 covers the entire disk.\n") );
-       if( ( sgi_get_start_sector( Index[0] ) != 0 ) && verbose )
-           printf( _("The entire disk partition should start at block 0,\nnot "
-                   "at diskblock %d.\n"), sgi_get_start_sector(Index[0] ) );
-    if(debug)   /* I do not understand how some disks fulfil it */
-       if( ( sgi_get_num_sectors( Index[0] ) != lastblock ) && verbose )
-           printf( _("The entire disk partition is only %d diskblock large,\n"
+    qsort(Index, sortcount, sizeof(Index[0]), (void*)compare_start);
+    if (sgi_get_sysid(Index[0]) == ENTIRE_DISK) {
+       if ((Index[0] != 10) && verbose)
+           printf(_("IRIX likes when Partition 11 covers the entire disk.\n"));
+           if ((sgi_get_start_sector(Index[0]) != 0) && verbose)
+               printf(_("The entire disk partition should start "
+                               "at block 0,\n"
+                               "not at diskblock %d.\n"),
+                             sgi_get_start_sector(Index[0]));
+               if (debug)      /* I do not understand how some disks fulfil it */
+                      if ((sgi_get_num_sectors(Index[0]) != lastblock) && verbose)
+                              printf(_("The entire disk partition is only %d diskblock large,\n"
                    "but the disk is %d diskblocks long.\n"),
-                   sgi_get_num_sectors( Index[0] ), lastblock );
-       lastblock = sgi_get_num_sectors( Index[0] );
-    } else
-    {
-       if( verbose )
-           printf( _("One Partition (#11) should cover the entire disk.\n") );
-       if(debug>2)
-           printf( "sysid=%d\tpartition=%d\n",
-                       sgi_get_sysid( Index[0] ), Index[0]+1 );
+                                     sgi_get_num_sectors(Index[0]), lastblock);
+               lastblock = sgi_get_num_sectors(Index[0]);
+    } else {
+           if (verbose)
+               printf(_("One Partition (#11) should cover the entire disk.\n"));
+           if (debug>2)
+               printf("sysid=%d\tpartition=%d\n",
+                             sgi_get_sysid(Index[0]), Index[0]+1);
     }
-    for( i=1, start=0; i<sortcount; i++ )
-    {
+    for (i=1, start=0; i<sortcount; i++) {
        int cylsize = sgi_get_nsect() * sgi_get_ntrks();
-       if( (sgi_get_start_sector( Index[i] ) % cylsize) != 0 )
-       {
-       if(debug)       /* I do not understand how some disks fulfil it */
-           if( verbose )
-               printf( _("Partition %d does not start on cylinder boundary.\n"),
-                       Index[i]+1 );
-       }
-       if( sgi_get_num_sectors( Index[i] ) % cylsize != 0 )
-       {
-       if(debug)       /* I do not understand how some disks fulfil it */
-           if( verbose )
-               printf( _("Partition %d does not end on cylinder boundary.\n"),
-                       Index[i]+1 );
+
+           if ((sgi_get_start_sector(Index[i]) % cylsize) != 0) {
+               if (debug)      /* I do not understand how some disks fulfil it */
+                   if (verbose)
+                       printf(_("Partition %d does not start on cylinder boundary.\n"),
+                                             Index[i]+1);
+           }
+           if (sgi_get_num_sectors(Index[i]) % cylsize != 0) {
+               if (debug)      /* I do not understand how some disks fulfil it */
+                   if (verbose)
+                       printf(_("Partition %d does not end on cylinder boundary.\n"),
+                                             Index[i]+1);
        }
        /* We cannot handle several "entire disk" entries. */
-       if( sgi_get_sysid( Index[i] ) == ENTIRE_DISK ) continue;
-       if( start > sgi_get_start_sector( Index[i] ) )
-       {
-           if( verbose )
-               printf( _("The Partition %d and %d overlap by %d sectors.\n"),
+           if (sgi_get_sysid(Index[i]) == ENTIRE_DISK) continue;
+           if (start > sgi_get_start_sector(Index[i])) {
+               if (verbose)
+                   printf(_("The Partition %d and %d overlap by %d sectors.\n"),
                        Index[i-1]+1, Index[i]+1,
-                       start - sgi_get_start_sector( Index[i] ) );
-           if( gap >  0 ) gap = -gap;
-           if( gap == 0 ) gap = -1;
-       }
-       if( start < sgi_get_start_sector( Index[i] ) )
-       {
-           if( verbose )
-               printf( _("Unused gap of %8d sectors - sectors %8d-%d\n"),
-                       sgi_get_start_sector( Index[i] ) - start,
-                       start, sgi_get_start_sector( Index[i] )-1 );
-           gap += sgi_get_start_sector( Index[i] ) - start;
-           add2freelist( start, sgi_get_start_sector( Index[i] ) );
-       }
-       start = sgi_get_start_sector( Index[i] )
-             + sgi_get_num_sectors(  Index[i] );
-       if(debug>1)
-       {
-           if( verbose )
-               printf( "%2d:%12d\t%12d\t%12d\n", Index[i],
+                               start - sgi_get_start_sector(Index[i]));
+               if (gap >  0) gap = -gap;
+               if (gap == 0) gap = -1;
+           }
+           if (start < sgi_get_start_sector(Index[i])) {
+               if (verbose)
+                   printf(_("Unused gap of %8u sectors - sectors %8u-%u\n"),
+                               sgi_get_start_sector(Index[i]) - start,
+                               start, sgi_get_start_sector(Index[i])-1);
+               gap += sgi_get_start_sector(Index[i]) - start;
+               add2freelist(start, sgi_get_start_sector(Index[i]));
+           }
+           start = sgi_get_start_sector(Index[i])
+                      + sgi_get_num_sectors(Index[i]);
+           if (debug > 1) {
+               if (verbose)
+                   printf("%2d:%12d\t%12d\t%12d\n", Index[i],
                        sgi_get_start_sector(Index[i]),
                        sgi_get_num_sectors(Index[i]),
-                       sgi_get_sysid(Index[i]) );
+                               sgi_get_sysid(Index[i]));
        }
     }
-    if( ( start < lastblock ) )
-    {
-       if( verbose )
-           printf( _("Unused gap of %8d sectors - sectors %8d-%d\n"),
-                   lastblock - start, start, lastblock-1 );
+    if (start < lastblock) {
+       if (verbose)
+               printf(_("Unused gap of %8u sectors - sectors %8u-%u\n"),
+                           lastblock - start, start, lastblock-1);
        gap += lastblock - start;
-       add2freelist( start, lastblock );
+       add2freelist(start, lastblock);
     }
     /*
      * Done with arithmetics
      * Go for details now
      */
-    if( verbose )
-    {
-       if( !sgi_get_num_sectors( sgi_get_bootpartition() ) )
-       {
-           printf( _("\nThe boot partition does not exist.\n") );
-       }
-       if( !sgi_get_num_sectors( sgi_get_swappartition() ) )
-       {
-           printf( _("\nThe swap partition does not exist.\n") );
-       } else
-       if( ( sgi_get_sysid( sgi_get_swappartition() ) != SGI_SWAP )
-       &&  ( sgi_get_sysid( sgi_get_swappartition() ) != LINUX_SWAP ) )
-       {
-           printf( _("\nThe swap partition has no swap type.\n") );
+    if (verbose) {
+       if (!sgi_get_num_sectors(sgi_get_bootpartition())) {
+           printf(_("\nThe boot partition does not exist.\n"));
        }
-       if( sgi_check_bootfile( "/unix" ) )
-       {
-           printf( _("\tYou have chosen an unusual boot file name.\n") );
+       if (!sgi_get_num_sectors(sgi_get_swappartition())) {
+           printf(_("\nThe swap partition does not exist.\n"));
+       } else {
+           if ((sgi_get_sysid(sgi_get_swappartition()) != SGI_SWAP)
+                       &&  (sgi_get_sysid(sgi_get_swappartition()) != LINUX_SWAP))
+               printf(_("\nThe swap partition has no swap type.\n"));
        }
+       if (sgi_check_bootfile("/unix"))
+           printf(_("\tYou have chosen an unusual boot file name.\n"));
     }
-    return gap;
+    return (gap > 0) ? 1 : (gap == 0) ? 0 : -1;
 }
 
 static int
@@ -2325,32 +2197,32 @@ sgi_change_sysid( int i, int sys )
 /* returns partition index of first entry marked as entire disk */
 static int
 sgi_entire(void) {
-    int i=0;
-    for( i=0; i<16; i++ )
-       if( sgi_get_sysid(i) == SGI_VOLUME )
+    int i;
+
+    for(i=0; i<16; i++)
+       if(sgi_get_sysid(i) == SGI_VOLUME)
            return i;
     return -1;
 }
 
 static void
-sgi_set_partition( int i, uint start, uint length, int sys ) {
-    sgilabel->partitions[i].id =
-           SGI_SSWAP32( sys );
-    sgilabel->partitions[i].num_sectors =
-           SGI_SSWAP32( length );
-    sgilabel->partitions[i].start_sector =
-           SGI_SSWAP32( start );
+sgi_set_partition(int i, unsigned int start, unsigned int length, int sys) {
+
+    sgilabel->partitions[i].id = SGI_SSWAP32(sys);
+    sgilabel->partitions[i].num_sectors = SGI_SSWAP32(length);
+    sgilabel->partitions[i].start_sector = SGI_SSWAP32(start);
     set_changed(i);
-    if( sgi_gaps() < 0 )        /* rebuild freelist */
+    if (sgi_gaps() < 0)     /* rebuild freelist */
        printf(_("Do You know, You got a partition overlap on the disk?\n"));
 }
 
 static void
 sgi_set_entire(void) {
     int n;
-    for( n=10; n<partitions; n++ ) {
-       if(!sgi_get_num_sectors( n ) ) {
-           sgi_set_partition( n, 0, sgi_get_lastblock(), SGI_VOLUME );
+
+    for(n=10; n < partitions; n++) {
+       if(!sgi_get_num_sectors(n) ) {
+           sgi_set_partition(n, 0, sgi_get_lastblock(), SGI_VOLUME);
            break;
        }
     }
@@ -2387,34 +2259,28 @@ static void
 sgi_add_partition( int n, int sys )
 {
     char mesg[256];
-    int first=0, last=0;
+    unsigned int first=0, last=0;
 
     if( n == 10 ) {
        sys = SGI_VOLUME;
     } else if ( n == 8 ) {
        sys = 0;
     }
-    if( sgi_get_num_sectors(n) )
-    {
+    if(sgi_get_num_sectors(n)) {
        printf(_("Partition %d is already defined.  Delete "
                "it before re-adding it.\n"), n + 1);
        return;
     }
-    if( (sgi_entire() == -1)
-    &&  (sys != SGI_VOLUME) )
-    {
+    if( (sgi_entire() == -1) &&  (sys != SGI_VOLUME) ) {
        printf(_("Attempting to generate entire disk entry automatically.\n"));
        sgi_set_entire();
        sgi_set_volhdr();
     }
-    if( (sgi_gaps() == 0)
-    &&  (sys != SGI_VOLUME) )
-    {
+    if( (sgi_gaps() == 0) &&  (sys != SGI_VOLUME) ) {
        printf(_("The entire disk is already covered with partitions.\n"));
        return;
     }
-    if( sgi_gaps() < 0 )
-    {
+    if(sgi_gaps() < 0) {
        printf(_("You got a partition overlap on the disk. Fix it first!\n"));
        return;
     }
@@ -2458,48 +2324,62 @@ sgi_add_partition( int n, int sys )
     sgi_set_partition( n, first, last-first, sys );
 }
 
+#ifdef CONFIG_FEATURE_FDISK_ADVANCED
 static void
 create_sgilabel(void)
 {
     struct hd_geometry geometry;
-    struct { int start;
-            int nsect;
-            int sysid; } old[4];
+    struct {
+       unsigned int start;
+       unsigned int nsect;
+       int sysid;
+    } old[4];
     int i=0;
+    long longsectors;               /* the number of sectors on the device */
+    int res;                        /* the result from the ioctl */
+    int sec_fac;                    /* the sector factor */
+
+    sec_fac = sector_size / 512;    /* determine the sector factor */
+
     fprintf( stderr,
        _("Building a new SGI disklabel. Changes will remain in memory only,\n"
        "until you decide to write them. After that, of course, the previous\n"
        "content will be unrecoverably lost.\n\n"));
 
     sgi_other_endian = (BYTE_ORDER == LITTLE_ENDIAN);
-
-#ifdef HDIO_REQ
-    if (!ioctl(fd, HDIO_REQ, &geometry))
-#else
-    if (!ioctl(fd, HDIO_GETGEO, &geometry))
-#endif
-    {
+    res = ioctl(fd, BLKGETSIZE, &longsectors);
+    if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
        heads = geometry.heads;
        sectors = geometry.sectors;
+       if (res == 0) {
+           /* the get device size ioctl was successful */
+           cylinders = longsectors / (heads * sectors);
+           cylinders /= sec_fac;
+       } else {
+           /* otherwise print error and use truncated version */
        cylinders = geometry.cylinders;
+           fprintf(stderr,
+                  _("Warning:  BLKGETSIZE ioctl failed on %s.  "
+                    "Using geometry cylinder value of %d.\n"
+                    "This value may be truncated for devices"
+                    " > 33.8 GB.\n"), disk_device, cylinders);
     }
-    for (i = 0; i < 4; i++)
-    {
+    }
+    for (i = 0; i < 4; i++) {
        old[i].sysid = 0;
-       if( valid_part_table_flag(MBRbuffer) )
-       {
-           if( get_part_table(i)->sys_ind )
-           {
+       if(valid_part_table_flag(MBRbuffer)) {
+           if(get_part_table(i)->sys_ind) {
                old[i].sysid = get_part_table(i)->sys_ind;
-               old[i].start = get_start_sect( get_part_table(i) );
-               old[i].nsect = get_nr_sects( get_part_table(i) );
-               printf( _("Trying to keep parameters of partition %d.\n"), i );
-               if( debug )
-                   printf( _("ID=%02x\tSTART=%d\tLENGTH=%d\n"),
-                           old[i].sysid, old[i].start, old[i].nsect );
+               old[i].start = get_start_sect(get_part_table(i));
+               old[i].nsect = get_nr_sects(get_part_table(i));
+               printf(_("Trying to keep parameters of partition %d.\n"), i);
+               if (debug)
+                   printf(_("ID=%02x\tSTART=%d\tLENGTH=%d\n"),
+                       old[i].sysid, old[i].start, old[i].nsect);
            }
        }
     }
+
     memset(MBRbuffer, 0, sizeof(MBRbuffer));
     sgilabel->magic = SGI_SSWAP32(SGI_LABEL_MAGIC);
     sgilabel->boot_part = SGI_SSWAP16(0);
@@ -2542,11 +2422,9 @@ create_sgilabel(void)
     sgi_volumes    = 15;
     sgi_set_entire();
     sgi_set_volhdr();
-    for (i = 0; i < 4; i++)
-    {
-       if( old[i].sysid )
-       {
-           sgi_set_partition( i, old[i].start, old[i].nsect, old[i].sysid );
+    for (i = 0; i < 4; i++) {
+       if(old[i].sysid) {
+           sgi_set_partition(i, old[i].start, old[i].nsect, old[i].sysid);
        }
     }
 }
@@ -2556,14 +2434,16 @@ sgi_set_xcyl(void)
 {
     /* do nothing in the beginning */
 }
+#endif /* CONFIG_FEATURE_FDISK_ADVANCED */
 
 /* _____________________________________________________________
  */
 
-static sgiinfo*
+static sgiinfo *
 fill_sgiinfo(void)
 {
-    sgiinfo*info=calloc( 1, sizeof(sgiinfo) );
+    sgiinfo *info = calloc(1, sizeof(sgiinfo));
+
     info->magic=SGI_SSWAP32(SGI_INFO_MAGIC);
     info->b1=SGI_SSWAP32(-1);
     info->b2=SGI_SSWAP16(-1);
@@ -2602,6 +2482,7 @@ static int     floppy;
 #ifndef IDE1_MAJOR
 #define IDE1_MAJOR 22
 #endif
+
 static void guess_device_type(void) {
        struct stat bootstat;
 
@@ -2609,12 +2490,12 @@ static void guess_device_type(void) {
                scsi_disk = 0;
                floppy = 0;
        } else if (S_ISBLK(bootstat.st_mode)
-                  && ((bootstat.st_rdev >> 8) == IDE0_MAJOR ||
-                      (bootstat.st_rdev >> 8) == IDE1_MAJOR)) {
+                  && (major(bootstat.st_rdev) == IDE0_MAJOR ||
+                      major(bootstat.st_rdev) == IDE1_MAJOR)) {
                scsi_disk = 0;
                floppy = 0;
        } else if (S_ISBLK(bootstat.st_mode)
-                  && (bootstat.st_rdev >> 8) == FLOPPY_MAJOR) {
+                  && major(bootstat.st_rdev) == FLOPPY_MAJOR) {
                scsi_disk = 0;
                floppy = 1;
        } else {
@@ -2840,11 +2721,7 @@ static void create_sunlabel(void)
            }
        }
        if (!p || floppy) {
-#ifdef HDIO_REQ
-           if (!ioctl(fd, HDIO_REQ, &geometry)) {
-#else
            if (!ioctl(fd, HDIO_GETGEO, &geometry)) {
-#endif
                heads = geometry.heads;
                sectors = geometry.sectors;
                cylinders = geometry.cylinders;
@@ -3150,15 +3027,16 @@ and is of type `Whole disk'\n");
 
 static void
 sun_delete_partition(int i) {
+       unsigned int nsec;
+
        if (i == 2 && sunlabel->infos[i].id == WHOLE_DISK &&
            !sunlabel->partitions[i].start_cylinder &&
-           SUN_SSWAP32(sunlabel->partitions[i].num_sectors)
+           (nsec = SUN_SSWAP32(sunlabel->partitions[i].num_sectors))
              == heads * sectors * cylinders)
                printf(_("If you want to maintain SunOS/Solaris compatibility, "
                       "consider leaving this\n"
                       "partition as Whole disk (5), starting at 0, with %u "
-                      "sectors\n"),
-                      (uint) SUN_SSWAP32(sunlabel->partitions[i].num_sectors));
+                      "sectors\n"), nsec);
        sunlabel->infos[i].id = 0;
        sunlabel->partitions[i].num_sectors = 0;
 }
@@ -3236,6 +3114,8 @@ sun_list_table(int xtra) {
        }
 }
 
+#ifdef CONFIG_FEATURE_FDISK_ADVANCED
+
 static void
 sun_set_alt_cyl(void) {
        sunlabel->nacyl =
@@ -3275,6 +3155,7 @@ sun_set_pcylcount(void) {
                SUN_SSWAP16(read_int(0, SUN_SSWAP16(sunlabel->pcylcount), 65535, 0,
                                 _("Number of physical cylinders")));
 }
+#endif /* CONFIG_FEATURE_FDISK_ADVANCED */
 
 static void
 sun_write_table(void) {
@@ -3436,19 +3317,19 @@ set_start_sect(struct partition *p, unsigned int start_sect) {
 }
 #endif
 
-static unsigned int
+static int32_t
 get_start_sect(const struct partition *p) {
        return read4_little_endian(p->start4);
 }
 
 #ifdef CONFIG_FEATURE_FDISK_WRITABLE
 static void
-set_nr_sects(struct partition *p, unsigned int nr_sects) {
+set_nr_sects(struct partition *p, int32_t nr_sects) {
        store4_little_endian(p->size4, nr_sects);
 }
 #endif
 
-static unsigned int
+static int32_t
 get_nr_sects(const struct partition *p) {
        return read4_little_endian(p->size4);
 }
@@ -3471,12 +3352,9 @@ static uint    user_cylinders, user_heads, user_sectors;
 static uint    pt_heads, pt_sectors;
 static uint    kern_heads, kern_sectors;
 
-static uint extended_offset;            /* offset of link pointers */
-static uint    sector_size = DEFAULT_SECTOR_SIZE,
-       user_set_sector_size,
-       sector_offset = 1;
+static off_t extended_offset;            /* offset of link pointers */
 
-static unsigned long total_number_of_sectors;
+static unsigned long long total_number_of_sectors;
 
 
 static jmp_buf listingbuf;
@@ -3515,15 +3393,15 @@ static void fdisk_fatal(enum failure why) {
 }
 
 static void
-seek_sector(uint secno) {
-       ext2_loff_t offset = (ext2_loff_t) secno * sector_size;
-       if (ext2_llseek(fd, offset, SEEK_SET) == (ext2_loff_t) -1)
+seek_sector(off_t secno) {
+       off_t offset = secno * sector_size;
+       if (lseek(fd, offset, SEEK_SET) == (off_t) -1)
                fdisk_fatal(unable_to_seek);
 }
 
 #ifdef CONFIG_FEATURE_FDISK_WRITABLE
 static void
-write_sector(uint secno, char *buf) {
+write_sector(off_t secno, char *buf) {
        seek_sector(secno);
        if (write(fd, buf, sector_size) != sector_size)
                fdisk_fatal(unable_to_write);
@@ -3532,7 +3410,7 @@ write_sector(uint secno, char *buf) {
 
 /* Allocate a buffer and read a partition table sector */
 static void
-read_pte(struct pte *pe, uint offset) {
+read_pte(struct pte *pe, off_t offset) {
 
        pe->offset = offset;
        pe->sectorbuffer = (char *) xmalloc(sector_size);
@@ -3809,9 +3687,9 @@ clear_partition(struct partition *p) {
 
 #ifdef CONFIG_FEATURE_FDISK_WRITABLE
 static void
-set_partition(int i, int doext, uint start, uint stop, int sysid) {
+set_partition(int i, int doext, off_t start, off_t stop, int sysid) {
        struct partition *p;
-       uint offset;
+       off_t offset;
 
        if (doext) {
                p = ptes[i].ext_pointer;
@@ -4083,7 +3961,7 @@ get_partition_table_geometry(void) {
 void
 get_geometry(void) {
        int sec_fac;
-       unsigned long longsectors;
+       unsigned long long bytes;       /* really u64 */
 
        get_sectorsize();
        sec_fac = sector_size / 512;
@@ -4103,20 +3981,25 @@ get_geometry(void) {
        sectors = user_sectors ? user_sectors :
                pt_sectors ? pt_sectors :
                kern_sectors ? kern_sectors : 63;
+       if (ioctl(fd, BLKGETSIZE64, &bytes) == 0) {
+               /* got bytes */
+       } else {
+               unsigned long longsectors;
 
        if (ioctl(fd, BLKGETSIZE, &longsectors))
                longsectors = 0;
+                       bytes = ((unsigned long long) longsectors) << 9;
+       }
+
+       total_number_of_sectors = (bytes >> 9);
 
        sector_offset = 1;
        if (dos_compatible_flag)
                sector_offset = sectors;
 
-       cylinders = longsectors / (heads * sectors);
-       cylinders /= sec_fac;
+       cylinders = total_number_of_sectors / (heads * sectors * sec_fac);
        if (!cylinders)
                cylinders = user_cylinders;
-
-       total_number_of_sectors = longsectors;
 }
 
 /*
@@ -4302,10 +4185,10 @@ read_int(uint low, uint dflt, uint high, uint base, char *mesg)
                default_ok = 0;
 
        if (default_ok)
-               snprintf(ms, mslen, _("%s (%d-%d, default %d): "),
+               snprintf(ms, mslen, _("%s (%u-%u, default %u): "),
                         mesg, low, high, dflt);
        else
-               snprintf(ms, mslen, "%s (%d-%d): ",
+               snprintf(ms, mslen, "%s (%u-%u): ",
                         mesg, low, high);
 
        while (1) {
@@ -4316,7 +4199,7 @@ read_int(uint low, uint dflt, uint high, uint base, char *mesg)
                       && *line_ptr != '-' && *line_ptr != '+')
                        continue;
 
-               if (*line_ptr == '+' || *line_ptr == '-') {
+               if (*line_ptr == '+' || *line_ptr == '-') {
                       int minus = (*line_ptr == '-');
                       int absolute = 0;
 
@@ -4324,15 +4207,17 @@ read_int(uint low, uint dflt, uint high, uint base, char *mesg)
 
                       while (isdigit(*++line_ptr))
                               use_default = 0;
-           
+
                       switch (*line_ptr) {
                               case 'c':
                               case 'C':
                                        if (!display_in_cyl_units)
                                                i *= heads * sectors;
                                        break;
-                              case 'k':
                               case 'K':
+                                       absolute = 1024;
+                                       break;
+                               case 'k':
                                       absolute = 1000;
                                       break;
                               case 'm':
@@ -4352,14 +4237,14 @@ read_int(uint low, uint dflt, uint high, uint base, char *mesg)
 
                               bytes = (unsigned long long) i * absolute;
                               unit = sector_size * units_per_sector;
-                              bytes += unit/2; /* round */
+                              bytes += unit/2; /* round */
                               bytes /= unit;
                               i = bytes;
                        }
                       if (minus)
                               i = -i;
                       i += base;
-               } else {
+               } else {
                        i = atoi(line_ptr);
                        while (isdigit(*line_ptr)) {
                                line_ptr++;
@@ -4367,7 +4252,7 @@ read_int(uint low, uint dflt, uint high, uint base, char *mesg)
                        }
                }
                if (use_default)
-                       printf(_("Using default value %d\n"), i = dflt);
+                       printf(_("Using default value %u\n"), i = dflt);
                if (i >= low && i <= high)
                        break;
                else
@@ -4570,7 +4455,18 @@ change_sysid(void) {
        int i, sys, origsys;
        struct partition *p;
 
+#ifdef CONFIG_FEATURE_SGI_LABEL
+       /* If sgi_label then don't use get_existing_partition,
+          let the user select a partition, since get_existing_partition()
+          only works for Linux like partition tables. */
+       if (!sgi_label) {
+       i = get_existing_partition(0, partitions);
+       } else {
+               i = get_partition(0, partitions);
+       }
+#else
        i = get_existing_partition(0, partitions);
+#endif
        if (i == -1)
                return;
        p = ptes[i].part_table;
@@ -4710,7 +4606,7 @@ static void check_consistency(const struct partition *p, int partition) {
 
 /* Ending on cylinder boundary? */
        if (peh != (heads - 1) || pes != sectors) {
-               printf(_("Partition %i does not end on cylinder boundary.\n"),
+               printf(_("Partition %i does not end on cylinder boundary.\n"),
                        partition + 1);
 #if 0
                printf(_("     phys=(%d, %d, %d) "), pec, peh, pes);
@@ -4722,7 +4618,7 @@ static void check_consistency(const struct partition *p, int partition) {
 
 static void
 list_disk_geometry(void) {
-       long long bytes = (long long) total_number_of_sectors * 512;
+       long long bytes = (total_number_of_sectors << 9);
        long megabytes = bytes/1000000;
 
        if (megabytes < 10000)
@@ -4734,7 +4630,7 @@ list_disk_geometry(void) {
        printf(_("%d heads, %d sectors/track, %d cylinders"),
               heads, sectors, cylinders);
        if (units_per_sector == 1)
-               printf(_(", total %lu sectors"),
+               printf(_(", total %llu sectors"),
                       total_number_of_sectors / (sector_size/512));
        printf(_("\nUnits = %s of %d * %d = %d bytes\n\n"),
               str_units(PLURAL),
@@ -4750,7 +4646,7 @@ static int
 wrong_p_order(int *prev) {
        const struct pte *pe;
        const struct partition *p;
-       uint last_p_start_pos = 0, p_start_pos;
+       off_t last_p_start_pos = 0, p_start_pos;
        int i, last_i = 0;
 
        for (i = 0 ; i < partitions; i++) {
@@ -4924,8 +4820,8 @@ list_table(int xtra) {
 
                p = pe->part_table;
                if (p && !is_cleared_partition(p)) {
-                       unsigned int psects = get_nr_sects(p);
-                       unsigned int pblocks = psects;
+                       off_t psects = get_nr_sects(p);
+                       off_t pblocks = psects;
                        unsigned int podd = 0;
 
                        if (sector_size < 1024) {
@@ -4935,14 +4831,14 @@ list_table(int xtra) {
                        if (sector_size > 1024)
                                pblocks *= (sector_size / 1024);
                        printf(
-                           "%s  %c %9ld %9ld %9ld%c  %2x  %s\n",
+                           "%s  %c %11llu %11llu %11llu%c  %2x  %s\n",
                        partname(disk_device, i+1, w+2),
 /* boot flag */         !p->boot_ind ? ' ' : p->boot_ind == ACTIVE_FLAG
                        ? '*' : '?',
-/* start */             (long) cround(get_partition_start(pe)),
-/* end */               (long) cround(get_partition_start(pe) + psects
+/* start */             (unsigned long long) cround(get_partition_start(pe)),
+/* end */               (unsigned long long) cround(get_partition_start(pe) + psects
                                - (psects ? 1 : 0)),
-/* odd flag on end */   (long) pblocks, podd ? '+' : ' ',
+/* odd flag on end */   (unsigned long long) pblocks, podd ? '+' : ' ',
 /* type id */           p->sys_ind,
 /* type name */         partition_type(p->sys_ind));
                        check_consistency(p, i);
@@ -4971,7 +4867,7 @@ x_list_table(int extend) {
                pe = &ptes[i];
                p = (extend ? pe->ext_pointer : pe->part_table);
                if (p != NULL) {
-                       printf("%2d %02x%4d%4d%5d%4d%4d%5d%9d%9d %02x\n",
+                       printf("%2d %02x%4d%4d%5d%4d%4d%5d%11u%11u %02x\n",
                                i + 1, p->boot_ind, p->head,
                                sector(p->sector),
                                cylinder(p->sector, p->cyl), p->end_head,
@@ -4987,7 +4883,7 @@ x_list_table(int extend) {
 
 #ifdef CONFIG_FEATURE_FDISK_WRITABLE
 static void
-fill_bounds(uint *first, uint *last) {
+fill_bounds(off_t *first, off_t *last) {
        int i;
        const struct pte *pe = &ptes[0];
        const struct partition *p;
@@ -5005,8 +4901,8 @@ fill_bounds(uint *first, uint *last) {
 }
 
 static void
-check(int n, uint h, uint s, uint c, uint start) {
-       uint total, real_s, real_c;
+check(int n, uint h, uint s, uint c, off_t start) {
+       off_t total, real_s, real_c;
 
        real_s = sector(s) - 1;
        real_c = cylinder(s, c);
@@ -5021,19 +4917,19 @@ check(int n, uint h, uint s, uint c, uint start) {
                fprintf(stderr, _("Partition %d: sector %d greater than "
                        "maximum %d\n"), n, s, sectors);
        if (real_c >= cylinders)
-               fprintf(stderr, _("Partitions %d: cylinder %d greater than "
-                       "maximum %d\n"), n, real_c + 1, cylinders);
+               fprintf(stderr, _("Partitions %d: cylinder %llu greater than "
+                       "maximum %d\n"), n, (unsigned long long)real_c + 1, cylinders);
        if (cylinders <= 1024 && start != total)
                fprintf(stderr,
-                       _("Partition %d: previous sectors %d disagrees with "
-                       "total %d\n"), n, start, total);
+                       _("Partition %d: previous sectors %llu disagrees with "
+                       "total %llu\n"), n, (unsigned long long)start, (unsigned long long)total);
 }
 
 static void
 verify(void) {
        int i, j;
        uint total = 1;
-       uint first[partitions], last[partitions];
+       off_t first[partitions], last[partitions];
        struct partition *p;
 
        if (warn_geometry())
@@ -5080,7 +4976,7 @@ verify(void) {
 
        if (extended_offset) {
                struct pte *pex = &ptes[ext_index];
-               uint e_last = get_start_sect(pex->part_table) +
+               off_t e_last = get_start_sect(pex->part_table) +
                        get_nr_sects(pex->part_table) - 1;
 
                for (i = 4; i < partitions; i++) {
@@ -5111,7 +5007,8 @@ add_partition(int n, int sys) {
        int i, readed = 0;
        struct partition *p = ptes[n].part_table;
        struct partition *q = ptes[ext_index].part_table;
-       uint start, stop = 0, limit, temp,
+       long long llimit;
+       off_t start, stop = 0, limit, temp,
                first[partitions], last[partitions];
 
        if (p && p->sys_ind) {
@@ -5122,10 +5019,13 @@ add_partition(int n, int sys) {
        fill_bounds(first, last);
        if (n < 4) {
                start = sector_offset;
-               if (display_in_cyl_units)
-                       limit = heads * sectors * cylinders - 1;
+               if (display_in_cyl_units || !total_number_of_sectors)
+                       llimit = heads * sectors * cylinders - 1;
                else
-                       limit = total_number_of_sectors - 1;
+                       llimit = total_number_of_sectors - 1;
+               limit = llimit;
+               if (limit != llimit)
+                       limit = 0x7fffffff;
                if (extended_offset) {
                        first[ext_index] = extended_offset;
                        last[ext_index] = get_start_sect(q) +
@@ -5154,12 +5054,12 @@ add_partition(int n, int sys) {
                if (start > limit)
                        break;
                if (start >= temp+units_per_sector && readed) {
-                       printf(_("Sector %d is already allocated\n"), temp);
+                       printf(_("Sector %llu is already allocated\n"), (unsigned long long)temp);
                        temp = start;
                        readed = 0;
                }
                if (!readed && start == temp) {
-                       uint saved_start;
+                       off_t saved_start;
 
                        saved_start = start;
                        start = read_int(cround(saved_start), cround(saved_start), cround(limit),
@@ -5279,8 +5179,8 @@ new_partition(void) {
                free_primary += !ptes[i].part_table->sys_ind;
 
        if (!free_primary && partitions >= MAXIMUM_PARTS) {
-               printf(_("The maximum number of partitions has been created\n"));
-               return;
+               printf(_("The maximum number of partitions has been created\n"));
+               return;
        }
 
        if (!free_primary) {
@@ -5444,7 +5344,7 @@ static void
 move_begin(int i) {
        struct pte *pe = &ptes[i];
        struct partition *p = pe->part_table;
-       uint new, first;
+       off_t new, first;
 
        if (warn_geometry())
                return;
@@ -5896,7 +5796,20 @@ int fdisk_main(int argc, char **argv) {
                        break;
                case 'd':
                        {
-                               int j = get_existing_partition(1, partitions);
+                               int j;
+#ifdef CONFIG_FEATURE_SGI_LABEL
+                       /* If sgi_label then don't use get_existing_partition,
+                          let the user select a partition, since
+                          get_existing_partition() only works for Linux-like
+                          partition tables */
+                               if (!sgi_label) {
+                                       j = get_existing_partition(1, partitions);
+                               } else {
+                                       j = get_partition(1, partitions);
+                               }
+#else
+                               j = get_existing_partition(1, partitions);
+#endif
                                if (j >= 0)
                                        delete_partition(j);
                        }