Skip to content

Commit

Permalink
Simplied user input, and provided more meaningful defaults.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcmaster-au committed May 29, 2014
1 parent b40203e commit 689eb73
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 359 deletions.
17 changes: 1 addition & 16 deletions dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <errno.h>
#include <fcntl.h>

#include "hfdisk.h"
#include "io.h"
#include "errors.h"
#include "partition_map.h"
Expand All @@ -51,27 +52,11 @@
//
// Types
//
typedef struct names {
char *abbr;
char *full;
} NAMES;


//
// Global Constants
//
NAMES plist[] = {
{"Drvr", "Apple_Driver"},
{"Dr43", "Apple_Driver43"},
{"Free", "Apple_Free"},
{" HFS", "Apple_HFS"},
{" MFS", "Apple_MFS"},
{"PDOS", "Apple_PRODOS"},
{"junk", "Apple_Scratch"},
{"unix", "Apple_UNIX_SVR2"},
{" map", "Apple_partition_map"},
{0, 0}
};

const char * kStringEmpty = "";
const char * kStringNot = " not";
Expand Down
26 changes: 11 additions & 15 deletions hfdisk.8
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
.TH PDISK 8 "20 December 1996" "MkLinux DR2" "Linux Programmer's Manual"
.TH HFDISK 8 "20 December 1996" "MkLinux DR2" "Linux Programmer's Manual"
.SH NAME
pdisk \- Apple partition table editor for Linux
hfdisk \- Apple partition table editor
.SH SYNOPSIS
.B pdisk
.B hfdisk
.B "[\-h|\--help] [\-v|\--version] [\-l|\--list [name ...]]"
.br
.B pdisk
.B hfdisk
.B "[\-r|\--readonly]"
device ...
.SH DESCRIPTION
.B pdisk
.B hfdisk
is a menu driven program which partitions disks using the standard Apple
disk partitioning scheme described in "Inside Macintosh: Devices".
It does not support the intel/dos partitioning scheme supported by
Expand Down Expand Up @@ -52,12 +52,12 @@ is the partition described by the second entry in the partiton map on /dev/sda.
.TP
.B \-v | \--version
Prints version number of the
.B pdisk
.B hfdisk
program.
.TP
.B \-h | \--help
Prints a rather lame set of help messages for the
.B pdisk
.B hfdisk
program.
.TP
.B \-l | \--list
Expand All @@ -77,13 +77,13 @@ Otherwise, lists the partition tables for the specified
.TP
.B \-r | \--readonly
Prevents
.B pdisk
.B hfdisk
from writing to the device.
.SH "Editing Partition Tables"
An argument which is simply the name of a
.I device
indicates that
.B pdisk
.B hfdisk
should edit the partition table of that device.

The current top level editing commands are:
Expand Down Expand Up @@ -129,12 +129,8 @@ The
.B c
(create new partition) command is the only one with complicated arguments.
The first argument is the base address (in blocks) of the partition.
Besides a raw number, you can also specify a partition number followed
by the letter 'p' to indicate that the first block of the new partition should
be the same as the first block of that existing free space partition.
The second argument is the length of the partition in blocks.
This can be a raw number or can be a partition number followed by the
letter 'p' to use the size of that partition or can be a number followed
This can be a raw number or can be a number followed
by 'k', 'm', or 'g' to indicate the size in kilobytes, megabytes, or gigabytes
respectively.
(These are powers of 1024, of course, not powers of 1000.)
Expand Down Expand Up @@ -170,7 +166,7 @@ In order to use the new partition map you must reboot.
.SH BUGS
Some people believe there should really be just one disk partitioning utility.
.br
.B pdisk
.B hfdisk
should be able to create HFS partitions that work.
.br
Even more help should be available during user input.
Expand Down
117 changes: 61 additions & 56 deletions hfdisk.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#include <sys/ioctl.h>

Expand Down Expand Up @@ -65,6 +66,19 @@ enum getopt_values {
kListOption = 1001
};

const NAMES plist[] = {
{"Drvr", "Apple_Driver"},
{"Dr43", "Apple_Driver43"},
{"Free", "Apple_Free"},
{" HFS", "Apple_HFS"},
{" MFS", "Apple_MFS"},
{"PDOS", "Apple_PRODOS"},
{"junk", "Apple_Scratch"},
{"unix", "Apple_UNIX_SVR2"},
{" map", "Apple_partition_map"},
{0, 0}
};


//
// Global Variables
Expand All @@ -90,8 +104,7 @@ void do_reorder(partition_map_header *map);
void do_write_partition_map(partition_map_header *map);
void edit(char *name);
int get_base_argument(long *number, partition_map_header *map);
int get_command_line(int *argc, char ***argv);
int get_size_argument(long *number, partition_map_header *map);
int get_size_argument(uint32_t base, long *number, partition_map_header *map);
int get_options(int argc, char **argv);
void print_notes();

