remove unused files
[oweals/busybox.git] / util-linux / fdisk.c
index 6c28e02ec7505ff76add149a6d01e735e3c6f955..8580bec7036b755606f532dbc1c500afc2ea2d2a 100644 (file)
@@ -14,6 +14,7 @@
 
 #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 */
 #define BLKSSZGET  _IO(0x12,104)   /* get block device sector size */
 
 /* Avoid conflicts with the 2.6 kernel headers, which define
- * _IOR rather differently */ 
+ * _IOR rather differently */
 #undef _IOR
 #define _IOR(type,nr,size)      _IOC(_IOC_READ,(type),(nr),sizeof(size))
-#define BLKGETSIZE64 _IOR(0x12,114,8)  /* 8 = sizeof(u64) */
+#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(CONFIG_LFS) || defined(FDISK_SUPPORT_LARGE_DISKS) || defined(__alpha__) || defined(__ia64__) || defined(__s390x__)
-typedef long long fdisk_loff_t;
-#else
-typedef long fdisk_loff_t;
+#ifdef CONFIG_FEATURE_SUN_LABEL
+#define SCSI_IOCTL_GET_IDLUN 0x5382
 #endif
 
 
@@ -203,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,
@@ -259,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
@@ -276,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];
 
@@ -483,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
@@ -847,79 +840,6 @@ typedef struct {
 #define SUN_SSWAP32(x) (sun_other_endian ? __swap32(x) \
                                 : (uint32_t)(x))
 
-#if defined(FDISK_SUPPORT_LARGE_DISKS)
-/*
- * 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.
- */
-
-
-#include <syscall.h>
-
-
-#if defined(__alpha__) || defined(__ia64__) || defined(__s390x__)
-
-#define my_llseek lseek
-
-#else
-
-static int _llseek (unsigned int, unsigned long,
-                  unsigned long, fdisk_loff_t *, unsigned int);
-
-static _syscall5(int,_llseek,unsigned int,f_d,unsigned long,offset_high,
-                unsigned long, offset_low,fdisk_loff_t *,result,
-                unsigned int, origin)
-
-static fdisk_loff_t my_llseek (unsigned int f_d, fdisk_loff_t offset,
-               unsigned int origin)
-{
-       fdisk_loff_t result;
-       int retval;
-
-       retval = _llseek (f_d, ((unsigned long long) offset) >> 32,
-                       ((unsigned long long) offset) & 0xffffffff,
-                       &result, origin);
-       return (retval == -1 ? (fdisk_loff_t) retval : result);
-}
-
-#endif /* __alpha__ */
-
-
-static fdisk_loff_t fdisk_llseek (unsigned int f_d, fdisk_loff_t offset,
-                        unsigned int origin)
-{
-       fdisk_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(fdisk_loff_t)) ||
-           (offset < ((fdisk_loff_t) 1 << ((sizeof(off_t)*8) -1))))
-               return lseek(f_d, (off_t) offset, origin);
-
-       errno = EINVAL;
-       return -1;
-}
-#else
-# define fdisk_llseek lseek
-#endif  /* FDISK_SUPPORT_LARGE_DISKS */
-
-
 
 #ifdef CONFIG_FEATURE_OSF_LABEL
 /*
@@ -962,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];
@@ -1139,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;
 
@@ -1446,7 +1366,7 @@ xbsd_write_bootstrap (void)
   sector = get_start_sect(xbsd_part);
 #endif
 
-  if (fdisk_llseek (fd, (fdisk_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);
@@ -1614,7 +1534,7 @@ xbsd_readlabel (struct partition *p, struct xbsd_disklabel *d)
        sector = 0;
 #endif
 
-       if (fdisk_llseek (fd, (fdisk_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);
@@ -1660,12 +1580,12 @@ xbsd_writelabel (struct partition *p, struct xbsd_disklabel *d)
 
 #if defined (__alpha__) && BSD_LABELSECTOR == 0
   alpha_bootblock_checksum (disklabelbuffer);
-  if (fdisk_llseek (fd, (fdisk_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 (fdisk_llseek (fd, (fdisk_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)))
@@ -1720,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];
@@ -2092,8 +2012,7 @@ sgi_write_table(void) {
         */
         sgiinfo *info = fill_sgiinfo();
         int infostartblock = SGI_SSWAP32(sgilabel->directory[0].vol_file_start);
-        if (fdisk_llseek(fd, (long long)infostartblock*
-                               SECTOR_SIZE, SEEK_SET) < 0)
+        if (lseek(fd, infostartblock*SECTOR_SIZE, SEEK_SET) < 0)
            fdisk_fatal(unable_to_seek);
         if (write(fd, info, SECTOR_SIZE) != SECTOR_SIZE)
            fdisk_fatal(unable_to_write);
@@ -3398,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);
 }
@@ -3433,7 +3352,7 @@ 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 off_t extended_offset;            /* offset of link pointers */
 
 static unsigned long long total_number_of_sectors;
 
@@ -3474,15 +3393,15 @@ static void fdisk_fatal(enum failure why) {
 }
 
 static void
-seek_sector(uint secno) {
-       fdisk_loff_t offset = (fdisk_loff_t) secno * sector_size;
-       if (fdisk_llseek(fd, offset, SEEK_SET) == (fdisk_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);
@@ -3491,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);
@@ -3768,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;
@@ -4280,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;
 
@@ -4288,7 +4207,7 @@ 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':
@@ -4318,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++;
@@ -4687,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);
@@ -4727,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++) {
@@ -4901,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) {
@@ -4912,14 +4831,14 @@ list_table(int xtra) {
                        if (sector_size > 1024)
                                pblocks *= (sector_size / 1024);
                        printf(
-                           "%s  %c %11lu %11lu %11lu%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 */             (unsigned long) cround(get_partition_start(pe)),
-/* end */               (unsigned 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 */   (unsigned 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);
@@ -4964,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;
@@ -4982,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);
@@ -4998,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())
@@ -5057,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++) {
@@ -5089,7 +5008,7 @@ add_partition(int n, int sys) {
        struct partition *p = ptes[n].part_table;
        struct partition *q = ptes[ext_index].part_table;
        long long llimit;
-       uint start, stop = 0, limit, temp,
+       off_t start, stop = 0, limit, temp,
                first[partitions], last[partitions];
 
        if (p && p->sys_ind) {
@@ -5135,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),
@@ -5260,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) {
@@ -5425,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;