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#include "cmdline.h" 44 45 46// 47// Defines 48// 49 50 51// 52// Types 53// 54 55 56// 57// Global Constants 58// 59 60 61// 62// Global Variables 63// 64char *program_name; 65#ifdef NeXT 66extern int errno; 67extern int sys_nerr; 68extern const char * const sys_errlist[]; 69#endif 70 71 72// 73// Forward declarations 74// 75 76 77// 78// Routines 79// 80void 81init_program_name(char **argv) 82{ 83#if defined(__linux__) || defined(__unix__) 84 if ((program_name = strrchr(argv[0], '/')) != (char *)NULL) { 85 program_name++; 86 } else { 87 program_name = argv[0]; 88 } 89#else 90 program_name = "pdisk"; 91#endif 92} 93 94 95void 96do_help() 97{ 98 fprintf(stderr, "usage:"); 99 fprintf(stderr, "\t%s [-h|--help]\n", program_name); 100 fprintf(stderr, "\t%s [-i|--interactive]\n", program_name); 101#if defined(__linux__) || defined(__APPLE__) 102 fprintf(stderr, "\t%s [-l|--list] [name] [...]\n", program_name); 103#else 104 fprintf(stderr, "\t%s [-l|--list] name [...]\n", program_name); 105#endif 106 fprintf(stderr, "\t%s [-r|--readonly] name [...]\n", program_name); 107 fprintf(stderr, "\t%s [-v|--version]\n", program_name); 108 fprintf(stderr, "\t%s name [...]\n", program_name); 109 fprintf(stderr, "\t%s name command\n", program_name); 110/* 111 {"debug", no_argument, 0, 'd'}, 112 {"abbr", no_argument, 0, 'a'}, 113 {"fs", no_argument, 0, 'f'}, 114 {"logical", no_argument, 0, kLogicalOption}, 115 {"compute_size", no_argument, 0, 'c'}, 116*/ 117 fprintf(stderr, "\n"); 118 do_command_help(); 119} 120 121 122void 123usage(const char *kind) 124{ 125 error(-1, "bad usage - %s\n", kind); 126 hflag = 1; 127} 128 129 130// 131// Print a message on standard error and exit with value. 132// Values in the range of system error numbers will add 133// the perror(3) message. 134// 135void 136fatal(int value, const char *fmt, ...) 137{ 138 va_list ap; 139 140 fprintf(stderr, "%s: ", program_name); 141 va_start(ap, fmt); 142 vfprintf(stderr, fmt, ap); 143 va_end(ap); 144 145#if defined(__linux__) || defined(NeXT) || defined(__unix__) 146 if (value > 0 && value < sys_nerr) { 147 fprintf(stderr, " (%s)\n", sys_errlist[value]); 148 } else { 149 fprintf(stderr, "\n"); 150 } 151#else 152 fprintf(stderr, "\n"); 153 printf("Processing stopped: Choose 'Quit' from the file menu to quit.\n\n"); 154#endif 155 exit(value); 156} 157 158 159// 160// Print a message on standard error. 161// Values in the range of system error numbers will add 162// the perror(3) message. 163// 164void 165error(int value, const char *fmt, ...) 166{ 167 va_list ap; 168 169 fprintf(stderr, "%s: ", program_name); 170 va_start(ap, fmt); 171 vfprintf(stderr, fmt, ap); 172 va_end(ap); 173 174#if defined(__linux__) || defined(NeXT) || defined(__unix__) 175 if (value > 0 && value < sys_nerr) { 176 fprintf(stderr, " (%s)\n", sys_errlist[value]); 177 } else { 178 fprintf(stderr, "\n"); 179 } 180#else 181 fprintf(stderr, "\n"); 182#endif 183} 184