Expand Down Expand Up @@ -270,7 +283,6 @@ edit(char *name)
break;
case 'Q':
case 'q':
flush_to_newline(1);
goto finis;
break;
case 'I':
Expand Down Expand Up @@ -304,7 +316,6 @@ edit(char *name)
if (!dflag) {
goto do_error;
} else if (do_expert(map)) {
flush_to_newline(0);
goto finis;
}
break;
Expand Down Expand Up @@ -345,7 +356,7 @@ do_create_partition(partition_map_header *map, int get_type)
if (get_base_argument(&base, map) == 0) {
return;
}
if (get_size_argument(&length, map) == 0) {
if (get_size_argument(base, &length, map) == 0) {
return;
}

Expand All @@ -356,17 +367,27 @@ do_create_partition(partition_map_header *map, int get_type)
if (get_type == 0) {
add_partition_to_map(name, kUnixType, base, length, map);

} else if (get_string_argument("Type of partition: ", &type_name, 1) == 0) {
bad_input("Bad type");
return;
} else {
if (strncmp(type_name, kFreeType, DPISTRLEN) == 0) {
bad_input("Can't create a partition with the Free type");
return;
}
if (strncmp(type_name, kMapType, DPISTRLEN) == 0) {
bad_input("Can't create a partition with the Map type");
return;
while (1) {
if (get_string_argument("Type of partition (L for known types): ", &type_name, 1) == 0) {
bad_input("Bad type");
return;
} else if (strncmp(type_name, kFreeType, DPISTRLEN) == 0) {
bad_input("Can't create a partition with the Free type");
} else if (strncmp(type_name, kMapType, DPISTRLEN) == 0) {
bad_input("Can't create a partition with the Map type");
} else if (strncmp(type_name, "L", DPISTRLEN) == 0) {
int i = 0;
while (plist[i].full) {
printf("%s\n", plist[i].full);
++i;
}
printf("\n");
}
else
{
break;
}
}
add_partition_to_map(name, type_name, base, length, map);
}
Expand Down Expand Up @@ -400,62 +421,47 @@ do_create_bootstrap_partition(partition_map_header *map)
int
get_base_argument(long *number, partition_map_header *map)
{
partition_map * entry;
int c;
int result = 0;

if (get_number_argument("First block: ", number, kDefault) == 0) {
uint32_t defaultFirstBlock = find_free_space(map);
char prompt[32];
sprintf(prompt, "First block [%"PRIu32"]: ", defaultFirstBlock);
if (get_number_argument(prompt, number, defaultFirstBlock) == 0) {
bad_input("Bad block number");
} else {
result = 1;
c = getch();

if (c == 'p' || c == 'P') {
entry = find_entry_by_disk_address(*number, map);
if (entry == NULL) {
bad_input("Bad partition number");
result = 0;
} else {
*number = entry->data->dpme_pblock_start;
}
} else if (c > 0) {
ungetch(c);
}
}
return result;
}


int
get_size_argument(long *number, partition_map_header *map)
get_size_argument(uint32_t base, long *number, partition_map_header *map)
{
partition_map * entry;
int c;
int result = 0;
long multiple;

if (get_number_argument("Length (in blocks, kB (k), MB (M) or GB (G)): ", number, kDefault) == 0) {
uint32_t defaultSize = 20480; // 10MB

// Work out how much free-space is available.
partition_map* part = find_entry_by_sector(base, map);
if (part)
{
size_t partEnd =
part->data->dpme_pblock_start +
part->data->dpme_pblocks;
defaultSize = partEnd - base;
}

char prompt[80];
sprintf(
prompt,
"Length (in blocks, kB (k), MB (M) or GB (G)) [%"PRIu32"]: ",
defaultSize);

if (get_number_argument(prompt, number, defaultSize) == 0) {
bad_input("Bad length");
} else {
result = 1;
multiple = get_multiplier(PBLOCK_SIZE);
if (multiple != 1) {
*number *= multiple;
} else {
c = getch();

if (c == 'p' || c == 'P') {
entry = find_entry_by_disk_address(*number, map);
if (entry == NULL) {
bad_input("Bad partition number");
result = 0;
} else {
*number = entry->data->dpme_pblocks;
}
} else if (c > 0) {
ungetch(c);
}
}
}
return result;
}
Expand Down Expand Up @@ -536,7 +542,7 @@ do_write_partition_map(partition_map_header *map)
printf("the map causes all data on that partition to be LOST FOREVER. \n");
printf("Make sure you have a backup of any data on such partitions you \n");
printf("want to keep before answering 'yes' to the question below! \n\n");
if (get_okay("Write partition map? [n/y]: ", 0) != 1) {
if (get_okay("Write partition map? [N/y]: ") != 1) {
return;
}

Expand Down Expand Up @@ -579,7 +585,6 @@ do_expert(partition_map_header *map)
break;
case 'X':
case 'x':
flush_to_newline(1);
goto finis;
break;
case 'Q':
Expand Down
5 changes: 5 additions & 0 deletions hfdisk.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@
//
// Types
//
typedef struct names {
char *abbr;
char *full;
} NAMES;


//
// Global Constants
//
extern const NAMES plist[];


//
Expand Down
Loading

0 comments on commit 689eb73

Please sign in to comment.