#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <ctype.h>
#include "partition_map.h"
#include "errors.h"
#define CFLAG_DEFAULT 0
#define DFLAG_DEFAULT 0
#define HFLAG_DEFAULT 0
#define INTERACT_DEFAULT 0
#define RFLAG_DEFAULT 0
int hflag = HFLAG_DEFAULT;
int dflag = DFLAG_DEFAULT;
int rflag = RFLAG_DEFAULT;
int interactive = INTERACT_DEFAULT;
int cflag = CFLAG_DEFAULT;
void process(char *filename);
int
main(int argc, char **argv)
{
register int i;
#ifdef notdef
register int c;
extern char *optarg;
extern int optind;
extern int opterr;
extern int optopt;
int getopt_error;
char option_error[100];
char *arg_option = 0;
int bool_option = 0;
#else
int optind = 1;
#endif
init_program_name(argv);
#ifdef notdef
opterr = 0;
while ((c = getopt(argc, argv, "a:b")) != EOF) {
if (c == '?') {
getopt_error = 1;
c = optopt;
} else {
getopt_error = 0;
}
switch (c) {
case 'a':
if (getopt_error) {
usage("missing argument");
} else {
arg_option = optarg;
}
break;
case 'b':
bool_option = 1;
break;
default:
snprintf(option_error, sizeof(option_error),
"no such option as -%c", c);
usage(option_error);
}
}
#endif
if (optind >= argc) {
usage("no file argument");
}
for (i = optind ; i < argc; i++) {
process(argv[i]);
}
return 0;
}
int
trim_num(char *s)
{
char *t;
int n;
for (t = s; *t; t++) {
}
for (t--; t >= s; t--) {
if (!isdigit(*t)) {
t++;
if (*t) {
n = atoi(t);
*t = 0;
} else {
n = -1;
}
return n;
}
}
return -1;
}
void
process(char *filename)
{
char *s;
int index;
partition_map_header *map;
int valid_file;
partition_map * entry;
s = strdup(filename);
index = trim_num(s);
if (index < 0) {
fatal(-1, "%s does not end in a number", filename);
}
map = open_partition_map(s, &valid_file, 0);
if (!valid_file) {
fatal(-1, "%s does not have a partition map", s);
return;
}
if (map->writable == 0) {
fatal(-1, "The map is not writable");
return;
}
entry = find_entry_by_disk_address(index, map);
if (entry == NULL) {
fatal(-1, "No such partition");
} else if (strcmp(entry->data->dpme_type, kHFSType) != 0) {
fatal(-1, "Can't convert a partition with type %s",
entry->data->dpme_type);
} else {
strncpy(entry->data->dpme_type, kUnixType, DPISTRLEN);
write_partition_map(map);
}
}