aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--util-linux/fdisk.c227
-rw-r--r--util-linux/fdisk_aix.c2
-rw-r--r--util-linux/fdisk_osf.c28
-rw-r--r--util-linux/fdisk_sgi.c18
-rw-r--r--util-linux/fdisk_sun.c16
5 files changed, 136 insertions, 155 deletions
diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c
index dcfae96f5..57d4d653e 100644
--- a/util-linux/fdisk.c
+++ b/util-linux/fdisk.c
@@ -78,13 +78,13 @@ static const char ioctl_error[] ALIGN1 = "BLKGETSIZE ioctl failed on %s";
78static void fdisk_fatal(const char *why) ATTRIBUTE_NORETURN; 78static void fdisk_fatal(const char *why) ATTRIBUTE_NORETURN;
79 79
80enum label_type { 80enum label_type {
81 label_dos, label_sun, label_sgi, label_aix, label_osf 81 LABEL_DOS, LABEL_SUN, LABEL_SGI, LABEL_AIX, LABEL_OSF
82}; 82};
83 83
84#define LABEL_IS_DOS (label_dos == current_label_type) 84#define LABEL_IS_DOS (LABEL_DOS == current_label_type)
85 85
86#if ENABLE_FEATURE_SUN_LABEL 86#if ENABLE_FEATURE_SUN_LABEL
87#define LABEL_IS_SUN (label_sun == current_label_type) 87#define LABEL_IS_SUN (LABEL_SUN == current_label_type)
88#define STATIC_SUN static 88#define STATIC_SUN static
89#else 89#else
90#define LABEL_IS_SUN 0 90#define LABEL_IS_SUN 0
@@ -92,7 +92,7 @@ enum label_type {
92#endif 92#endif
93 93
94#if ENABLE_FEATURE_SGI_LABEL 94#if ENABLE_FEATURE_SGI_LABEL
95#define LABEL_IS_SGI (label_sgi == current_label_type) 95#define LABEL_IS_SGI (LABEL_SGI == current_label_type)
96#define STATIC_SGI static 96#define STATIC_SGI static
97#else 97#else
98#define LABEL_IS_SGI 0 98#define LABEL_IS_SGI 0
@@ -100,7 +100,7 @@ enum label_type {
100#endif 100#endif
101 101
102#if ENABLE_FEATURE_AIX_LABEL 102#if ENABLE_FEATURE_AIX_LABEL
103#define LABEL_IS_AIX (label_aix == current_label_type) 103#define LABEL_IS_AIX (LABEL_AIX == current_label_type)
104#define STATIC_AIX static 104#define STATIC_AIX static
105#else 105#else
106#define LABEL_IS_AIX 0 106#define LABEL_IS_AIX 0
@@ -108,14 +108,14 @@ enum label_type {
108#endif 108#endif
109 109
110#if ENABLE_FEATURE_OSF_LABEL 110#if ENABLE_FEATURE_OSF_LABEL
111#define LABEL_IS_OSF (label_osf == current_label_type) 111#define LABEL_IS_OSF (LABEL_OSF == current_label_type)
112#define STATIC_OSF static 112#define STATIC_OSF static
113#else 113#else
114#define LABEL_IS_OSF 0 114#define LABEL_IS_OSF 0
115#define STATIC_OSF extern 115#define STATIC_OSF extern
116#endif 116#endif
117 117
118enum action { fdisk, require, try_only, create_empty_dos, create_empty_sun }; 118enum action { OPEN_MAIN, TRY_ONLY, CREATE_EMPTY_DOS, CREATE_EMPTY_SUN };
119 119
120static void update_units(void); 120static void update_units(void);
121#if ENABLE_FEATURE_FDISK_WRITABLE 121#if ENABLE_FEATURE_FDISK_WRITABLE
@@ -263,14 +263,15 @@ static const char *const i386_sys_types[] = {
263 NULL 263 NULL
264}; 264};
265 265
266enum {
267 dev_fd = 3 /* the disk */
268};
266 269
267/* Globals */ 270/* Globals */
268
269struct globals { 271struct globals {
270 char *line_ptr; 272 char *line_ptr;
271 273
272 const char *disk_device; 274 const char *disk_device;
273 int fd; /* the disk */
274 int g_partitions; // = 4; /* maximum partition + 1 */ 275 int g_partitions; // = 4; /* maximum partition + 1 */
275 unsigned units_per_sector; // = 1; 276 unsigned units_per_sector; // = 1;
276 unsigned sector_size; // = DEFAULT_SECTOR_SIZE; 277 unsigned sector_size; // = DEFAULT_SECTOR_SIZE;
@@ -295,7 +296,6 @@ struct globals {
295#define G (*ptr_to_globals) 296#define G (*ptr_to_globals)
296#define line_ptr (G.line_ptr) 297#define line_ptr (G.line_ptr)
297#define disk_device (G.disk_device ) 298#define disk_device (G.disk_device )
298#define fd (G.fd )
299#define g_partitions (G.g_partitions ) 299#define g_partitions (G.g_partitions )
300#define units_per_sector (G.units_per_sector ) 300#define units_per_sector (G.units_per_sector )
301#define sector_size (G.sector_size ) 301#define sector_size (G.sector_size )
@@ -323,7 +323,7 @@ struct globals {
323 323
324 324
325/* TODO: move to libbb? */ 325/* TODO: move to libbb? */
326static ullong bb_BLKGETSIZE_sectors(void) 326static ullong bb_BLKGETSIZE_sectors(int fd)
327{ 327{
328 uint64_t v64; 328 uint64_t v64;
329 unsigned long longsectors; 329 unsigned long longsectors;
@@ -642,9 +642,6 @@ get_nr_sects(const struct partition *p)
642 return read4_little_endian(p->size4); 642 return read4_little_endian(p->size4);
643} 643}
644 644
645/* normally O_RDWR, -l option gives O_RDONLY */
646static int type_open = O_RDWR;
647
648static int ext_index; /* the prime extended partition */ 645static int ext_index; /* the prime extended partition */
649static smallint listing; /* no aborts for fdisk -l */ 646static smallint listing; /* no aborts for fdisk -l */
650static smallint dos_compatible_flag = 1; 647static smallint dos_compatible_flag = 1;
@@ -663,7 +660,7 @@ static ullong total_number_of_sectors;
663static void fdisk_fatal(const char *why) 660static void fdisk_fatal(const char *why)
664{ 661{
665 if (listing) { 662 if (listing) {
666 close(fd); 663 close(dev_fd);
667 longjmp(listingbuf, 1); 664 longjmp(listingbuf, 1);
668 } 665 }
669 bb_error_msg_and_die(why, disk_device); 666 bb_error_msg_and_die(why, disk_device);
@@ -674,11 +671,11 @@ seek_sector(ullong secno)
674{ 671{
675 secno *= sector_size; 672 secno *= sector_size;
676#if ENABLE_FDISK_SUPPORT_LARGE_DISKS 673#if ENABLE_FDISK_SUPPORT_LARGE_DISKS
677 if (lseek64(fd, (off64_t)secno, SEEK_SET) == (off64_t) -1) 674 if (lseek64(dev_fd, (off64_t)secno, SEEK_SET) == (off64_t) -1)
678 fdisk_fatal(unable_to_seek); 675 fdisk_fatal(unable_to_seek);
679#else 676#else
680 if (secno > MAXINT(off_t) 677 if (secno > MAXINT(off_t)
681 || lseek(fd, (off_t)secno, SEEK_SET) == (off_t) -1 678 || lseek(dev_fd, (off_t)secno, SEEK_SET) == (off_t) -1
682 ) { 679 ) {
683 fdisk_fatal(unable_to_seek); 680 fdisk_fatal(unable_to_seek);
684 } 681 }
@@ -690,7 +687,7 @@ static void
690write_sector(ullong secno, char *buf) 687write_sector(ullong secno, char *buf)
691{ 688{
692 seek_sector(secno); 689 seek_sector(secno);
693 if (write(fd, buf, sector_size) != sector_size) 690 if (write(dev_fd, buf, sector_size) != sector_size)
694 fdisk_fatal(unable_to_write); 691 fdisk_fatal(unable_to_write);
695} 692}
696#endif 693#endif
@@ -702,7 +699,7 @@ read_pte(struct pte *pe, ullong offset)
702 pe->offset = offset; 699 pe->offset = offset;
703 pe->sectorbuffer = xmalloc(sector_size); 700 pe->sectorbuffer = xmalloc(sector_size);
704 seek_sector(offset); 701 seek_sector(offset);
705 if (read(fd, pe->sectorbuffer, sector_size) != sector_size) 702 if (read(dev_fd, pe->sectorbuffer, sector_size) != sector_size)
706 fdisk_fatal(unable_to_read); 703 fdisk_fatal(unable_to_read);
707#if ENABLE_FEATURE_FDISK_WRITABLE 704#if ENABLE_FEATURE_FDISK_WRITABLE
708 pe->changed = 0; 705 pe->changed = 0;
@@ -1126,7 +1123,7 @@ create_doslabel(void)
1126 1123
1127 printf(msg_building_new_label, "DOS disklabel"); 1124 printf(msg_building_new_label, "DOS disklabel");
1128 1125
1129 current_label_type = label_dos; 1126 current_label_type = LABEL_DOS;
1130 1127
1131#if ENABLE_FEATURE_OSF_LABEL 1128#if ENABLE_FEATURE_OSF_LABEL
1132 possibly_osf_label = 0; 1129 possibly_osf_label = 0;
@@ -1139,7 +1136,7 @@ create_doslabel(void)
1139 extended_offset = 0; 1136 extended_offset = 0;
1140 set_all_unchanged(); 1137 set_all_unchanged();
1141 set_changed(0); 1138 set_changed(0);
1142 get_boot(create_empty_dos); 1139 get_boot(CREATE_EMPTY_DOS);
1143} 1140}
1144#endif /* FEATURE_FDISK_WRITABLE */ 1141#endif /* FEATURE_FDISK_WRITABLE */
1145 1142
@@ -1148,7 +1145,7 @@ get_sectorsize(void)
1148{ 1145{
1149 if (!user_set_sector_size) { 1146 if (!user_set_sector_size) {
1150 int arg; 1147 int arg;
1151 if (ioctl(fd, BLKSSZGET, &arg) == 0) 1148 if (ioctl(dev_fd, BLKSSZGET, &arg) == 0)
1152 sector_size = arg; 1149 sector_size = arg;
1153 if (sector_size != DEFAULT_SECTOR_SIZE) 1150 if (sector_size != DEFAULT_SECTOR_SIZE)
1154 printf("Note: sector size is %d (not %d)\n", 1151 printf("Note: sector size is %d (not %d)\n",
@@ -1161,7 +1158,7 @@ get_kernel_geometry(void)
1161{ 1158{
1162 struct hd_geometry geometry; 1159 struct hd_geometry geometry;
1163 1160
1164 if (!ioctl(fd, HDIO_GETGEO, &geometry)) { 1161 if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
1165 kern_heads = geometry.heads; 1162 kern_heads = geometry.heads;
1166 kern_sectors = geometry.sectors; 1163 kern_sectors = geometry.sectors;
1167 /* never use geometry.cylinders - it is truncated */ 1164 /* never use geometry.cylinders - it is truncated */
@@ -1224,7 +1221,7 @@ get_geometry(void)
1224 g_sectors = user_sectors ? user_sectors : 1221 g_sectors = user_sectors ? user_sectors :
1225 pt_sectors ? pt_sectors : 1222 pt_sectors ? pt_sectors :
1226 kern_sectors ? kern_sectors : 63; 1223 kern_sectors ? kern_sectors : 63;
1227 total_number_of_sectors = bb_BLKGETSIZE_sectors(); 1224 total_number_of_sectors = bb_BLKGETSIZE_sectors(dev_fd);
1228 1225
1229 sector_offset = 1; 1226 sector_offset = 1;
1230 if (dos_compatible_flag) 1227 if (dos_compatible_flag)
@@ -1236,7 +1233,9 @@ get_geometry(void)
1236} 1233}
1237 1234
1238/* 1235/*
1239 * Read MBR. Returns: 1236 * Opens disk_device and optionally reads MBR.
1237 * FIXME: document what each 'what' value will do!
1238 * Returns:
1240 * -1: no 0xaa55 flag present (possibly entire disk BSD) 1239 * -1: no 0xaa55 flag present (possibly entire disk BSD)
1241 * 0: found or created label 1240 * 0: found or created label
1242 * 1: I/O error 1241 * 1: I/O error
@@ -1248,82 +1247,80 @@ static int get_boot(void)
1248#define get_boot(what) get_boot() 1247#define get_boot(what) get_boot()
1249#endif 1248#endif
1250{ 1249{
1251 int i; 1250 int i, fd;
1252 1251
1253 g_partitions = 4; 1252 g_partitions = 4;
1254
1255 for (i = 0; i < 4; i++) { 1253 for (i = 0; i < 4; i++) {
1256 struct pte *pe = &ptes[i]; 1254 struct pte *pe = &ptes[i];
1257
1258 pe->part_table = pt_offset(MBRbuffer, i); 1255 pe->part_table = pt_offset(MBRbuffer, i);
1259 pe->ext_pointer = NULL; 1256 pe->ext_pointer = NULL;
1260 pe->offset = 0; 1257 pe->offset = 0;
1261 pe->sectorbuffer = MBRbuffer; 1258 pe->sectorbuffer = MBRbuffer;
1262#if ENABLE_FEATURE_FDISK_WRITABLE 1259#if ENABLE_FEATURE_FDISK_WRITABLE
1263 pe->changed = (what == create_empty_dos); 1260 pe->changed = (what == CREATE_EMPTY_DOS);
1264#endif 1261#endif
1265 } 1262 }
1266 1263
1267#if ENABLE_FEATURE_SUN_LABEL
1268 if (what == create_empty_sun && check_sun_label())
1269 return 0;
1270#endif
1271
1272 memset(MBRbuffer, 0, 512);
1273
1274#if ENABLE_FEATURE_FDISK_WRITABLE 1264#if ENABLE_FEATURE_FDISK_WRITABLE
1275 if (what == create_empty_dos) 1265// ALERT! highly idiotic design!
1276 goto got_dos_table; /* skip reading disk */ 1266// We end up here when we call get_boot() recursively
1267// via get_boot() [table is bad] -> create_doslabel() -> get_boot(CREATE_EMPTY_DOS).
1268// or get_boot() [table is bad] -> create_sunlabel() -> get_boot(CREATE_EMPTY_SUN).
1269// (just factor out re-init of ptes[0,1,2,3] in a separate fn instead?)
1270// So skip opening device _again_...
1271 if (what == CREATE_EMPTY_DOS USE_FEATURE_SUN_LABEL(|| what == CREATE_EMPTY_SUN))
1272 goto created_table;
1273
1274 fd = open(disk_device, (option_mask32 & OPT_l) ? O_RDONLY : O_RDWR);
1277 1275
1278 fd = open(disk_device, type_open);
1279 if (fd < 0) { 1276 if (fd < 0) {
1280 fd = open(disk_device, O_RDONLY); 1277 fd = open(disk_device, O_RDONLY);
1281 if (fd < 0) { 1278 if (fd < 0) {
1282 if (what == try_only) 1279 if (what == TRY_ONLY)
1283 return 1; 1280 return 1;
1284 fdisk_fatal(unable_to_open); 1281 fdisk_fatal(unable_to_open);
1285 } else 1282 }
1286 printf("You will not be able to write " 1283 xmove_fd(fd, dev_fd);
1287 "the partition table\n"); 1284 printf("'%s' is opened for read only\n", disk_device);
1288 } 1285 }
1289 1286 if (512 != read(dev_fd, MBRbuffer, 512)) {
1290 if (512 != read(fd, MBRbuffer, 512)) { 1287 if (what == TRY_ONLY) {
1291 if (what == try_only) 1288 close(dev_fd);
1292 return 1; 1289 return 1;
1290 }
1293 fdisk_fatal(unable_to_read); 1291 fdisk_fatal(unable_to_read);
1294 } 1292 }
1295#else 1293#else
1296 fd = open(disk_device, O_RDONLY); 1294 fd = open(disk_device, O_RDONLY);
1297 if (fd < 0) 1295 if (fd < 0)
1298 return 1; 1296 return 1;
1299 if (512 != read(fd, MBRbuffer, 512)) 1297 if (512 != read(fd, MBRbuffer, 512)) {
1298 close(fd);
1300 return 1; 1299 return 1;
1300 }
1301 xmove_fd(fd, dev_fd);
1301#endif 1302#endif
1302 1303
1303 get_geometry(); 1304 get_geometry();
1304
1305 update_units(); 1305 update_units();
1306 1306
1307#if ENABLE_FEATURE_SUN_LABEL 1307#if ENABLE_FEATURE_SUN_LABEL
1308 if (check_sun_label()) 1308 if (check_sun_label())
1309 return 0; 1309 return 0;
1310#endif 1310#endif
1311
1312#if ENABLE_FEATURE_SGI_LABEL 1311#if ENABLE_FEATURE_SGI_LABEL
1313 if (check_sgi_label()) 1312 if (check_sgi_label())
1314 return 0; 1313 return 0;
1315#endif 1314#endif
1316
1317#if ENABLE_FEATURE_AIX_LABEL 1315#if ENABLE_FEATURE_AIX_LABEL
1318 if (check_aix_label()) 1316 if (check_aix_label())
1319 return 0; 1317 return 0;
1320#endif 1318#endif
1321
1322#if ENABLE_FEATURE_OSF_LABEL 1319#if ENABLE_FEATURE_OSF_LABEL
1323 if (check_osf_label()) { 1320 if (check_osf_label()) {
1324 possibly_osf_label = 1; 1321 possibly_osf_label = 1;
1325 if (!valid_part_table_flag(MBRbuffer)) { 1322 if (!valid_part_table_flag(MBRbuffer)) {
1326 current_label_type = label_osf; 1323 current_label_type = LABEL_OSF;
1327 return 0; 1324 return 0;
1328 } 1325 }
1329 printf("This disk has both DOS and BSD magic.\n" 1326 printf("This disk has both DOS and BSD magic.\n"
@@ -1331,49 +1328,34 @@ static int get_boot(void)
1331 } 1328 }
1332#endif 1329#endif
1333 1330
1334#if ENABLE_FEATURE_FDISK_WRITABLE
1335 got_dos_table:
1336#endif
1337
1338 if (!valid_part_table_flag(MBRbuffer)) {
1339#if !ENABLE_FEATURE_FDISK_WRITABLE 1331#if !ENABLE_FEATURE_FDISK_WRITABLE
1332 if (!valid_part_table_flag(MBRbuffer))
1340 return -1; 1333 return -1;
1341#else 1334#else
1342 switch (what) { 1335 if (!valid_part_table_flag(MBRbuffer)) {
1343 case fdisk: 1336 if (what == OPEN_MAIN) {
1344 printf("Device contains neither a valid DOS " 1337 printf("Device contains neither a valid DOS "
1345 "partition table, nor Sun, SGI or OSF " 1338 "partition table, nor Sun, SGI or OSF "
1346 "disklabel\n"); 1339 "disklabel\n");
1347#ifdef __sparc__ 1340#ifdef __sparc__
1348#if ENABLE_FEATURE_SUN_LABEL 1341 USE_FEATURE_SUN_LABEL(create_sunlabel();)
1349 create_sunlabel();
1350#endif
1351#else 1342#else
1352 create_doslabel(); 1343 create_doslabel();
1353#endif 1344#endif
1354 return 0; 1345 return 0;
1355 case try_only:
1356 return -1;
1357 case create_empty_dos:
1358#if ENABLE_FEATURE_SUN_LABEL
1359 case create_empty_sun:
1360#endif
1361 break;
1362 default:
1363 bb_error_msg_and_die("internal error");
1364 } 1346 }
1365#endif /* FEATURE_FDISK_WRITABLE */ 1347 /* TRY_ONLY: */
1348 return -1;
1366 } 1349 }
1350 created_table:
1351#endif /* FEATURE_FDISK_WRITABLE */
1367 1352
1368#if ENABLE_FEATURE_FDISK_WRITABLE 1353
1369 warn_cylinders(); 1354 USE_FEATURE_FDISK_WRITABLE(warn_cylinders();)
1370#endif
1371 warn_geometry(); 1355 warn_geometry();
1372 1356
1373 for (i = 0; i < 4; i++) { 1357 for (i = 0; i < 4; i++) {
1374 struct pte *pe = &ptes[i]; 1358 if (IS_EXTENDED(ptes[i].part_table->sys_ind)) {
1375
1376 if (IS_EXTENDED(pe->part_table->sys_ind)) {
1377 if (g_partitions != 4) 1359 if (g_partitions != 4)
1378 printf("Ignoring extra extended " 1360 printf("Ignoring extra extended "
1379 "partition %d\n", i + 1); 1361 "partition %d\n", i + 1);
@@ -1384,16 +1366,13 @@ static int get_boot(void)
1384 1366
1385 for (i = 3; i < g_partitions; i++) { 1367 for (i = 3; i < g_partitions; i++) {
1386 struct pte *pe = &ptes[i]; 1368 struct pte *pe = &ptes[i];
1387
1388 if (!valid_part_table_flag(pe->sectorbuffer)) { 1369 if (!valid_part_table_flag(pe->sectorbuffer)) {
1389 printf("Warning: invalid flag 0x%02x,0x%02x of partition " 1370 printf("Warning: invalid flag 0x%02x,0x%02x of partition "
1390 "table %d will be corrected by w(rite)\n", 1371 "table %d will be corrected by w(rite)\n",
1391 pe->sectorbuffer[510], 1372 pe->sectorbuffer[510],
1392 pe->sectorbuffer[511], 1373 pe->sectorbuffer[511],
1393 i + 1); 1374 i + 1);
1394#if ENABLE_FEATURE_FDISK_WRITABLE 1375 USE_FEATURE_FDISK_WRITABLE(pe->changed = 1;)
1395 pe->changed = 1;
1396#endif
1397 } 1376 }
1398 } 1377 }
1399 1378
@@ -2474,7 +2453,7 @@ reread_partition_table(int leave)
2474 printf("Calling ioctl() to re-read partition table\n"); 2453 printf("Calling ioctl() to re-read partition table\n");
2475 sync(); 2454 sync();
2476 /* sleep(2); Huh? */ 2455 /* sleep(2); Huh? */
2477 i = ioctl_or_perror(fd, BLKRRPART, NULL, 2456 i = ioctl_or_perror(dev_fd, BLKRRPART, NULL,
2478 "WARNING: rereading partition table " 2457 "WARNING: rereading partition table "
2479 "failed, kernel still uses old table"); 2458 "failed, kernel still uses old table");
2480#if 0 2459#if 0
@@ -2487,7 +2466,7 @@ reread_partition_table(int leave)
2487 2466
2488 if (leave) { 2467 if (leave) {
2489 if (ENABLE_FEATURE_CLEAN_UP) 2468 if (ENABLE_FEATURE_CLEAN_UP)
2490 close(fd); 2469 close(dev_fd);
2491 exit(i != 0); 2470 exit(i != 0);
2492 } 2471 }
2493} 2472}
@@ -2619,7 +2598,8 @@ xselect(void)
2619 x_list_table(0); 2598 x_list_table(0);
2620 break; 2599 break;
2621 case 'q': 2600 case 'q':
2622 close(fd); 2601 if (ENABLE_FEATURE_CLEAN_UP)
2602 close(dev_fd);
2623 bb_putchar('\n'); 2603 bb_putchar('\n');
2624 exit(0); 2604 exit(0);
2625 case 'r': 2605 case 'r':
@@ -2687,7 +2667,7 @@ is_ide_cdrom_or_tape(const char *device)
2687 2667
2688 2668
2689static void 2669static void
2690trydev(const char *device, int user_specified) 2670open_list_and_close(const char *device, int user_specified)
2691{ 2671{
2692 int gb; 2672 int gb;
2693 2673
@@ -2697,46 +2677,44 @@ trydev(const char *device, int user_specified)
2697 if (!user_specified) 2677 if (!user_specified)
2698 if (is_ide_cdrom_or_tape(device)) 2678 if (is_ide_cdrom_or_tape(device))
2699 return; 2679 return;
2700 fd = open(disk_device, type_open); 2680
2701 if (fd >= 0) { 2681 /* Open disk_device, save file descriptor to dev_fd */
2702 gb = get_boot(try_only); 2682 errno = 0;
2703 if (gb > 0) { /* I/O error */ 2683 gb = get_boot(TRY_ONLY);
2704 close(fd); 2684 if (gb > 0) { /* I/O error */
2705 } else if (gb < 0) { /* no DOS signature */
2706 list_disk_geometry();
2707 if (LABEL_IS_AIX) {
2708 return;
2709 }
2710#if ENABLE_FEATURE_OSF_LABEL
2711 if (bsd_trydev(device) < 0)
2712#endif
2713 printf("Disk %s doesn't contain a valid "
2714 "partition table\n", device);
2715 close(fd);
2716 } else {
2717 close(fd);
2718 list_table(0);
2719#if ENABLE_FEATURE_FDISK_WRITABLE
2720 if (!LABEL_IS_SUN && g_partitions > 4){
2721 delete_partition(ext_index);
2722 }
2723#endif
2724 }
2725 } else {
2726 /* Ignore other errors, since we try IDE 2685 /* Ignore other errors, since we try IDE
2727 and SCSI hard disks which may not be 2686 and SCSI hard disks which may not be
2728 installed on the system. */ 2687 installed on the system. */
2729 if (errno == EACCES) { 2688 if (user_specified || errno == EACCES)
2730 printf("Cannot open %s\n", device); 2689 bb_perror_msg("can't open '%s'", device);
2731 return; 2690 return;
2691 }
2692
2693 if (gb < 0) { /* no DOS signature */
2694 list_disk_geometry();
2695 if (LABEL_IS_AIX)
2696 goto ret;
2697#if ENABLE_FEATURE_OSF_LABEL
2698 if (bsd_trydev(device) < 0)
2699#endif
2700 printf("Disk %s doesn't contain a valid "
2701 "partition table\n", device);
2702 } else {
2703 list_table(0);
2704#if ENABLE_FEATURE_FDISK_WRITABLE
2705 if (!LABEL_IS_SUN && g_partitions > 4) {
2706 delete_partition(ext_index);
2732 } 2707 }
2708#endif
2733 } 2709 }
2710 ret:
2711 close(dev_fd);
2734} 2712}
2735 2713
2736/* for fdisk -l: try all things in /proc/partitions 2714/* for fdisk -l: try all things in /proc/partitions
2737 that look like a partition name (do not end in a digit) */ 2715 that look like a partition name (do not end in a digit) */
2738static void 2716static void
2739tryprocpt(void) 2717list_devs_in_proc_partititons(void)
2740{ 2718{
2741 FILE *procpt; 2719 FILE *procpt;
2742 char line[100], ptname[100], devname[120], *s; 2720 char line[100], ptname[100], devname[120], *s;
@@ -2753,7 +2731,7 @@ tryprocpt(void)
2753 if (isdigit(s[-1])) 2731 if (isdigit(s[-1]))
2754 continue; 2732 continue;
2755 sprintf(devname, "/dev/%s", ptname); 2733 sprintf(devname, "/dev/%s", ptname);
2756 trydev(devname, 0); 2734 open_list_and_close(devname, 0);
2757 } 2735 }
2758#if ENABLE_FEATURE_CLEAN_UP 2736#if ENABLE_FEATURE_CLEAN_UP
2759 fclose(procpt); 2737 fclose(procpt);
@@ -2792,6 +2770,8 @@ int fdisk_main(int argc, char **argv)
2792 2770
2793 INIT_G(); 2771 INIT_G();
2794 2772
2773 close(dev_fd); /* just in case */
2774
2795 opt_complementary = "b+:C+:H+:S+"; /* numeric params */ 2775 opt_complementary = "b+:C+:H+:S+"; /* numeric params */
2796 opt = getopt32(argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"), 2776 opt = getopt32(argv, "b:C:H:lS:u" USE_FEATURE_FDISK_BLKSIZE("s"),
2797 &sector_size, &user_cylinders, &user_heads, &user_sectors); 2777 &sector_size, &user_cylinders, &user_heads, &user_sectors);
@@ -2819,16 +2799,15 @@ int fdisk_main(int argc, char **argv)
2819 if (opt & OPT_l) { 2799 if (opt & OPT_l) {
2820 nowarn = 1; 2800 nowarn = 1;
2821#endif 2801#endif
2822 type_open = O_RDONLY;
2823 if (*argv) { 2802 if (*argv) {
2824 listing = 1; 2803 listing = 1;
2825 do { 2804 do {
2826 trydev(*argv, 1); 2805 open_list_and_close(*argv, 1);
2827 } while (*++argv); 2806 } while (*++argv);
2828 } else { 2807 } else {
2829 /* we don't have device names, */ 2808 /* we don't have device names, */
2830 /* use /proc/partitions instead */ 2809 /* use /proc/partitions instead */
2831 tryprocpt(); 2810 list_devs_in_proc_partititons();
2832 } 2811 }
2833 return 0; 2812 return 0;
2834#if ENABLE_FEATURE_FDISK_WRITABLE 2813#if ENABLE_FEATURE_FDISK_WRITABLE
@@ -2845,7 +2824,7 @@ int fdisk_main(int argc, char **argv)
2845 for (j = 0; j < argc; j++) { 2824 for (j = 0; j < argc; j++) {
2846 unsigned long long size; 2825 unsigned long long size;
2847 fd = xopen(argv[j], O_RDONLY); 2826 fd = xopen(argv[j], O_RDONLY);
2848 size = bb_BLKGETSIZE_sectors() / 2; 2827 size = bb_BLKGETSIZE_sectors(fd) / 2;
2849 close(fd); 2828 close(fd);
2850 if (argc == 1) 2829 if (argc == 1)
2851 printf("%lld\n", size); 2830 printf("%lld\n", size);
@@ -2861,7 +2840,7 @@ int fdisk_main(int argc, char **argv)
2861 bb_show_usage(); 2840 bb_show_usage();
2862 2841
2863 disk_device = argv[0]; 2842 disk_device = argv[0];
2864 get_boot(fdisk); 2843 get_boot(OPEN_MAIN);
2865 2844
2866 if (LABEL_IS_OSF) { 2845 if (LABEL_IS_OSF) {
2867 /* OSF label, and no DOS label */ 2846 /* OSF label, and no DOS label */
@@ -2869,7 +2848,7 @@ int fdisk_main(int argc, char **argv)
2869 "disklabel mode\n", disk_device); 2848 "disklabel mode\n", disk_device);
2870 bsd_select(); 2849 bsd_select();
2871 /*Why do we do this? It seems to be counter-intuitive*/ 2850 /*Why do we do this? It seems to be counter-intuitive*/
2872 current_label_type = label_dos; 2851 current_label_type = LABEL_DOS;
2873 /* If we return we may want to make an empty DOS label? */ 2852 /* If we return we may want to make an empty DOS label? */
2874 } 2853 }
2875 2854
@@ -2954,7 +2933,7 @@ int fdisk_main(int argc, char **argv)
2954 list_table(0); 2933 list_table(0);
2955 break; 2934 break;
2956 case 'q': 2935 case 'q':
2957 close(fd); 2936 close(dev_fd);
2958 bb_putchar('\n'); 2937 bb_putchar('\n');
2959 return 0; 2938 return 0;
2960 case 's': 2939 case 's':
diff --git a/util-linux/fdisk_aix.c b/util-linux/fdisk_aix.c
index 0b9fa2be9..83be8a8f9 100644
--- a/util-linux/fdisk_aix.c
+++ b/util-linux/fdisk_aix.c
@@ -62,7 +62,7 @@ check_aix_label(void)
62 } 62 }
63 aix_other_endian = (aixlabel->magic == AIX_LABEL_MAGIC_SWAPPED); 63 aix_other_endian = (aixlabel->magic == AIX_LABEL_MAGIC_SWAPPED);
64 update_units(); 64 update_units();
65 current_label_type = label_aix; 65 current_label_type = LABEL_AIX;
66 g_partitions = 1016; 66 g_partitions = 1016;
67 aix_volumes = 15; 67 aix_volumes = 15;
68 aix_info(); 68 aix_info();
diff --git a/util-linux/fdisk_osf.c b/util-linux/fdisk_osf.c
index 5a7e6323d..ba01a7f0c 100644
--- a/util-linux/fdisk_osf.c
+++ b/util-linux/fdisk_osf.c
@@ -413,7 +413,8 @@ bsd_select(void)
413 xbsd_print_disklabel(0); 413 xbsd_print_disklabel(0);
414 break; 414 break;
415 case 'q': 415 case 'q':
416 close(fd); 416 if (ENABLE_FEATURE_CLEAN_UP)
417 close(dev_fd);
417 exit(EXIT_SUCCESS); 418 exit(EXIT_SUCCESS);
418 case 'r': 419 case 'r':
419 return; 420 return;
@@ -627,12 +628,13 @@ xbsd_create_disklabel(void)
627#else 628#else
628 xbsd_part 629 xbsd_part
629#endif 630#endif
630 ) == 1) { 631 ) == 1) {
631 xbsd_print_disklabel(1); 632 xbsd_print_disklabel(1);
632 return 1; 633 return 1;
633 } else 634 }
634 return 0; 635 return 0;
635 } else if (c == 'n') 636 }
637 if (c == 'n')
636 return 0; 638 return 0;
637 } 639 }
638} 640}
@@ -766,9 +768,9 @@ xbsd_write_bootstrap(void)
766 sector = get_start_sect(xbsd_part); 768 sector = get_start_sect(xbsd_part);
767#endif 769#endif
768 770
769 if (lseek(fd, sector * SECTOR_SIZE, SEEK_SET) == -1) 771 if (lseek(dev_fd, sector * SECTOR_SIZE, SEEK_SET) == -1)
770 fdisk_fatal(unable_to_seek); 772 fdisk_fatal(unable_to_seek);
771 if (BSD_BBSIZE != write(fd, disklabelbuffer, BSD_BBSIZE)) 773 if (BSD_BBSIZE != write(dev_fd, disklabelbuffer, BSD_BBSIZE))
772 fdisk_fatal(unable_to_write); 774 fdisk_fatal(unable_to_write);
773 775
774#if defined(__alpha__) 776#if defined(__alpha__)
@@ -939,9 +941,9 @@ xbsd_readlabel(struct partition *p)
939 sector = 0; 941 sector = 0;
940#endif 942#endif
941 943
942 if (lseek(fd, sector * SECTOR_SIZE, SEEK_SET) == -1) 944 if (lseek(dev_fd, sector * SECTOR_SIZE, SEEK_SET) == -1)
943 fdisk_fatal(unable_to_seek); 945 fdisk_fatal(unable_to_seek);
944 if (BSD_BBSIZE != read(fd, disklabelbuffer, BSD_BBSIZE)) 946 if (BSD_BBSIZE != read(dev_fd, disklabelbuffer, BSD_BBSIZE))
945 fdisk_fatal(unable_to_read); 947 fdisk_fatal(unable_to_read);
946 948
947 memmove(d, &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET], 949 memmove(d, &disklabelbuffer[BSD_LABELSECTOR * SECTOR_SIZE + BSD_LABELOFFSET],
@@ -985,14 +987,14 @@ xbsd_writelabel(struct partition *p)
985 987
986#if defined(__alpha__) && BSD_LABELSECTOR == 0 988#if defined(__alpha__) && BSD_LABELSECTOR == 0
987 alpha_bootblock_checksum(disklabelbuffer); 989 alpha_bootblock_checksum(disklabelbuffer);
988 if (lseek(fd, 0, SEEK_SET) == -1) 990 if (lseek(dev_fd, 0, SEEK_SET) == -1)
989 fdisk_fatal(unable_to_seek); 991 fdisk_fatal(unable_to_seek);
990 if (BSD_BBSIZE != write(fd, disklabelbuffer, BSD_BBSIZE)) 992 if (BSD_BBSIZE != write(dev_fd, disklabelbuffer, BSD_BBSIZE))
991 fdisk_fatal(unable_to_write); 993 fdisk_fatal(unable_to_write);
992#else 994#else
993 if (lseek(fd, sector * SECTOR_SIZE + BSD_LABELOFFSET, SEEK_SET) == -1) 995 if (lseek(dev_fd, sector * SECTOR_SIZE + BSD_LABELOFFSET, SEEK_SET) == -1)
994 fdisk_fatal(unable_to_seek); 996 fdisk_fatal(unable_to_seek);
995 if (sizeof(struct xbsd_disklabel) != write(fd, d, sizeof(struct xbsd_disklabel))) 997 if (sizeof(struct xbsd_disklabel) != write(dev_fd, d, sizeof(struct xbsd_disklabel)))
996 fdisk_fatal(unable_to_write); 998 fdisk_fatal(unable_to_write);
997#endif 999#endif
998 sync_disks(); 1000 sync_disks();
diff --git a/util-linux/fdisk_sgi.c b/util-linux/fdisk_sgi.c
index 1fce0c1c7..55e77d58c 100644
--- a/util-linux/fdisk_sgi.c
+++ b/util-linux/fdisk_sgi.c
@@ -235,7 +235,7 @@ check_sgi_label(void)
235 if (sgilabel->magic != SGI_LABEL_MAGIC 235 if (sgilabel->magic != SGI_LABEL_MAGIC
236 && sgilabel->magic != SGI_LABEL_MAGIC_SWAPPED 236 && sgilabel->magic != SGI_LABEL_MAGIC_SWAPPED
237 ) { 237 ) {
238 current_label_type = label_dos; 238 current_label_type = LABEL_DOS;
239 return 0; 239 return 0;
240 } 240 }
241 241
@@ -248,7 +248,7 @@ check_sgi_label(void)
248 printf("Detected sgi disklabel with wrong checksum\n"); 248 printf("Detected sgi disklabel with wrong checksum\n");
249 } 249 }
250 update_units(); 250 update_units();
251 current_label_type = label_sgi; 251 current_label_type = LABEL_SGI;
252 g_partitions = 16; 252 g_partitions = 16;
253 sgi_volumes = 15; 253 sgi_volumes = 15;
254 return 1; 254 return 1;
@@ -439,9 +439,9 @@ sgi_write_table(void)
439 assert(two_s_complement_32bit_sum( 439 assert(two_s_complement_32bit_sum(
440 (unsigned int*)sgilabel, sizeof(*sgilabel)) == 0); 440 (unsigned int*)sgilabel, sizeof(*sgilabel)) == 0);
441 441
442 if (lseek(fd, 0, SEEK_SET) < 0) 442 if (lseek(dev_fd, 0, SEEK_SET) < 0)
443 fdisk_fatal(unable_to_seek); 443 fdisk_fatal(unable_to_seek);
444 if (write(fd, sgilabel, SECTOR_SIZE) != SECTOR_SIZE) 444 if (write(dev_fd, sgilabel, SECTOR_SIZE) != SECTOR_SIZE)
445 fdisk_fatal(unable_to_write); 445 fdisk_fatal(unable_to_write);
446 if (!strncmp((char*)sgilabel->directory[0].vol_file_name, "sgilabel", 8)) { 446 if (!strncmp((char*)sgilabel->directory[0].vol_file_name, "sgilabel", 8)) {
447 /* 447 /*
@@ -450,9 +450,9 @@ sgi_write_table(void)
450 */ 450 */
451 sgiinfo *info = fill_sgiinfo(); 451 sgiinfo *info = fill_sgiinfo();
452 int infostartblock = SGI_SSWAP32(sgilabel->directory[0].vol_file_start); 452 int infostartblock = SGI_SSWAP32(sgilabel->directory[0].vol_file_start);
453 if (lseek(fd, infostartblock*SECTOR_SIZE, SEEK_SET) < 0) 453 if (lseek(dev_fd, infostartblock*SECTOR_SIZE, SEEK_SET) < 0)
454 fdisk_fatal(unable_to_seek); 454 fdisk_fatal(unable_to_seek);
455 if (write(fd, info, SECTOR_SIZE) != SECTOR_SIZE) 455 if (write(dev_fd, info, SECTOR_SIZE) != SECTOR_SIZE)
456 fdisk_fatal(unable_to_write); 456 fdisk_fatal(unable_to_write);
457 free(info); 457 free(info);
458 } 458 }
@@ -782,8 +782,8 @@ create_sgilabel(void)
782 printf(msg_building_new_label, "SGI disklabel"); 782 printf(msg_building_new_label, "SGI disklabel");
783 783
784 sgi_other_endian = BB_LITTLE_ENDIAN; 784 sgi_other_endian = BB_LITTLE_ENDIAN;
785 res = ioctl(fd, BLKGETSIZE, &longsectors); 785 res = ioctl(dev_fd, BLKGETSIZE, &longsectors);
786 if (!ioctl(fd, HDIO_GETGEO, &geometry)) { 786 if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
787 g_heads = geometry.heads; 787 g_heads = geometry.heads;
788 g_sectors = geometry.sectors; 788 g_sectors = geometry.sectors;
789 if (res == 0) { 789 if (res == 0) {
@@ -851,7 +851,7 @@ create_sgilabel(void)
851 //sgilabel->devparam.xylogics_writecont = SGI_SSWAP16(0); 851 //sgilabel->devparam.xylogics_writecont = SGI_SSWAP16(0);
852 //memset( &(sgilabel->directory), 0, sizeof(struct volume_directory)*15 ); 852 //memset( &(sgilabel->directory), 0, sizeof(struct volume_directory)*15 );
853 //memset( &(sgilabel->partitions), 0, sizeof(struct sgi_partinfo)*16 ); 853 //memset( &(sgilabel->partitions), 0, sizeof(struct sgi_partinfo)*16 );
854 current_label_type = label_sgi; 854 current_label_type = LABEL_SGI;
855 g_partitions = 16; 855 g_partitions = 16;
856 sgi_volumes = 15; 856 sgi_volumes = 15;
857 sgi_set_entire(); 857 sgi_set_entire();
diff --git a/util-linux/fdisk_sun.c b/util-linux/fdisk_sun.c
index fcd3818d2..e595444c4 100644
--- a/util-linux/fdisk_sun.c
+++ b/util-linux/fdisk_sun.c
@@ -42,7 +42,7 @@ guess_device_type(void)
42{ 42{
43 struct stat bootstat; 43 struct stat bootstat;
44 44
45 if (fstat(fd, &bootstat) < 0) { 45 if (fstat(dev_fd, &bootstat) < 0) {
46 scsi_disk = 0; 46 scsi_disk = 0;
47 floppy = 0; 47 floppy = 0;
48 } else if (S_ISBLK(bootstat.st_mode) 48 } else if (S_ISBLK(bootstat.st_mode)
@@ -98,7 +98,7 @@ check_sun_label(void)
98 98
99 if (sunlabel->magic != SUN_LABEL_MAGIC 99 if (sunlabel->magic != SUN_LABEL_MAGIC
100 && sunlabel->magic != SUN_LABEL_MAGIC_SWAPPED) { 100 && sunlabel->magic != SUN_LABEL_MAGIC_SWAPPED) {
101 current_label_type = label_dos; 101 current_label_type = LABEL_DOS;
102 sun_other_endian = 0; 102 sun_other_endian = 0;
103 return 0; 103 return 0;
104 } 104 }
@@ -116,7 +116,7 @@ check_sun_label(void)
116 g_sectors = SUN_SSWAP16(sunlabel->nsect); 116 g_sectors = SUN_SSWAP16(sunlabel->nsect);
117 } 117 }
118 update_units(); 118 update_units();
119 current_label_type = label_sun; 119 current_label_type = LABEL_SUN;
120 g_partitions = 8; 120 g_partitions = 8;
121 return 1; 121 return 1;
122} 122}
@@ -168,7 +168,7 @@ sun_autoconfigure_scsi(void)
168 char *q; 168 char *q;
169 int i; 169 int i;
170 170
171 if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, &id)) 171 if (ioctl(dev_fd, SCSI_IOCTL_GET_IDLUN, &id))
172 return NULL; 172 return NULL;
173 173
174 sprintf(buffer, 174 sprintf(buffer,
@@ -272,7 +272,7 @@ create_sunlabel(void)
272 } 272 }
273 } 273 }
274 if (!p || floppy) { 274 if (!p || floppy) {
275 if (!ioctl(fd, HDIO_GETGEO, &geometry)) { 275 if (!ioctl(dev_fd, HDIO_GETGEO, &geometry)) {
276 g_heads = geometry.heads; 276 g_heads = geometry.heads;
277 g_sectors = geometry.sectors; 277 g_sectors = geometry.sectors;
278 g_cylinders = geometry.cylinders; 278 g_cylinders = geometry.cylinders;
@@ -346,7 +346,7 @@ create_sunlabel(void)
346 346
347 set_all_unchanged(); 347 set_all_unchanged();
348 set_changed(0); 348 set_changed(0);
349 get_boot(create_empty_sun); 349 get_boot(CREATE_EMPTY_SUN);
350} 350}
351 351
352static void 352static void
@@ -722,9 +722,9 @@ sun_write_table(void)
722 while (ush < (unsigned short *)(&sunlabel->csum)) 722 while (ush < (unsigned short *)(&sunlabel->csum))
723 csum ^= *ush++; 723 csum ^= *ush++;
724 sunlabel->csum = csum; 724 sunlabel->csum = csum;
725 if (lseek(fd, 0, SEEK_SET) < 0) 725 if (lseek(dev_fd, 0, SEEK_SET) < 0)
726 fdisk_fatal(unable_to_seek); 726 fdisk_fatal(unable_to_seek);
727 if (write(fd, sunlabel, SECTOR_SIZE) != SECTOR_SIZE) 727 if (write(dev_fd, sunlabel, SECTOR_SIZE) != SECTOR_SIZE)
728 fdisk_fatal(unable_to_write); 728 fdisk_fatal(unable_to_write);
729} 729}
730#endif /* SUN_LABEL */ 730#endif /* SUN_LABEL */