19428323dSchristos /*
29428323dSchristos * cvt_pt.c
39428323dSchristos *
4*48a628aeSchristos * Covert partition type. $Revision: 1.2 $
59428323dSchristos *
69428323dSchristos * Copyright (c) 1999, Eryk Vershen
79428323dSchristos *
89428323dSchristos * History:
9*48a628aeSchristos * Log: cvt_pt.c,v
109428323dSchristos * Revision 1.2 2000/05/16 13:56:11 eryk
119428323dSchristos * Minor fixes
129428323dSchristos *
139428323dSchristos * Revision 1.1 2000/02/13 22:04:08 eryk
149428323dSchristos * Initial revision
159428323dSchristos *
169428323dSchristos */
179428323dSchristos
189428323dSchristos #include <stdio.h>
199428323dSchristos #include <stdlib.h>
209428323dSchristos #include <string.h>
219428323dSchristos #include <stdarg.h>
229428323dSchristos #include <errno.h>
239428323dSchristos #include <ctype.h>
249428323dSchristos
259428323dSchristos #include "partition_map.h"
269428323dSchristos #include "errors.h"
279428323dSchristos
289428323dSchristos
299428323dSchristos /*
309428323dSchristos * Defines / Constants
319428323dSchristos */
329428323dSchristos #define CFLAG_DEFAULT 0
339428323dSchristos #define DFLAG_DEFAULT 0
349428323dSchristos #define HFLAG_DEFAULT 0
359428323dSchristos #define INTERACT_DEFAULT 0
369428323dSchristos #define RFLAG_DEFAULT 0
379428323dSchristos
389428323dSchristos
399428323dSchristos /*
409428323dSchristos * Structs / Types
419428323dSchristos */
429428323dSchristos
439428323dSchristos
449428323dSchristos /*
459428323dSchristos * Global variables
469428323dSchristos */
479428323dSchristos int hflag = HFLAG_DEFAULT; /* show help */
489428323dSchristos int dflag = DFLAG_DEFAULT; /* turn on debugging commands and printout */
499428323dSchristos int rflag = RFLAG_DEFAULT; /* open device read Only */
509428323dSchristos int interactive = INTERACT_DEFAULT;
519428323dSchristos int cflag = CFLAG_DEFAULT; /* compute device size */
529428323dSchristos
539428323dSchristos
549428323dSchristos /*
559428323dSchristos * Forward declarations
569428323dSchristos */
579428323dSchristos void process(char *filename);
589428323dSchristos
599428323dSchristos
609428323dSchristos /*
619428323dSchristos * Start here ...
629428323dSchristos */
639428323dSchristos int
main(int argc,char ** argv)649428323dSchristos main(int argc, char **argv)
659428323dSchristos {
669428323dSchristos register int i;
679428323dSchristos #ifdef notdef
689428323dSchristos register int c;
699428323dSchristos extern char *optarg; /* pointer to argument */
709428323dSchristos extern int optind; /* next argv index */
719428323dSchristos extern int opterr; /* who does error messages */
729428323dSchristos extern int optopt; /* char that caused the error */
739428323dSchristos int getopt_error; /* getopt choked */
749428323dSchristos char option_error[100]; /* buffer for error message */
759428323dSchristos char *arg_option = 0;
769428323dSchristos int bool_option = 0;
779428323dSchristos #else
789428323dSchristos int optind = 1;
799428323dSchristos #endif
809428323dSchristos
819428323dSchristos init_program_name(argv);
829428323dSchristos
839428323dSchristos #ifdef notdef
849428323dSchristos opterr = 0; /* tell getopt to be quiet */
859428323dSchristos
869428323dSchristos /*
879428323dSchristos * Changes to getopt's last argument should
889428323dSchristos * be reflected in the string printed by the
899428323dSchristos * usage() function.
909428323dSchristos */
919428323dSchristos while ((c = getopt(argc, argv, "a:b")) != EOF) {
929428323dSchristos if (c == '?') {
939428323dSchristos getopt_error = 1;
949428323dSchristos c = optopt;
959428323dSchristos } else {
969428323dSchristos getopt_error = 0;
979428323dSchristos }
989428323dSchristos
999428323dSchristos switch (c) {
1009428323dSchristos case 'a':
1019428323dSchristos if (getopt_error) {
1029428323dSchristos usage("missing argument");
1039428323dSchristos } else {
1049428323dSchristos arg_option = optarg;
1059428323dSchristos }
1069428323dSchristos break;
1079428323dSchristos case 'b':
1089428323dSchristos bool_option = 1;
1099428323dSchristos break;
1109428323dSchristos default:
111*48a628aeSchristos snprintf(option_error, sizeof(option_error),
112*48a628aeSchristos "no such option as -%c", c);
1139428323dSchristos usage(option_error);
1149428323dSchristos }
1159428323dSchristos }
1169428323dSchristos #endif
1179428323dSchristos
1189428323dSchristos if (optind >= argc) {
1199428323dSchristos usage("no file argument");
1209428323dSchristos }
1219428323dSchristos for (i = optind ; i < argc; i++) {
1229428323dSchristos process(argv[i]);
1239428323dSchristos }
1249428323dSchristos return 0;
1259428323dSchristos }
1269428323dSchristos
1279428323dSchristos
1289428323dSchristos int
trim_num(char * s)1299428323dSchristos trim_num(char *s)
1309428323dSchristos {
1319428323dSchristos char *t;
1329428323dSchristos int n;
1339428323dSchristos
1349428323dSchristos for (t = s; *t; t++) {
1359428323dSchristos }
1369428323dSchristos
1379428323dSchristos for (t--; t >= s; t--) {
138*48a628aeSchristos if (!isdigit((uint8_t)*t)) {
1399428323dSchristos t++;
1409428323dSchristos if (*t) {
1419428323dSchristos n = atoi(t);
1429428323dSchristos *t = 0;
1439428323dSchristos } else {
1449428323dSchristos n = -1;
1459428323dSchristos }
1469428323dSchristos return n;
1479428323dSchristos }
1489428323dSchristos }
1499428323dSchristos
1509428323dSchristos return -1;
1519428323dSchristos }
1529428323dSchristos
1539428323dSchristos /*
1549428323dSchristos * The operation to apply to each file ...
1559428323dSchristos */
1569428323dSchristos void
process(char * filename)1579428323dSchristos process(char *filename)
1589428323dSchristos {
1599428323dSchristos char *s;
1609428323dSchristos int index;
1619428323dSchristos partition_map_header *map;
1629428323dSchristos int valid_file;
1639428323dSchristos partition_map * entry;
1649428323dSchristos
1659428323dSchristos //printf("Processing %s\n", filename);
1669428323dSchristos
1679428323dSchristos // 1) strip off number from end of filename
1689428323dSchristos s = strdup(filename);
1699428323dSchristos index = trim_num(s);
1709428323dSchristos
1719428323dSchristos if (index < 0) {
1729428323dSchristos fatal(-1, "%s does not end in a number", filename);
1739428323dSchristos }
1749428323dSchristos
1759428323dSchristos // 2) open prefix of filename as partition map
1769428323dSchristos map = open_partition_map(s, &valid_file, 0);
1779428323dSchristos if (!valid_file) {
1789428323dSchristos fatal(-1, "%s does not have a partition map", s);
1799428323dSchristos return;
1809428323dSchristos }
1819428323dSchristos
1829428323dSchristos // 3) verify the type for the partition;
1839428323dSchristos
184*48a628aeSchristos if (map->writable == 0) {
185*48a628aeSchristos fatal(-1, "The map is not writable");
1869428323dSchristos return;
1879428323dSchristos }
1889428323dSchristos
1899428323dSchristos // 4) find partition and change it
1909428323dSchristos entry = find_entry_by_disk_address(index, map);
1919428323dSchristos if (entry == NULL) {
1929428323dSchristos fatal(-1, "No such partition");
1939428323dSchristos } else if (strcmp(entry->data->dpme_type, kHFSType) != 0) {
1949428323dSchristos fatal(-1, "Can't convert a partition with type %s",
1959428323dSchristos entry->data->dpme_type);
1969428323dSchristos } else {
1979428323dSchristos // 4a) modify the type
1989428323dSchristos strncpy(entry->data->dpme_type, kUnixType, DPISTRLEN);
1999428323dSchristos
2009428323dSchristos // 5) and write back.
2019428323dSchristos write_partition_map(map);
2029428323dSchristos }
2039428323dSchristos }
204