aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-11-29 12:00:28 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-11-29 12:00:28 +0000
commit834410a5a56e7e7efac8b111bffd4c2362f5bc3f (patch)
treead67babd01fe71cafdad94738509d622208e349b
parentc61852a02bd2d61682235ba3185173f527313827 (diff)
downloadbusybox-w32-834410a5a56e7e7efac8b111bffd4c2362f5bc3f.tar.gz
busybox-w32-834410a5a56e7e7efac8b111bffd4c2362f5bc3f.tar.bz2
busybox-w32-834410a5a56e7e7efac8b111bffd4c2362f5bc3f.zip
getopt32-ification of fdisk
-rw-r--r--util-linux/fdisk.c350
1 files changed, 165 insertions, 185 deletions
diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c
index 07dac8bad..c226d6a00 100644
--- a/util-linux/fdisk.c
+++ b/util-linux/fdisk.c
@@ -11,6 +11,12 @@
11#include "busybox.h" 11#include "busybox.h"
12#define _(x) x 12#define _(x) x
13 13
14/* Looks like someone forgot to add this to config system */
15#ifndef ENABLE_FEATURE_FDISK_BLKSIZE
16# define ENABLE_FEATURE_FDISK_BLKSIZE 0
17# define USE_FEATURE_FDISK_BLKSIZE(a)
18#endif
19
14#define DEFAULT_SECTOR_SIZE 512 20#define DEFAULT_SECTOR_SIZE 512
15#define MAX_SECTOR_SIZE 2048 21#define MAX_SECTOR_SIZE 2048
16#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */ 22#define SECTOR_SIZE 512 /* still used in osf/sgi/sun code */
@@ -48,9 +54,9 @@ struct systypes {
48 const char *name; 54 const char *name;
49}; 55};
50 56
51static uint sector_size = DEFAULT_SECTOR_SIZE; 57static unsigned sector_size = DEFAULT_SECTOR_SIZE;
52static uint user_set_sector_size; 58static unsigned user_set_sector_size;
53static uint sector_offset = 1; 59static unsigned sector_offset = 1;
54 60
55/* 61/*
56 * Raw disk label. For DOS-type partition tables the MBR, 62 * Raw disk label. For DOS-type partition tables the MBR,
@@ -62,11 +68,11 @@ static char MBRbuffer[MAX_SECTOR_SIZE];
62# define MBRbuffer bb_common_bufsiz1 68# define MBRbuffer bb_common_bufsiz1
63#endif 69#endif
64 70
65#ifdef CONFIG_FEATURE_OSF_LABEL 71#if ENABLE_FEATURE_OSF_LABEL
66static int possibly_osf_label; 72static int possibly_osf_label;
67#endif 73#endif
68 74
69static uint heads, sectors, cylinders; 75static unsigned heads, sectors, cylinders;
70static void update_units(void); 76static void update_units(void);
71 77
72 78
@@ -132,7 +138,7 @@ enum label_type {
132}; 138};
133#define LABEL_IS_DOS (label_dos == current_label_type) 139#define LABEL_IS_DOS (label_dos == current_label_type)
134 140
135#ifdef CONFIG_FEATURE_SUN_LABEL 141#if ENABLE_FEATURE_SUN_LABEL
136#define LABEL_IS_SUN (label_sun == current_label_type) 142#define LABEL_IS_SUN (label_sun == current_label_type)
137#define STATIC_SUN static 143#define STATIC_SUN static
138#else 144#else
@@ -140,7 +146,7 @@ enum label_type {
140#define STATIC_SUN extern 146#define STATIC_SUN extern
141#endif 147#endif
142 148
143#ifdef CONFIG_FEATURE_SGI_LABEL 149#if ENABLE_FEATURE_SGI_LABEL
144#define LABEL_IS_SGI (label_sgi == current_label_type) 150#define LABEL_IS_SGI (label_sgi == current_label_type)
145#define STATIC_SGI static 151#define STATIC_SGI static
146#else 152#else
@@ -148,7 +154,7 @@ enum label_type {
148#define STATIC_SGI extern 154#define STATIC_SGI extern
149#endif 155#endif
150 156
151#ifdef CONFIG_FEATURE_AIX_LABEL 157#if ENABLE_FEATURE_AIX_LABEL
152#define LABEL_IS_AIX (label_aix == current_label_type) 158#define LABEL_IS_AIX (label_aix == current_label_type)
153#define STATIC_AIX static 159#define STATIC_AIX static
154#else 160#else
@@ -156,7 +162,7 @@ enum label_type {
156#define STATIC_AIX extern 162#define STATIC_AIX extern
157#endif 163#endif
158 164
159#ifdef CONFIG_FEATURE_OSF_LABEL 165#if ENABLE_FEATURE_OSF_LABEL
160#define LABEL_IS_OSF (label_osf == current_label_type) 166#define LABEL_IS_OSF (label_osf == current_label_type)
161#define STATIC_OSF static 167#define STATIC_OSF static
162#else 168#else
@@ -171,15 +177,15 @@ static enum label_type current_label_type;
171static const char *disk_device; 177static const char *disk_device;
172static int fd; /* the disk */ 178static int fd; /* the disk */
173static int partitions = 4; /* maximum partition + 1 */ 179static int partitions = 4; /* maximum partition + 1 */
174static uint display_in_cyl_units = 1; 180static int display_in_cyl_units = 1;
175static uint units_per_sector = 1; 181static unsigned units_per_sector = 1;
176#ifdef CONFIG_FEATURE_FDISK_WRITABLE 182#if ENABLE_FEATURE_FDISK_WRITABLE
177static void change_units(void); 183static void change_units(void);
178static void reread_partition_table(int leave); 184static void reread_partition_table(int leave);
179static void delete_partition(int i); 185static void delete_partition(int i);
180static int get_partition(int warn, int max); 186static int get_partition(int warn, int max);
181static void list_types(const struct systypes *sys); 187static void list_types(const struct systypes *sys);
182static uint read_int(uint low, uint dflt, uint high, uint base, char *mesg); 188static unsigned read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, char *mesg);
183#endif 189#endif
184static const char *partition_type(unsigned char type); 190static const char *partition_type(unsigned char type);
185static void fdisk_fatal(enum failure why) ATTRIBUTE_NORETURN; 191static void fdisk_fatal(enum failure why) ATTRIBUTE_NORETURN;
@@ -228,7 +234,7 @@ static int32_t get_nr_sects(const struct partition *p);
228static struct pte { 234static struct pte {
229 struct partition *part_table; /* points into sectorbuffer */ 235 struct partition *part_table; /* points into sectorbuffer */
230 struct partition *ext_pointer; /* points into sectorbuffer */ 236 struct partition *ext_pointer; /* points into sectorbuffer */
231#ifdef CONFIG_FEATURE_FDISK_WRITABLE 237#if ENABLE_FEATURE_FDISK_WRITABLE
232 char changed; /* boolean */ 238 char changed; /* boolean */
233#endif 239#endif
234 off_t offset; /* disk sector number */ 240 off_t offset; /* disk sector number */
@@ -236,7 +242,7 @@ static struct pte {
236} ptes[MAXIMUM_PARTS]; 242} ptes[MAXIMUM_PARTS];
237 243
238 244
239#ifdef CONFIG_FEATURE_FDISK_WRITABLE 245#if ENABLE_FEATURE_FDISK_WRITABLE
240static void 246static void
241set_all_unchanged(void) 247set_all_unchanged(void)
242{ 248{
@@ -246,14 +252,14 @@ set_all_unchanged(void)
246 ptes[i].changed = 0; 252 ptes[i].changed = 0;
247} 253}
248 254
249static void 255extern inline void
250set_changed(int i) 256set_changed(int i)
251{ 257{
252 ptes[i].changed = 1; 258 ptes[i].changed = 1;
253} 259}
254#endif /* CONFIG_FEATURE_FDISK_WRITABLE */ 260#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
255 261
256static inline struct partition * 262extern inline struct partition *
257get_part_table(int i) 263get_part_table(int i)
258{ 264{
259 return ptes[i].part_table; 265 return ptes[i].part_table;
@@ -271,11 +277,17 @@ str_units(int n)
271static int 277static int
272valid_part_table_flag(const char *mbuffer) 278valid_part_table_flag(const char *mbuffer)
273{ 279{
274 const unsigned char *b = (const unsigned char *)mbuffer; 280 return (mbuffer[510] == 0x55 && (uint8_t)mbuffer[511] == 0xaa);
275 return (b[510] == 0x55 && b[511] == 0xaa); 281}
282
283#if ENABLE_FEATURE_FDISK_WRITABLE
284extern inline void
285write_part_table_flag(char *b)
286{
287 b[510] = 0x55;
288 b[511] = 0xaa;
276} 289}
277 290
278#ifdef CONFIG_FEATURE_FDISK_WRITABLE
279static char line_buffer[LINE_LENGTH]; 291static char line_buffer[LINE_LENGTH];
280static char *line_ptr; 292static char *line_ptr;
281 293
@@ -396,7 +408,7 @@ __swap32(uint32_t x)
396#endif 408#endif
397 409
398STATIC_SGI const struct systypes sgi_sys_types[]; 410STATIC_SGI const struct systypes sgi_sys_types[];
399STATIC_SGI unsigned int sgi_get_num_sectors(int i); 411STATIC_SGI unsigned sgi_get_num_sectors(int i);
400STATIC_SGI int sgi_get_sysid(int i); 412STATIC_SGI int sgi_get_sysid(int i);
401STATIC_SGI void sgi_delete_partition(int i); 413STATIC_SGI void sgi_delete_partition(int i);
402STATIC_SGI void sgi_change_sysid(int i, int sys); 414STATIC_SGI void sgi_change_sysid(int i, int sys);
@@ -483,7 +495,7 @@ static const struct systypes i386_sys_types[] = {
483 { "\xfd" "Linux raid autodetect" },/* New (2.2.x) raid partition with 495 { "\xfd" "Linux raid autodetect" },/* New (2.2.x) raid partition with
484 autodetect using persistent 496 autodetect using persistent
485 superblock */ 497 superblock */
486#ifdef CONFIG_WEIRD_PARTITION_TYPES 498#if 0 /* ENABLE_WEIRD_PARTITION_TYPES */
487 { "\x02" "XENIX root" }, 499 { "\x02" "XENIX root" },
488 { "\x03" "XENIX usr" }, 500 { "\x03" "XENIX usr" },
489 { "\x08" "AIX" }, /* AIX boot (AIX -- PS/2 port) or SplitDrive */ 501 { "\x08" "AIX" }, /* AIX boot (AIX -- PS/2 port) or SplitDrive */
@@ -536,44 +548,28 @@ static const struct systypes i386_sys_types[] = {
536}; 548};
537 549
538 550
539/* A valid partition table sector ends in 0x55 0xaa */ 551#if ENABLE_FEATURE_FDISK_WRITABLE
540static unsigned int
541part_table_flag(const char *b)
542{
543 return ((uint) b[510]) + (((uint) b[511]) << 8);
544}
545
546
547#ifdef CONFIG_FEATURE_FDISK_WRITABLE
548static void
549write_part_table_flag(char *b)
550{
551 b[510] = 0x55;
552 b[511] = 0xaa;
553}
554
555/* start_sect and nr_sects are stored little endian on all machines */ 552/* start_sect and nr_sects are stored little endian on all machines */
556/* moreover, they are not aligned correctly */ 553/* moreover, they are not aligned correctly */
557static void 554static void
558store4_little_endian(unsigned char *cp, unsigned int val) 555store4_little_endian(unsigned char *cp, unsigned val)
559{ 556{
560 cp[0] = (val & 0xff); 557 cp[0] = val;
561 cp[1] = ((val >> 8) & 0xff); 558 cp[1] = val >> 8;
562 cp[2] = ((val >> 16) & 0xff); 559 cp[2] = val >> 16;
563 cp[3] = ((val >> 24) & 0xff); 560 cp[3] = val >> 24;
564} 561}
565#endif /* CONFIG_FEATURE_FDISK_WRITABLE */ 562#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
566 563
567static unsigned int 564static unsigned
568read4_little_endian(const unsigned char *cp) 565read4_little_endian(const unsigned char *cp)
569{ 566{
570 return (uint)(cp[0]) + ((uint)(cp[1]) << 8) 567 return cp[0] + (cp[1] << 8) + (cp[2] << 16) + (cp[3] << 24);
571 + ((uint)(cp[2]) << 16) + ((uint)(cp[3]) << 24);
572} 568}
573 569
574#ifdef CONFIG_FEATURE_FDISK_WRITABLE 570#if ENABLE_FEATURE_FDISK_WRITABLE
575static void 571static void
576set_start_sect(struct partition *p, unsigned int start_sect) 572set_start_sect(struct partition *p, unsigned start_sect)
577{ 573{
578 store4_little_endian(p->start4, start_sect); 574 store4_little_endian(p->start4, start_sect);
579} 575}
@@ -585,7 +581,7 @@ get_start_sect(const struct partition *p)
585 return read4_little_endian(p->start4); 581 return read4_little_endian(p->start4);
586} 582}
587 583
588#ifdef CONFIG_FEATURE_FDISK_WRITABLE 584#if ENABLE_FEATURE_FDISK_WRITABLE
589static void 585static void
590set_nr_sects(struct partition *p, int32_t nr_sects) 586set_nr_sects(struct partition *p, int32_t nr_sects)
591{ 587{
@@ -606,16 +602,16 @@ static int type_open = O_RDWR;
606static int ext_index; /* the prime extended partition */ 602static int ext_index; /* the prime extended partition */
607static int listing; /* no aborts for fdisk -l */ 603static int listing; /* no aborts for fdisk -l */
608static int dos_compatible_flag = ~0; 604static int dos_compatible_flag = ~0;
609#ifdef CONFIG_FEATURE_FDISK_WRITABLE 605#if ENABLE_FEATURE_FDISK_WRITABLE
610static int dos_changed; 606static int dos_changed;
611static int nowarn; /* no warnings for fdisk -l/-s */ 607static int nowarn; /* no warnings for fdisk -l/-s */
612#endif 608#endif
613 609
614 610
615 611
616static uint user_cylinders, user_heads, user_sectors; 612static unsigned user_cylinders, user_heads, user_sectors;
617static uint pt_heads, pt_sectors; 613static unsigned pt_heads, pt_sectors;
618static uint kern_heads, kern_sectors; 614static unsigned kern_heads, kern_sectors;
619 615
620static off_t extended_offset; /* offset of link pointers */ 616static off_t extended_offset; /* offset of link pointers */
621 617
@@ -664,7 +660,7 @@ seek_sector(off_t secno)
664 fdisk_fatal(unable_to_seek); 660 fdisk_fatal(unable_to_seek);
665} 661}
666 662
667#ifdef CONFIG_FEATURE_FDISK_WRITABLE 663#if ENABLE_FEATURE_FDISK_WRITABLE
668static void 664static void
669write_sector(off_t secno, char *buf) 665write_sector(off_t secno, char *buf)
670{ 666{
@@ -683,19 +679,19 @@ read_pte(struct pte *pe, off_t offset)
683 seek_sector(offset); 679 seek_sector(offset);
684 if (read(fd, pe->sectorbuffer, sector_size) != sector_size) 680 if (read(fd, pe->sectorbuffer, sector_size) != sector_size)
685 fdisk_fatal(unable_to_read); 681 fdisk_fatal(unable_to_read);
686#ifdef CONFIG_FEATURE_FDISK_WRITABLE 682#if ENABLE_FEATURE_FDISK_WRITABLE
687 pe->changed = 0; 683 pe->changed = 0;
688#endif 684#endif
689 pe->part_table = pe->ext_pointer = NULL; 685 pe->part_table = pe->ext_pointer = NULL;
690} 686}
691 687
692static unsigned int 688static unsigned
693get_partition_start(const struct pte *pe) 689get_partition_start(const struct pte *pe)
694{ 690{
695 return pe->offset + get_start_sect(pe->part_table); 691 return pe->offset + get_start_sect(pe->part_table);
696} 692}
697 693
698#ifdef CONFIG_FEATURE_FDISK_WRITABLE 694#if ENABLE_FEATURE_FDISK_WRITABLE
699/* 695/*
700 * Avoid warning about DOS partitions when no DOS partition was changed. 696 * Avoid warning about DOS partitions when no DOS partition was changed.
701 * Here a heuristic "is probably dos partition". 697 * Here a heuristic "is probably dos partition".
@@ -732,7 +728,7 @@ menu(void)
732 puts(_("\tu\tchange display/entry units")); 728 puts(_("\tu\tchange display/entry units"));
733 puts(_("\tv\tverify the partition table")); 729 puts(_("\tv\tverify the partition table"));
734 puts(_("\tw\twrite table to disk and exit")); 730 puts(_("\tw\twrite table to disk and exit"));
735#ifdef CONFIG_FEATURE_FDISK_ADVANCED 731#if ENABLE_FEATURE_FDISK_ADVANCED
736 puts(_("\tx\textra functionality (experts only)")); 732 puts(_("\tx\textra functionality (experts only)"));
737#endif 733#endif
738 } else 734 } else
@@ -778,7 +774,7 @@ menu(void)
778 puts(_("\tu\tchange display/entry units")); 774 puts(_("\tu\tchange display/entry units"));
779 puts(_("\tv\tverify the partition table")); 775 puts(_("\tv\tverify the partition table"));
780 puts(_("\tw\twrite table to disk and exit")); 776 puts(_("\tw\twrite table to disk and exit"));
781#ifdef CONFIG_FEATURE_FDISK_ADVANCED 777#if ENABLE_FEATURE_FDISK_ADVANCED
782 puts(_("\tx\textra functionality (experts only)")); 778 puts(_("\tx\textra functionality (experts only)"));
783#endif 779#endif
784 } 780 }
@@ -786,7 +782,7 @@ menu(void)
786#endif /* CONFIG_FEATURE_FDISK_WRITABLE */ 782#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
787 783
788 784
789#ifdef CONFIG_FEATURE_FDISK_ADVANCED 785#if ENABLE_FEATURE_FDISK_ADVANCED
790static void 786static void
791xmenu(void) 787xmenu(void)
792{ 788{
@@ -846,7 +842,7 @@ xmenu(void)
846 puts(_("\td\tprint the raw data in the partition table")); 842 puts(_("\td\tprint the raw data in the partition table"));
847 puts(_("\te\tlist extended partitions")); /* !sun */ 843 puts(_("\te\tlist extended partitions")); /* !sun */
848 puts(_("\tf\tfix partition order")); /* !sun, !aix, !sgi */ 844 puts(_("\tf\tfix partition order")); /* !sun, !aix, !sgi */
849#ifdef CONFIG_FEATURE_SGI_LABEL 845#if ENABLE_FEATURE_SGI_LABEL
850 puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */ 846 puts(_("\tg\tcreate an IRIX (SGI) partition table"));/* sgi */
851#endif 847#endif
852 puts(_("\th\tchange number of heads")); 848 puts(_("\th\tchange number of heads"));
@@ -861,7 +857,7 @@ xmenu(void)
861} 857}
862#endif /* ADVANCED mode */ 858#endif /* ADVANCED mode */
863 859
864#ifdef CONFIG_FEATURE_FDISK_WRITABLE 860#if ENABLE_FEATURE_FDISK_WRITABLE
865static const struct systypes * 861static const struct systypes *
866get_sys_types(void) 862get_sys_types(void)
867{ 863{
@@ -887,7 +883,7 @@ static const char *partition_type(unsigned char type)
887} 883}
888 884
889 885
890#ifdef CONFIG_FEATURE_FDISK_WRITABLE 886#if ENABLE_FEATURE_FDISK_WRITABLE
891static int 887static int
892get_sysid(int i) 888get_sysid(int i)
893{ 889{
@@ -898,7 +894,7 @@ get_sysid(int i)
898 894
899void list_types(const struct systypes *sys) 895void list_types(const struct systypes *sys)
900{ 896{
901 uint last[4], done = 0, next = 0, size; 897 unsigned last[4], done = 0, next = 0, size;
902 int i; 898 int i;
903 899
904 for (i = 0; sys[i].name; i++); 900 for (i = 0; sys[i].name; i++);
@@ -938,7 +934,7 @@ clear_partition(struct partition *p)
938 memset(p, 0, sizeof(struct partition)); 934 memset(p, 0, sizeof(struct partition));
939} 935}
940 936
941#ifdef CONFIG_FEATURE_FDISK_WRITABLE 937#if ENABLE_FEATURE_FDISK_WRITABLE
942static void 938static void
943set_partition(int i, int doext, off_t start, off_t stop, int sysid) 939set_partition(int i, int doext, off_t start, off_t stop, int sysid)
944{ 940{
@@ -996,7 +992,7 @@ warn_geometry(void)
996 return 0; 992 return 0;
997 993
998 printf("%s%s.\n" 994 printf("%s%s.\n"
999#ifdef CONFIG_FEATURE_FDISK_WRITABLE 995#if ENABLE_FEATURE_FDISK_WRITABLE
1000 "You can do this from the extra functions menu.\n" 996 "You can do this from the extra functions menu.\n"
1001#endif 997#endif
1002 , prev ? _(" and ") : " ", m); 998 , prev ? _(" and ") : " ", m);
@@ -1014,7 +1010,7 @@ static void update_units(void)
1014 units_per_sector = 1; /* in sectors */ 1010 units_per_sector = 1; /* in sectors */
1015} 1011}
1016 1012
1017#ifdef CONFIG_FEATURE_FDISK_WRITABLE 1013#if ENABLE_FEATURE_FDISK_WRITABLE
1018static void 1014static void
1019warn_cylinders(void) 1015warn_cylinders(void)
1020{ 1016{
@@ -1055,7 +1051,7 @@ read_extended(int ext)
1055 this program uses arrays of size MAXIMUM_PARTS. 1051 this program uses arrays of size MAXIMUM_PARTS.
1056 Do not try to 'improve' this test. */ 1052 Do not try to 'improve' this test. */
1057 struct pte *pre = &ptes[partitions-1]; 1053 struct pte *pre = &ptes[partitions-1];
1058#ifdef CONFIG_FEATURE_FDISK_WRITABLE 1054#if ENABLE_FEATURE_FDISK_WRITABLE
1059 printf(_("Warning: deleting partitions after %d\n"), 1055 printf(_("Warning: deleting partitions after %d\n"),
1060 partitions); 1056 partitions);
1061 pre->changed = 1; 1057 pre->changed = 1;
@@ -1106,7 +1102,7 @@ read_extended(int ext)
1106 partitions++; 1102 partitions++;
1107 } 1103 }
1108 1104
1109#ifdef CONFIG_FEATURE_FDISK_WRITABLE 1105#if ENABLE_FEATURE_FDISK_WRITABLE
1110 /* remove empty links */ 1106 /* remove empty links */
1111 remove: 1107 remove:
1112 for (i = 4; i < partitions; i++) { 1108 for (i = 4; i < partitions; i++) {
@@ -1122,7 +1118,7 @@ read_extended(int ext)
1122#endif 1118#endif
1123} 1119}
1124 1120
1125#ifdef CONFIG_FEATURE_FDISK_WRITABLE 1121#if ENABLE_FEATURE_FDISK_WRITABLE
1126static void 1122static void
1127create_doslabel(void) 1123create_doslabel(void)
1128{ 1124{
@@ -1135,7 +1131,7 @@ create_doslabel(void)
1135 1131
1136 current_label_type = label_dos; 1132 current_label_type = label_dos;
1137 1133
1138#ifdef CONFIG_FEATURE_OSF_LABEL 1134#if ENABLE_FEATURE_OSF_LABEL
1139 possibly_osf_label = 0; 1135 possibly_osf_label = 0;
1140#endif 1136#endif
1141 partitions = 4; 1137 partitions = 4;
@@ -1216,7 +1212,7 @@ get_geometry(void)
1216 1212
1217 get_sectorsize(); 1213 get_sectorsize();
1218 sec_fac = sector_size / 512; 1214 sec_fac = sector_size / 512;
1219#ifdef CONFIG_FEATURE_SUN_LABEL 1215#if ENABLE_FEATURE_SUN_LABEL
1220 guess_device_type(); 1216 guess_device_type();
1221#endif 1217#endif
1222 heads = cylinders = sectors = 0; 1218 heads = cylinders = sectors = 0;
@@ -1273,19 +1269,19 @@ get_boot(enum action what)
1273 pe->ext_pointer = NULL; 1269 pe->ext_pointer = NULL;
1274 pe->offset = 0; 1270 pe->offset = 0;
1275 pe->sectorbuffer = MBRbuffer; 1271 pe->sectorbuffer = MBRbuffer;
1276#ifdef CONFIG_FEATURE_FDISK_WRITABLE 1272#if ENABLE_FEATURE_FDISK_WRITABLE
1277 pe->changed = (what == create_empty_dos); 1273 pe->changed = (what == create_empty_dos);
1278#endif 1274#endif
1279 } 1275 }
1280 1276
1281#ifdef CONFIG_FEATURE_SUN_LABEL 1277#if ENABLE_FEATURE_SUN_LABEL
1282 if (what == create_empty_sun && check_sun_label()) 1278 if (what == create_empty_sun && check_sun_label())
1283 return 0; 1279 return 0;
1284#endif 1280#endif
1285 1281
1286 memset(MBRbuffer, 0, 512); 1282 memset(MBRbuffer, 0, 512);
1287 1283
1288#ifdef CONFIG_FEATURE_FDISK_WRITABLE 1284#if ENABLE_FEATURE_FDISK_WRITABLE
1289 if (what == create_empty_dos) 1285 if (what == create_empty_dos)
1290 goto got_dos_table; /* skip reading disk */ 1286 goto got_dos_table; /* skip reading disk */
1291 1287
@@ -1315,22 +1311,22 @@ get_boot(enum action what)
1315 1311
1316 update_units(); 1312 update_units();
1317 1313
1318#ifdef CONFIG_FEATURE_SUN_LABEL 1314#if ENABLE_FEATURE_SUN_LABEL
1319 if (check_sun_label()) 1315 if (check_sun_label())
1320 return 0; 1316 return 0;
1321#endif 1317#endif
1322 1318
1323#ifdef CONFIG_FEATURE_SGI_LABEL 1319#if ENABLE_FEATURE_SGI_LABEL
1324 if (check_sgi_label()) 1320 if (check_sgi_label())
1325 return 0; 1321 return 0;
1326#endif 1322#endif
1327 1323
1328#ifdef CONFIG_FEATURE_AIX_LABEL 1324#if ENABLE_FEATURE_AIX_LABEL
1329 if (check_aix_label()) 1325 if (check_aix_label())
1330 return 0; 1326 return 0;
1331#endif 1327#endif
1332 1328
1333#ifdef CONFIG_FEATURE_OSF_LABEL 1329#if ENABLE_FEATURE_OSF_LABEL
1334 if (check_osf_label()) { 1330 if (check_osf_label()) {
1335 possibly_osf_label = 1; 1331 possibly_osf_label = 1;
1336 if (!valid_part_table_flag(MBRbuffer)) { 1332 if (!valid_part_table_flag(MBRbuffer)) {
@@ -1342,7 +1338,7 @@ get_boot(enum action what)
1342 } 1338 }
1343#endif 1339#endif
1344 1340
1345#ifdef CONFIG_FEATURE_FDISK_WRITABLE 1341#if ENABLE_FEATURE_FDISK_WRITABLE
1346 got_dos_table: 1342 got_dos_table:
1347#endif 1343#endif
1348 1344
@@ -1356,7 +1352,7 @@ get_boot(enum action what)
1356 "partition table, nor Sun, SGI or OSF " 1352 "partition table, nor Sun, SGI or OSF "
1357 "disklabel\n")); 1353 "disklabel\n"));
1358#ifdef __sparc__ 1354#ifdef __sparc__
1359#ifdef CONFIG_FEATURE_SUN_LABEL 1355#if ENABLE_FEATURE_SUN_LABEL
1360 create_sunlabel(); 1356 create_sunlabel();
1361#endif 1357#endif
1362#else 1358#else
@@ -1366,7 +1362,7 @@ get_boot(enum action what)
1366 case try_only: 1362 case try_only:
1367 return -1; 1363 return -1;
1368 case create_empty_dos: 1364 case create_empty_dos:
1369#ifdef CONFIG_FEATURE_SUN_LABEL 1365#if ENABLE_FEATURE_SUN_LABEL
1370 case create_empty_sun: 1366 case create_empty_sun:
1371#endif 1367#endif
1372 break; 1368 break;
@@ -1376,7 +1372,7 @@ get_boot(enum action what)
1376#endif /* CONFIG_FEATURE_FDISK_WRITABLE */ 1372#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
1377 } 1373 }
1378 1374
1379#ifdef CONFIG_FEATURE_FDISK_WRITABLE 1375#if ENABLE_FEATURE_FDISK_WRITABLE
1380 warn_cylinders(); 1376 warn_cylinders();
1381#endif 1377#endif
1382 warn_geometry(); 1378 warn_geometry();
@@ -1397,10 +1393,12 @@ get_boot(enum action what)
1397 struct pte *pe = &ptes[i]; 1393 struct pte *pe = &ptes[i];
1398 1394
1399 if (!valid_part_table_flag(pe->sectorbuffer)) { 1395 if (!valid_part_table_flag(pe->sectorbuffer)) {
1400 printf(_("Warning: invalid flag 0x%04x of partition " 1396 printf(_("Warning: invalid flag 0x%02x,0x%02x of partition "
1401 "table %d will be corrected by w(rite)\n"), 1397 "table %d will be corrected by w(rite)\n"),
1402 part_table_flag(pe->sectorbuffer), i + 1); 1398 pe->sectorbuffer[510],
1403#ifdef CONFIG_FEATURE_FDISK_WRITABLE 1399 pe->sectorbuffer[511],
1400 i + 1);
1401#if ENABLE_FEATURE_FDISK_WRITABLE
1404 pe->changed = 1; 1402 pe->changed = 1;
1405#endif 1403#endif
1406 } 1404 }
@@ -1409,7 +1407,7 @@ get_boot(enum action what)
1409 return 0; 1407 return 0;
1410} 1408}
1411 1409
1412#ifdef CONFIG_FEATURE_FDISK_WRITABLE 1410#if ENABLE_FEATURE_FDISK_WRITABLE
1413/* 1411/*
1414 * Print the message MESG, then read an integer between LOW and HIGH (inclusive). 1412 * Print the message MESG, then read an integer between LOW and HIGH (inclusive).
1415 * If the user hits Enter, DFLT is returned. 1413 * If the user hits Enter, DFLT is returned.
@@ -1417,10 +1415,10 @@ get_boot(enum action what)
1417 * 1415 *
1418 * There is no default if DFLT is not between LOW and HIGH. 1416 * There is no default if DFLT is not between LOW and HIGH.
1419 */ 1417 */
1420static uint 1418static unsigned
1421read_int(uint low, uint dflt, uint high, uint base, char *mesg) 1419read_int(unsigned low, unsigned dflt, unsigned high, unsigned base, char *mesg)
1422{ 1420{
1423 uint i; 1421 unsigned i;
1424 int default_ok = 1; 1422 int default_ok = 1;
1425 const char *fmt = "%s (%u-%u, default %u): "; 1423 const char *fmt = "%s (%u-%u, default %u): ";
1426 1424
@@ -1775,7 +1773,7 @@ change_sysid(void)
1775 * Lubkin Oct. 1991). */ 1773 * Lubkin Oct. 1991). */
1776 1774
1777static void 1775static void
1778long2chs(ulong ls, uint *c, uint *h, uint *s) 1776long2chs(ulong ls, unsigned *c, unsigned *h, unsigned *s)
1779{ 1777{
1780 int spc = heads * sectors; 1778 int spc = heads * sectors;
1781 1779
@@ -1788,10 +1786,10 @@ long2chs(ulong ls, uint *c, uint *h, uint *s)
1788static void 1786static void
1789check_consistency(const struct partition *p, int partition) 1787check_consistency(const struct partition *p, int partition)
1790{ 1788{
1791 uint pbc, pbh, pbs; /* physical beginning c, h, s */ 1789 unsigned pbc, pbh, pbs; /* physical beginning c, h, s */
1792 uint pec, peh, pes; /* physical ending c, h, s */ 1790 unsigned pec, peh, pes; /* physical ending c, h, s */
1793 uint lbc, lbh, lbs; /* logical beginning c, h, s */ 1791 unsigned lbc, lbh, lbs; /* logical beginning c, h, s */
1794 uint lec, leh, les; /* logical ending c, h, s */ 1792 unsigned lec, leh, les; /* logical ending c, h, s */
1795 1793
1796 if (!heads || !sectors || (partition >= 4)) 1794 if (!heads || !sectors || (partition >= 4))
1797 return; /* do not check extended partitions */ 1795 return; /* do not check extended partitions */
@@ -1892,7 +1890,7 @@ wrong_p_order(int *prev)
1892 return 0; 1890 return 0;
1893} 1891}
1894 1892
1895#ifdef CONFIG_FEATURE_FDISK_ADVANCED 1893#if ENABLE_FEATURE_FDISK_ADVANCED
1896/* 1894/*
1897 * Fix the chain of logicals. 1895 * Fix the chain of logicals.
1898 * extended_offset is unchanged, the set of sectors used is unchanged 1896 * extended_offset is unchanged, the set of sectors used is unchanged
@@ -2037,7 +2035,7 @@ list_table(int xtra)
2037 const struct pte *pe = &ptes[i]; 2035 const struct pte *pe = &ptes[i];
2038 off_t psects; 2036 off_t psects;
2039 off_t pblocks; 2037 off_t pblocks;
2040 unsigned int podd; 2038 unsigned podd;
2041 2039
2042 p = pe->part_table; 2040 p = pe->part_table;
2043 if (!p || is_cleared_partition(p)) 2041 if (!p || is_cleared_partition(p))
@@ -2077,7 +2075,7 @@ list_table(int xtra)
2077 } 2075 }
2078} 2076}
2079 2077
2080#ifdef CONFIG_FEATURE_FDISK_ADVANCED 2078#if ENABLE_FEATURE_FDISK_ADVANCED
2081static void 2079static void
2082x_list_table(int extend) 2080x_list_table(int extend)
2083{ 2081{
@@ -2106,7 +2104,7 @@ x_list_table(int extend)
2106} 2104}
2107#endif 2105#endif
2108 2106
2109#ifdef CONFIG_FEATURE_FDISK_WRITABLE 2107#if ENABLE_FEATURE_FDISK_WRITABLE
2110static void 2108static void
2111fill_bounds(off_t *first, off_t *last) 2109fill_bounds(off_t *first, off_t *last)
2112{ 2110{
@@ -2127,7 +2125,7 @@ fill_bounds(off_t *first, off_t *last)
2127} 2125}
2128 2126
2129static void 2127static void
2130check(int n, uint h, uint s, uint c, off_t start) 2128check(int n, unsigned h, unsigned s, unsigned c, off_t start)
2131{ 2129{
2132 off_t total, real_s, real_c; 2130 off_t total, real_s, real_c;
2133 2131
@@ -2154,7 +2152,7 @@ static void
2154verify(void) 2152verify(void)
2155{ 2153{
2156 int i, j; 2154 int i, j;
2157 uint total = 1; 2155 unsigned total = 1;
2158 off_t first[partitions], last[partitions]; 2156 off_t first[partitions], last[partitions];
2159 struct partition *p; 2157 struct partition *p;
2160 2158
@@ -2522,7 +2520,7 @@ reread_partition_table(int leave)
2522} 2520}
2523#endif /* CONFIG_FEATURE_FDISK_WRITABLE */ 2521#endif /* CONFIG_FEATURE_FDISK_WRITABLE */
2524 2522
2525#ifdef CONFIG_FEATURE_FDISK_ADVANCED 2523#if ENABLE_FEATURE_FDISK_ADVANCED
2526#define MAX_PER_LINE 16 2524#define MAX_PER_LINE 16
2527static void 2525static void
2528print_buffer(char *pbuffer) 2526print_buffer(char *pbuffer)
@@ -2625,7 +2623,7 @@ xselect(void)
2625 fix_partition_table_order(); 2623 fix_partition_table_order();
2626 break; 2624 break;
2627 case 'g': 2625 case 'g':
2628#ifdef CONFIG_FEATURE_SGI_LABEL 2626#if ENABLE_FEATURE_SGI_LABEL
2629 create_sgilabel(); 2627 create_sgilabel();
2630#endif 2628#endif
2631 break; 2629 break;
@@ -2736,7 +2734,7 @@ try(const char *device, int user_specified)
2736 if (LABEL_IS_AIX) { 2734 if (LABEL_IS_AIX) {
2737 return; 2735 return;
2738 } 2736 }
2739#ifdef CONFIG_FEATURE_OSF_LABEL 2737#if ENABLE_FEATURE_OSF_LABEL
2740 if (btrydev(device) < 0) 2738 if (btrydev(device) < 0)
2741#endif 2739#endif
2742 printf(_("Disk %s doesn't contain a valid " 2740 printf(_("Disk %s doesn't contain a valid "
@@ -2745,7 +2743,7 @@ try(const char *device, int user_specified)
2745 } else { 2743 } else {
2746 close(fd); 2744 close(fd);
2747 list_table(0); 2745 list_table(0);
2748#ifdef CONFIG_FEATURE_FDISK_WRITABLE 2746#if ENABLE_FEATURE_FDISK_WRITABLE
2749 if (!LABEL_IS_SUN && partitions > 4){ 2747 if (!LABEL_IS_SUN && partitions > 4){
2750 delete_partition(ext_index); 2748 delete_partition(ext_index);
2751 } 2749 }
@@ -2783,12 +2781,12 @@ tryprocpt(void)
2783 sprintf(devname, "/dev/%s", ptname); 2781 sprintf(devname, "/dev/%s", ptname);
2784 try(devname, 0); 2782 try(devname, 0);
2785 } 2783 }
2786#ifdef CONFIG_FEATURE_CLEAN_UP 2784#if ENABLE_FEATURE_CLEAN_UP
2787 fclose(procpt); 2785 fclose(procpt);
2788#endif 2786#endif
2789} 2787}
2790 2788
2791#ifdef CONFIG_FEATURE_FDISK_WRITABLE 2789#if ENABLE_FEATURE_FDISK_WRITABLE
2792static void 2790static void
2793unknown_command(int c) 2791unknown_command(int c)
2794{ 2792{
@@ -2798,82 +2796,67 @@ unknown_command(int c)
2798 2796
2799int fdisk_main(int argc, char **argv) 2797int fdisk_main(int argc, char **argv)
2800{ 2798{
2799 char *str_b, *str_C, *str_H, *str_S;
2800 unsigned opt;
2801 int c; 2801 int c;
2802#ifdef CONFIG_FEATURE_FDISK_WRITABLE
2803 int optl = 0;
2804#endif
2805#ifdef CONFIG_FEATURE_FDISK_BLKSIZE
2806 int opts = 0;
2807#endif
2808 /* 2802 /*
2809 * Calls:
2810 * fdisk -v 2803 * fdisk -v
2811 * fdisk -l [-b sectorsize] [-u] device ... 2804 * fdisk -l [-b sectorsize] [-u] device ...
2812 * fdisk -s [partition] ... 2805 * fdisk -s [partition] ...
2813 * fdisk [-b sectorsize] [-u] device 2806 * fdisk [-b sectorsize] [-u] device
2814 * 2807 *
2815 * Options -C, -H, -S set the geometry. 2808 * Options -C, -H, -S set the geometry.
2816 *
2817 */ 2809 */
2818 while ((c = getopt(argc, argv, "b:C:H:lS:u" 2810 enum {
2819#ifdef CONFIG_FEATURE_FDISK_BLKSIZE 2811 OPT_b = 1 << 0,
2820 "s" 2812 OPT_C = 1 << 1,
2821#endif 2813 OPT_H = 1 << 2,
2822 )) != -1) { 2814 OPT_l = 1 << 3,
2823 switch (c) { 2815 OPT_S = 1 << 4,
2824 case 'b': 2816 OPT_u = 1 << 5,
2825 /* Ugly: this sector size is really per device, 2817 OPT_s = (1 << 6) * ENABLE_FEATURE_FDISK_BLKSIZE,
2826 so cannot be combined with multiple disks, 2818 };
2827 and te same goes for the C/H/S options. 2819 opt = getopt32(argc, argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
2828 */ 2820 &str_b, &str_C, &str_H, &str_S);
2829 sector_size = xatoi_u(optarg); 2821 argc -= optind;
2830 if (sector_size != 512 && sector_size != 1024 && 2822 argv += optind;
2831 sector_size != 2048) 2823 if (opt & OPT_b) { // -b
2832 bb_show_usage(); 2824 /* Ugly: this sector size is really per device,
2833 sector_offset = 2; 2825 so cannot be combined with multiple disks,
2834 user_set_sector_size = 1; 2826 and the same goes for the C/H/S options.
2835 break; 2827 */
2836 case 'C': 2828 sector_size = xatoi_u(str_b);
2837 user_cylinders = xatoi_u(optarg); 2829 if (sector_size != 512 && sector_size != 1024 &&
2838 break; 2830 sector_size != 2048)
2839 case 'H':
2840 user_heads = xatoi_u(optarg);
2841 if (user_heads <= 0 || user_heads >= 256)
2842 user_heads = 0;
2843 break;
2844 case 'S':
2845 user_sectors = xatoi_u(optarg);
2846 if (user_sectors <= 0 || user_sectors >= 64)
2847 user_sectors = 0;
2848 break;
2849 case 'l':
2850#ifdef CONFIG_FEATURE_FDISK_WRITABLE
2851 optl = 1;
2852#endif
2853 break;
2854#ifdef CONFIG_FEATURE_FDISK_BLKSIZE
2855 case 's':
2856 opts = 1;
2857 break;
2858#endif
2859 case 'u':
2860 display_in_cyl_units = 0;
2861 break;
2862 default:
2863 bb_show_usage(); 2831 bb_show_usage();
2864 } 2832 sector_offset = 2;
2833 user_set_sector_size = 1;
2834 }
2835 if (opt & OPT_C) user_cylinders = xatoi_u(str_C); // -C
2836 if (opt & OPT_H) { // -H
2837 user_heads = xatoi_u(str_H);
2838 if (user_heads <= 0 || user_heads >= 256)
2839 user_heads = 0;
2840 }
2841 //if (opt & OPT_l) // -l
2842 if (opt & OPT_S) { // -S
2843 user_sectors = xatoi_u(str_S);
2844 if (user_sectors <= 0 || user_sectors >= 64)
2845 user_sectors = 0;
2865 } 2846 }
2847 if (opt & OPT_u) display_in_cyl_units = 0; // -u
2848 //if (opt & OPT_s) // -s
2866 2849
2867 if (user_set_sector_size && argc-optind != 1) 2850 if (user_set_sector_size && argc != 1)
2868 printf(_("Warning: the -b (set sector size) option should" 2851 printf(_("Warning: the -b (set sector size) option should"
2869 " be used with one specified device\n")); 2852 " be used with one specified device\n"));
2870 2853
2871#ifdef CONFIG_FEATURE_FDISK_WRITABLE 2854#if ENABLE_FEATURE_FDISK_WRITABLE
2872 if (optl) { 2855 if (opt & OPT_l) {
2873 nowarn = 1; 2856 nowarn = 1;
2874#endif 2857#endif
2875 type_open = O_RDONLY; 2858 type_open = O_RDONLY;
2876 if (argc > optind) { 2859 if (argc > 0) {
2877 int k; 2860 int k;
2878#if __GNUC__ 2861#if __GNUC__
2879 /* avoid gcc warning: 2862 /* avoid gcc warning:
@@ -2881,7 +2864,7 @@ int fdisk_main(int argc, char **argv)
2881 (void)&k; 2864 (void)&k;
2882#endif 2865#endif
2883 listing = 1; 2866 listing = 1;
2884 for (k = optind; k < argc; k++) 2867 for (k = 0; k < argc; k++)
2885 try(argv[k], 1); 2868 try(argv[k], 1);
2886 } else { 2869 } else {
2887 /* we no longer have default device names */ 2870 /* we no longer have default device names */
@@ -2889,30 +2872,30 @@ int fdisk_main(int argc, char **argv)
2889 tryprocpt(); 2872 tryprocpt();
2890 } 2873 }
2891 return 0; 2874 return 0;
2892#ifdef CONFIG_FEATURE_FDISK_WRITABLE 2875#if ENABLE_FEATURE_FDISK_WRITABLE
2893 } 2876 }
2894#endif 2877#endif
2895 2878
2896#ifdef CONFIG_FEATURE_FDISK_BLKSIZE 2879#if ENABLE_FEATURE_FDISK_BLKSIZE
2897 if (opts) { 2880 if (opt & OPT_s) {
2898 long size; 2881 long size;
2899 int j; 2882 int j;
2900 2883
2901 nowarn = 1; 2884 nowarn = 1;
2902 type_open = O_RDONLY; 2885 type_open = O_RDONLY;
2903 2886
2904 opts = argc - optind; 2887 if (argc <= 0)
2905 if (opts <= 0)
2906 bb_show_usage(); 2888 bb_show_usage();
2907 2889
2908 for (j = optind; j < argc; j++) { 2890 for (j = 0; j < argc; j++) {
2909 disk_device = argv[j]; 2891 disk_device = argv[j];
2910 if ((fd = open(disk_device, type_open)) < 0) 2892 fd = open(disk_device, type_open);
2893 if (fd < 0)
2911 fdisk_fatal(unable_to_open); 2894 fdisk_fatal(unable_to_open);
2912 if (ioctl(fd, BLKGETSIZE, &size)) 2895 if (ioctl(fd, BLKGETSIZE, &size))
2913 fdisk_fatal(ioctl_error); 2896 fdisk_fatal(ioctl_error);
2914 close(fd); 2897 close(fd);
2915 if (opts == 1) 2898 if (argc == 1)
2916 printf("%ld\n", size/2); 2899 printf("%ld\n", size/2);
2917 else 2900 else
2918 printf("%s: %ld\n", argv[j], size/2); 2901 printf("%s: %ld\n", argv[j], size/2);
@@ -2921,12 +2904,11 @@ int fdisk_main(int argc, char **argv)
2921 } 2904 }
2922#endif 2905#endif
2923 2906
2924#ifdef CONFIG_FEATURE_FDISK_WRITABLE 2907#if ENABLE_FEATURE_FDISK_WRITABLE
2925 if (argc-optind == 1) 2908 if (argc != 1)
2926 disk_device = argv[optind];
2927 else
2928 bb_show_usage(); 2909 bb_show_usage();
2929 2910
2911 disk_device = argv[0];
2930 get_boot(fdisk); 2912 get_boot(fdisk);
2931 2913
2932 if (LABEL_IS_OSF) { 2914 if (LABEL_IS_OSF) {
@@ -2964,13 +2946,11 @@ int fdisk_main(int argc, char **argv)
2964 printf(_("Boot file unchanged\n")); 2946 printf(_("Boot file unchanged\n"));
2965 else 2947 else
2966 sgi_set_bootfile(line_ptr); 2948 sgi_set_bootfile(line_ptr);
2967 } else 2949 }
2968#ifdef CONFIG_FEATURE_OSF_LABEL 2950#if ENABLE_FEATURE_OSF_LABEL
2951 else
2969 bsd_select(); 2952 bsd_select();
2970#endif 2953#endif
2971
2972/* BUG!? Think what will happen if !CONFIG_FEATURE_OSF_LABEL !!! */
2973
2974 break; 2954 break;
2975 case 'c': 2955 case 'c':
2976 if (LABEL_IS_DOS) 2956 if (LABEL_IS_DOS)
@@ -3025,7 +3005,7 @@ int fdisk_main(int argc, char **argv)
3025 puts(""); 3005 puts("");
3026 return 0; 3006 return 0;
3027 case 's': 3007 case 's':
3028#ifdef CONFIG_FEATURE_SUN_LABEL 3008#if ENABLE_FEATURE_SUN_LABEL
3029 create_sunlabel(); 3009 create_sunlabel();
3030#endif 3010#endif
3031 break; 3011 break;
@@ -3041,7 +3021,7 @@ int fdisk_main(int argc, char **argv)
3041 case 'w': 3021 case 'w':
3042 write_table(); /* does not return */ 3022 write_table(); /* does not return */
3043 break; 3023 break;
3044#ifdef CONFIG_FEATURE_FDISK_ADVANCED 3024#if ENABLE_FEATURE_FDISK_ADVANCED
3045 case 'x': 3025 case 'x':
3046 if (LABEL_IS_SGI) { 3026 if (LABEL_IS_SGI) {
3047 printf(_("\n\tSorry, no experts menu for SGI " 3027 printf(_("\n\tSorry, no experts menu for SGI "