Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame^] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "cgpt.h" |
| 6 | |
| 7 | #include <getopt.h> |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 8 | #include <string.h> |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 9 | |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame^] | 10 | #include "cgpt_params.h" |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 11 | |
| 12 | static void Usage(void) |
| 13 | { |
| 14 | printf("\nUsage: %s create [OPTIONS] DRIVE\n\n" |
| 15 | "Create or reset an empty GPT.\n\n" |
| 16 | "Options:\n" |
| 17 | " -z Zero the sectors of the GPT table and entries\n" |
| 18 | "\n", progname); |
| 19 | } |
| 20 | |
| 21 | int cmd_create(int argc, char *argv[]) { |
| 22 | struct drive drive; |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame^] | 23 | |
| 24 | CgptCreateParams params; |
| 25 | memset(¶ms, 0, sizeof(params)); |
Bill Richardson | c4e92af | 2010-10-12 07:33:15 -0700 | [diff] [blame] | 26 | |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 27 | int c; |
| 28 | int errorcnt = 0; |
| 29 | |
| 30 | opterr = 0; // quiet, you |
| 31 | while ((c=getopt(argc, argv, ":hz")) != -1) |
| 32 | { |
| 33 | switch (c) |
| 34 | { |
| 35 | case 'z': |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame^] | 36 | params.zap = 1; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 37 | break; |
| 38 | |
| 39 | case 'h': |
| 40 | Usage(); |
| 41 | return CGPT_OK; |
| 42 | case '?': |
| 43 | Error("unrecognized option: -%c\n", optopt); |
| 44 | errorcnt++; |
| 45 | break; |
| 46 | case ':': |
| 47 | Error("missing argument to -%c\n", optopt); |
| 48 | errorcnt++; |
| 49 | break; |
| 50 | default: |
| 51 | errorcnt++; |
| 52 | break; |
| 53 | } |
| 54 | } |
| 55 | if (errorcnt) |
| 56 | { |
| 57 | Usage(); |
| 58 | return CGPT_FAILED; |
| 59 | } |
| 60 | |
| 61 | if (optind >= argc) { |
| 62 | Usage(); |
| 63 | return CGPT_FAILED; |
| 64 | } |
| 65 | |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame^] | 66 | params.driveName = argv[optind]; |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 67 | |
Jay Srinivasan | a058143 | 2012-01-26 21:50:05 -0800 | [diff] [blame^] | 68 | return cgpt_create(¶ms); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 69 | } |