Fix some compiler warnings emitted by gcc-4.8.0
[oweals/busybox.git] / util-linux / fdisk.c
index da03e683eddcff7a54192177a6d5772ad81ec509..39eb27b470dd0df900b97493eb20b15b2822cadb 100644 (file)
@@ -18,7 +18,6 @@
 //usage:       "[-C CYLINDERS] [-H HEADS] [-S SECTORS] [-b SSZ] DISK"
 //usage:#define fdisk_full_usage "\n\n"
 //usage:       "Change partition table\n"
-//usage:     "\nOptions:"
 //usage:     "\n       -u              Start and End are in sectors (instead of cylinders)"
 //usage:     "\n       -l              Show partition table for each DISK, then exit"
 //usage:       IF_FEATURE_FDISK_BLKSIZE(
@@ -2543,6 +2542,35 @@ new_partition(void)
        }
 }
 
+static void
+reread_partition_table(int leave)
+{
+       int i;
+
+       printf("Calling ioctl() to re-read partition table\n");
+       sync();
+       /* Users with slow external USB disks on a 320MHz ARM system (year 2011)
+        * report that sleep is needed, otherwise BLKRRPART may fail with -EIO:
+        */
+       sleep(1);
+       i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
+                       "WARNING: rereading partition table "
+                       "failed, kernel still uses old table");
+#if 0
+       if (dos_changed)
+               printf(
+               "\nWARNING: If you have created or modified any DOS 6.x\n"
+               "partitions, please see the fdisk manual page for additional\n"
+               "information\n");
+#endif
+
+       if (leave) {
+               if (ENABLE_FEATURE_CLEAN_UP)
+                       close_dev_fd();
+               exit(i != 0);
+       }
+}
+
 static void
 write_table(void)
 {
@@ -2554,7 +2582,6 @@ write_table(void)
                                ptes[3].changed = 1;
                for (i = 3; i < g_partitions; i++) {
                        struct pte *pe = &ptes[i];
-
                        if (pe->changed) {
                                write_part_table_flag(pe->sectorbuffer);
                                write_sector(pe->offset_from_dev_start, pe->sectorbuffer);
@@ -2566,44 +2593,17 @@ write_table(void)
                sgi_write_table();
        }
        else if (LABEL_IS_SUN) {
-               int needw = 0;
-
-               for (i = 0; i < 8; i++)
-                       if (ptes[i].changed)
-                               needw = 1;
-               if (needw)
-                       sun_write_table();
+               for (i = 0; i < 8; i++) {
+                       if (ptes[i].changed) {
+                               sun_write_table();
+                               break;
+                       }
+               }
        }
 
-       printf("The partition table has been altered!\n\n");
+       printf("The partition table has been altered.\n");
        reread_partition_table(1);
 }
-
-static void
-reread_partition_table(int leave)
-{
-       int i;
-
-       printf("Calling ioctl() to re-read partition table\n");
-       sync();
-       /* sleep(2); Huh? */
-       i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
-                       "WARNING: rereading partition table "
-                       "failed, kernel still uses old table");
-#if 0
-       if (dos_changed)
-               printf(
-               "\nWARNING: If you have created or modified any DOS 6.x\n"
-               "partitions, please see the fdisk manual page for additional\n"
-               "information\n");
-#endif
-
-       if (leave) {
-               if (ENABLE_FEATURE_CLEAN_UP)
-                       close_dev_fd();
-               exit(i != 0);
-       }
-}
 #endif /* FEATURE_FDISK_WRITABLE */
 
 #if ENABLE_FEATURE_FDISK_ADVANCED
@@ -2846,13 +2846,37 @@ open_list_and_close(const char *device, int user_specified)
        close_dev_fd();
 }
 
+/* Is it a whole disk? The digit check is still useful
+   for Xen devices for example. */
+static int is_whole_disk(const char *disk)
+{
+       unsigned len;
+       int fd = open(disk, O_RDONLY);
+
+       if (fd != -1) {
+               struct hd_geometry geometry;
+               int err = ioctl(fd, HDIO_GETGEO, &geometry);
+               close(fd);
+               if (!err)
+                       return (geometry.start == 0);
+       }
+
+       /* Treat "nameN" as a partition name, not whole disk */
+       /* note: mmcblk0 should work from the geometry check above */
+       len = strlen(disk);
+       if (len != 0 && isdigit(disk[len - 1]))
+               return 0;
+
+       return 1;
+}
+
 /* for fdisk -l: try all things in /proc/partitions
    that look like a partition name (do not end in a digit) */
 static void
 list_devs_in_proc_partititons(void)
 {
        FILE *procpt;
-       char line[100], ptname[100], devname[120], *s;
+       char line[100], ptname[100], devname[120];
        int ma, mi, sz;
 
        procpt = fopen_or_warn("/proc/partitions", "r");
@@ -2861,13 +2885,10 @@ list_devs_in_proc_partititons(void)
                if (sscanf(line, " %u %u %u %[^\n ]",
                                &ma, &mi, &sz, ptname) != 4)
                        continue;
-               for (s = ptname; *s; s++)
-                       continue;
-               /* note: excluding '0': e.g. mmcblk0 is not a partition name! */
-               if (s[-1] >= '1' && s[-1] <= '9')
-                       continue;
+
                sprintf(devname, "/dev/%s", ptname);
-               open_list_and_close(devname, 0);
+               if (is_whole_disk(devname))
+                       open_list_and_close(devname, 0);
        }
 #if ENABLE_FEATURE_CLEAN_UP
        fclose(procpt);
@@ -3002,7 +3023,7 @@ int fdisk_main(int argc UNUSED_PARAM, char **argv)
                                printf("\nThe current boot file is: %s\n",
                                        sgi_get_bootfile());
                                if (read_maybe_empty("Please enter the name of the "
-                                                  "new boot file: ") == '\n')
+                                               "new boot file: ") == '\n')
                                        printf("Boot file unchanged\n");
                                else
                                        sgi_set_bootfile(line_ptr);
@@ -3080,7 +3101,7 @@ int fdisk_main(int argc UNUSED_PARAM, char **argv)
                        verify();
                        break;
                case 'w':
-                       write_table();          /* does not return */
+                       write_table();  /* does not return */
                        break;
 #if ENABLE_FEATURE_FDISK_ADVANCED
                case 'x':