1 /* 2 * Copyright 2008 Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Marco Minutoli, mminutoli@gmail.com 7 */ 8 9 #include <stdio.h> 10 #include <iostream> 11 #include <stdlib.h> 12 #include <String.h> 13 #include <getopt.h> 14 #include "FsCreator.h" 15 16 extern "C" const char* __progname; 17 static const char* kProgramName = __progname; 18 19 static const char* kUsage = 20 "Usage: %s -t <fs> <options> <device> <volume name>\n" 21 "\n" 22 "Options:\n" 23 " -t, --type <fs> - set type of filesystem to create\n\n" 24 " -h, --help - print this help text\n" 25 " -o, --options <opt> - set fs specific options\n" 26 " -v, --verbose - set verbose output\n" 27 ; 28 29 30 /* 31 * Print program help on the right stream 32 */ 33 inline void 34 print_help(bool out) 35 { 36 fprintf(out ? stdout : stderr, kUsage, kProgramName, kProgramName); 37 } 38 39 40 /* 41 * Print program help and exit 42 */ 43 inline void 44 print_help_exit(bool out) 45 { 46 print_help(out); 47 exit(out ? 0 : 1); 48 } 49 50 51 int 52 main(int argc, char* const* argv) 53 { 54 const struct option longOptions[] = { 55 { "help", 0, NULL, 'h' }, 56 { "options", 0, NULL, 'o' }, 57 { "type", 1, NULL, 't' }, 58 { "verbose", 0, NULL, 'v' }, 59 { NULL, 0, NULL, 0 } 60 }; 61 const char *shortOptions = "t:o:hv"; 62 63 // parse argument list 64 int32 nextOption; 65 BString fsType; 66 BString fsOptions; 67 bool verbose = false; 68 69 nextOption = getopt_long(argc, argv, shortOptions, longOptions, NULL); 70 if (nextOption == 't') 71 fsType = optarg; 72 else 73 print_help_exit(nextOption == 'h' ? true : false); 74 75 do { 76 nextOption = getopt_long(argc, argv, shortOptions, longOptions, NULL); 77 78 switch (nextOption) { 79 case 't': // -t or --type again? 80 print_help_exit(false); 81 break; 82 case 'h': // -h or --help 83 print_help_exit(true); 84 break; 85 case 'v': // -v or --verbose 86 verbose = true; 87 break; 88 case 'o': // -o or --options 89 fsOptions << optarg; 90 break; 91 case '?': // invalid option 92 break; 93 case -1: // done with options 94 break; 95 default: // everything else 96 print_help(false); 97 abort(); 98 } 99 } while (nextOption != -1); 100 101 // the device name should be the first non-option element 102 // right before the VolumeName 103 if (optind != argc - 2) 104 print_help_exit(false); 105 const char* devPath = argv[optind]; 106 BString volName = argv[argc-1]; 107 108 FsCreator* creator = new FsCreator(devPath, fsType, volName, 109 fsOptions.String(), verbose); 110 if (creator == NULL) { 111 std::cerr << "Error: FsCreator can't be allocated\n"; 112 abort(); 113 } 114 115 return creator->Run(); 116 } 117