]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdisk: improve list partition types
authorKarel Zak <kzak@redhat.com>
Mon, 24 Sep 2012 11:24:09 +0000 (13:24 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 27 Sep 2012 11:38:13 +0000 (13:38 +0200)
 - add fdisk_parttype->typestr for types like GPT UUID or Mac
 - list typestr if specified

Signed-off-by: Karel Zak <kzak@redhat.com>
fdisks/fdisk.c
fdisks/fdisk.h
fdisks/fdiskbsdlabel.c

index fe199669807c9a9b1a4fc85319b96cbffcc467d2..b7e350f75583c0edd22d08c0f034f54a83551b60 100644 (file)
@@ -241,38 +241,61 @@ char *partition_type(struct fdisk_context *cxt, unsigned char type)
        return NULL;
 }
 
-void list_types(struct fdisk_context *cxt)
+void list_partition_types(struct fdisk_context *cxt)
 {
-       struct fdisk_parttype *sys = cxt->label->parttypes;
-       unsigned int last[4], done = 0, next = 0, size;
+       struct fdisk_parttype *types;
        int i;
 
-       for (i = 0; sys[i].name; i++);
-       size = i;
+       if (!cxt || !cxt->label || !cxt->label->parttypes)
+               return;
 
-       for (i = 3; i >= 0; i--)
-               last[3 - i] = done += (size + i - done) / (i + 1);
-       i = done = 0;
+       types = cxt->label->parttypes;
 
-       do {
-               #define NAME_WIDTH 15
-               char name[NAME_WIDTH * MB_LEN_MAX];
-               size_t width = NAME_WIDTH;
-
-               printf("%c%2x  ", i ? ' ' : '\n', sys[next].type);
-               size_t ret = mbsalign(_(sys[next].name), name, sizeof(name),
-                                     &width, MBS_ALIGN_LEFT, 0);
-               if (ret == (size_t)-1 || ret >= sizeof(name))
-                       printf("%-15.15s", _(sys[next].name));
-               else
-                       fputs(name, stdout);
+       if (types[0].typestr == NULL) {
+               /*
+                * Prints in 4 columns in format <hex> <name>
+                */
+               unsigned int last[4], done = 0, next = 0, size;
 
-               next = last[i++] + done;
-               if (i > 3 || next >= last[i]) {
-                       i = 0;
-                       next = ++done;
-               }
-       } while (done < last[0]);
+               for (i = 0; types[i].name; i++);
+               size = i;
+
+               for (i = 3; i >= 0; i--)
+                       last[3 - i] = done += (size + i - done) / (i + 1);
+               i = done = 0;
+
+               do {
+                       #define NAME_WIDTH 15
+                       char name[NAME_WIDTH * MB_LEN_MAX];
+                       size_t width = NAME_WIDTH;
+                       struct fdisk_parttype *t = &types[next];
+                       size_t ret;
+
+                       printf("%c%2x  ", i ? ' ' : '\n', t->type);
+                       ret = mbsalign(_(t->name), name, sizeof(name),
+                                             &width, MBS_ALIGN_LEFT, 0);
+
+                       if (ret == (size_t)-1 || ret >= sizeof(name))
+                               printf("%-15.15s", _(t->name));
+                       else
+                               fputs(name, stdout);
+
+                       next = last[i++] + done;
+                       if (i > 3 || next >= last[i]) {
+                               i = 0;
+                               next = ++done;
+                       }
+               } while (done < last[0]);
+
+       } else {
+               /*
+                * Prints 1 column in format <idx> <name> <typestr>
+                */
+               struct fdisk_parttype *t;
+
+               for (i = 0, t = types; t->name; t++, i++)
+                       printf("%3d %-30s %s\n", i + 1, t->name, t->typestr);
+       }
        putchar('\n');
 }
 
@@ -567,7 +590,7 @@ read_hex(struct fdisk_context *cxt)
         {
            read_char(_("Hex code (type L to list codes): "));
            if (tolower(*line_ptr) == 'l')
-               list_types(cxt);
+               list_partition_types(cxt);
           else if (isxdigit (*line_ptr))
           {
              hex = 0;
@@ -1740,7 +1763,7 @@ static void command_prompt(struct fdisk_context *cxt)
                                unknown_command(c);
                        break;
                case 'l':
-                       list_types(cxt);
+                       list_partition_types(cxt);
                        break;
                case 'm':
                        print_menu(MAIN_MENU);
index 8221cd9a3868c3cf131d07af104778c24e17a0a2..631c47fc8d8d127e1c36fabcc8c3e8bd38c255e2 100644 (file)
@@ -104,7 +104,8 @@ typedef unsigned long long sector_t;
  */
 struct fdisk_parttype {
        unsigned int    type;           /* type as number or zero */
-       char    *name;                  /* description */
+       char            *name;          /* description */
+       char            *typestr;       /* type as string or NULL */
 };
 
 /*
@@ -200,7 +201,7 @@ extern void check(struct fdisk_context *cxt, int n, unsigned int h, unsigned int
 extern void change_units(struct fdisk_context *cxt);
 extern void fatal(struct fdisk_context *cxt, enum failure why);
 extern int  get_partition(struct fdisk_context *cxt, int warn, int max);
-extern void list_types(struct fdisk_context *cxt);
+extern void list_partition_types(struct fdisk_context *cxt);
 extern int read_line (int *asked);
 extern char read_char(char *mesg);
 extern int read_hex(struct fdisk_context *cxt);
index c7c13b1b12ab96af1629eb8c7c34f1470f966406..adcdc2f23cce3566514e5901f4ab1972650711dd 100644 (file)
@@ -274,7 +274,7 @@ bsd_command_prompt (struct fdisk_context *cxt)
        xbsd_write_bootstrap (cxt);
        break;
       case 'l':
-       list_types (cxt);
+       list_partition_types (cxt);
        break;
       case 'n':
              xbsd_add_part (cxt, 0, 0);