| OLD | NEW |
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 * | 4 * |
| 5 * Utility for ChromeOS-specific GPT partitions, Please see corresponding .c | 5 * Utility for ChromeOS-specific GPT partitions, Please see corresponding .c |
| 6 * files for more details. | 6 * files for more details. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include "cgpt.h" | 9 #include "cgpt.h" |
| 10 | 10 |
| 11 #include <stdio.h> | 11 #include <stdio.h> |
| 12 #include <string.h> | 12 #include <string.h> |
| 13 #include <unistd.h> | 13 #include <unistd.h> |
| 14 | 14 |
| 15 | 15 |
| 16 const char* progname; | 16 const char* progname; |
| 17 const char* command; | 17 const char* command; |
| 18 | 18 |
| 19 struct { | 19 struct { |
| 20 const char *name; | 20 const char *name; |
| 21 int (*fp)(int argc, char *argv[]); | 21 int (*fp)(int argc, char *argv[]); |
| 22 const char *comment; | 22 const char *comment; |
| 23 } cmds[] = { | 23 } cmds[] = { |
| 24 {"create", cmd_create, "Create or reset GPT headers and tables"}, | 24 {"create", cmd_create, "Create or reset GPT headers and tables"}, |
| 25 {"add", cmd_add, "Add, edit or remove a partition entry"}, | 25 {"add", cmd_add, "Add, edit or remove a partition entry"}, |
| 26 {"show", cmd_show, "Show partition table and entries"}, | 26 {"show", cmd_show, "Show partition table and entries"}, |
| 27 {"repair", cmd_repair, "Repair damaged GPT headers and tables"}, | 27 {"repair", cmd_repair, "Repair damaged GPT headers and tables"}, |
| 28 {"boot", cmd_boot, "Edit the PMBR sector for legacy BIOSes"}, | 28 {"boot", cmd_boot, "Edit the PMBR sector for legacy BIOSes"}, |
| 29 {"find", cmd_find, "Locate a partition by its GUID"}, | 29 {"find", cmd_find, "Locate a partition by its GUID"}, |
| 30 {"prioritize", cmd_prioritize, |
| 31 "Reorder the priority of all kernel partitions"}, |
| 30 }; | 32 }; |
| 31 | 33 |
| 32 | |
| 33 void Usage(void) { | 34 void Usage(void) { |
| 34 int i; | 35 int i; |
| 35 | 36 |
| 36 printf("Usage: %s COMMAND [OPTIONS] DRIVE\n\n" | 37 printf("\nUsage: %s COMMAND [OPTIONS] DRIVE\n\n" |
| 37 "Supported COMMANDs:\n\n", | 38 "Supported COMMANDs:\n\n", |
| 38 progname); | 39 progname); |
| 39 | 40 |
| 40 for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) { | 41 for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) { |
| 41 printf(" %-10s %s\n", cmds[i].name, cmds[i].comment); | 42 printf(" %-15s %s\n", cmds[i].name, cmds[i].comment); |
| 42 } | 43 } |
| 43 printf("\nFor more detailed usage, use %s COMMAND -h\n\n", progname); | 44 printf("\nFor more detailed usage, use %s COMMAND -h\n\n", progname); |
| 44 } | 45 } |
| 45 | 46 |
| 46 | 47 |
| 47 | 48 |
| 48 int main(int argc, char *argv[]) { | 49 int main(int argc, char *argv[]) { |
| 49 int i; | 50 int i; |
| 51 int match_count = 0; |
| 52 int match_index = 0; |
| 50 | 53 |
| 51 progname = strrchr(argv[0], '/'); | 54 progname = strrchr(argv[0], '/'); |
| 52 if (progname) | 55 if (progname) |
| 53 progname++; | 56 progname++; |
| 54 else | 57 else |
| 55 progname = argv[0]; | 58 progname = argv[0]; |
| 56 | 59 |
| 57 if (argc < 2) { | 60 if (argc < 2) { |
| 58 Usage(); | 61 Usage(); |
| 59 return CGPT_FAILED; | 62 return CGPT_FAILED; |
| 60 } | 63 } |
| 61 | 64 |
| 62 // increment optind now, so that getopt skips argv[0] in command function | 65 // increment optind now, so that getopt skips argv[0] in command function |
| 63 command = argv[optind++]; | 66 command = argv[optind++]; |
| 64 | 67 |
| 65 // Find the command to invoke. | 68 // Find the command to invoke. |
| 66 for (i = 0; command && i < sizeof(cmds)/sizeof(cmds[0]); ++i) { | 69 for (i = 0; command && i < sizeof(cmds)/sizeof(cmds[0]); ++i) { |
| 70 // exact match? |
| 67 if (0 == strcmp(cmds[i].name, command)) { | 71 if (0 == strcmp(cmds[i].name, command)) { |
| 68 return cmds[i].fp(argc, argv); | 72 match_index = i; |
| 73 match_count = 1; |
| 74 break; |
| 75 } |
| 76 // unique match? |
| 77 else if (0 == strncmp(cmds[i].name, command, strlen(command))) { |
| 78 match_index = i; |
| 79 match_count++; |
| 69 } | 80 } |
| 70 } | 81 } |
| 71 | 82 |
| 72 // Couldn't find the command. | 83 if (match_count == 1) |
| 84 return cmds[match_index].fp(argc, argv); |
| 85 |
| 86 // Couldn't find a single matching command. |
| 73 Usage(); | 87 Usage(); |
| 74 | 88 |
| 75 return CGPT_FAILED; | 89 return CGPT_FAILED; |
| 76 } | 90 } |
| OLD | NEW |