1 // 2 // errors.c - error & help routines 3 // 4 // Written by Eryk Vershen 5 // 6 7 /* 8 * Copyright 1996,1997,1998 by Apple Computer, Inc. 9 * All Rights Reserved 10 * 11 * Permission to use, copy, modify, and distribute this software and 12 * its documentation for any purpose and without fee is hereby granted, 13 * provided that the above copyright notice appears in all copies and 14 * that both the copyright notice and this permission notice appear in 15 * supporting documentation. 16 * 17 * APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE 18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 19 * FOR A PARTICULAR PURPOSE. 20 * 21 * IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR 22 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 23 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT, 24 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 25 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 26 */ 27 28 // for *printf() 29 #include <stdio.h> 30 #include <errno.h> 31 32 // for exit() 33 #ifndef __linux__ 34 #include <stdlib.h> 35 #endif 36 // for strrchr 37 #include <string.h> 38 39 // for va_start(), etc. 40 #include <stdarg.h> 41 42 #include "errors.h" 43 44 45 // 46 // Defines 47 // 48 49 50 // 51 // Types 52 // 53 54 55 // 56 // Global Constants 57 // 58 59 60 // 61 // Global Variables 62 // 63 char *program_name; 64 #ifdef NeXT 65 extern int errno; 66 extern int sys_nerr; 67 extern const char * const sys_errlist[]; 68 #endif 69 70 71 // 72 // Forward declarations 73 // 74 75 76 // 77 // Routines 78 // 79 void 80 init_program_name(char **argv) 81 { 82 #if defined(__linux__) || defined(__unix__) 83 if ((program_name = strrchr(argv[0], '/')) != (char *)NULL) { 84 program_name++; 85 } else { 86 program_name = argv[0]; 87 } 88 #else 89 program_name = "pdisk"; 90 #endif 91 } 92 93 94 void 95 do_help(void) 96 { 97 printf("\t%s [-h|--help]\n", program_name); 98 printf("\t%s [-v|--version]\n", program_name); 99 #ifdef __linux__ 100 printf("\t%s [-l|--list [name]] [...]\n", program_name); 101 #else 102 printf("\t%s [-l|--list] name [...]\n", program_name); 103 #endif 104 printf("\t%s [-r|--readonly] name ...\n", program_name); 105 printf("\t%s [-i|--interactive]\n", program_name); 106 printf("\t%s name [...]\n", program_name); 107 /* 108 {"debug", no_argument, 0, 'd'}, 109 {"abbr", no_argument, 0, 'a'}, 110 {"fs", no_argument, 0, 'f'}, 111 {"logical", no_argument, 0, kLogicalOption}, 112 {"compute_size", no_argument, 0, 'c'}, 113 */ 114 } 115 116 117 void 118 usage(const char *kind) 119 { 120 error(-1, "bad usage - %s\n", kind); 121 hflag = 1; 122 } 123 124 125 // 126 // Print a message on standard error and exit with value. 127 // Values in the range of system error numbers will add 128 // the perror(3) message. 129 // 130 void 131 fatal(int value, const char *fmt, ...) 132 { 133 va_list ap; 134 135 fprintf(stderr, "%s: ", program_name); 136 va_start(ap, fmt); 137 vfprintf(stderr, fmt, ap); 138 va_end(ap); 139 140 #if defined(__linux__) || defined(NeXT) || defined(__unix__) 141 if (value > 0 && value < sys_nerr) { 142 fprintf(stderr, " (%s)\n", sys_errlist[value]); 143 } else { 144 fprintf(stderr, "\n"); 145 } 146 #else 147 fprintf(stderr, "\n"); 148 printf("Processing stopped: Choose 'Quit' from the file menu to quit.\n\n"); 149 #endif 150 exit(value); 151 } 152 153 154 // 155 // Print a message on standard error. 156 // Values in the range of system error numbers will add 157 // the perror(3) message. 158 // 159 void 160 error(int value, const char *fmt, ...) 161 { 162 va_list ap; 163 164 fprintf(stderr, "%s: ", program_name); 165 va_start(ap, fmt); 166 vfprintf(stderr, fmt, ap); 167 va_end(ap); 168 169 #if defined(__linux__) || defined(NeXT) || defined(__unix__) 170 if (value > 0 && value < sys_nerr) { 171 fprintf(stderr, " (%s)\n", sys_errlist[value]); 172 } else { 173 fprintf(stderr, "\n"); 174 } 175 #else 176 fprintf(stderr, "\n"); 177 #endif 178 } 179