1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <stdarg.h> 32 #include <string.h> 33 #include <libgen.h> 34 #include <libintl.h> 35 #include <errno.h> 36 37 #include "ipsec_util.h" 38 39 /* Function exit/warning functions and global variables. */ 40 41 static const char *progname; 42 43 /* 44 * warncore() is the workhorse of these functions. Everything else has 45 * a warncore() component in it. 46 */ 47 static void 48 warncore(const char *fmt, va_list args) 49 { 50 if (progname == NULL) { 51 progname = strrchr(getexecname(), '/'); 52 if (progname == NULL) 53 progname = getexecname(); 54 else 55 progname++; 56 } 57 58 (void) fputs(progname, stderr); 59 60 if (fmt != NULL) { 61 (void) fputc(':', stderr); 62 (void) fputc(' ', stderr); 63 (void) vfprintf(stderr, fmt, args); 64 } 65 } 66 67 /* Finish a warning with a newline and a flush of stderr. */ 68 static void 69 warnfinish(void) 70 { 71 (void) fputc('\n', stderr); 72 (void) fflush(stderr); 73 } 74 75 void 76 vwarnx(const char *fmt, va_list args) 77 { 78 warncore(fmt, args); 79 warnfinish(); 80 } 81 82 void 83 vwarn(const char *fmt, va_list args) 84 { 85 int tmperr = errno; /* Capture errno now. */ 86 87 warncore(fmt, args); 88 (void) fputc(':', stderr); 89 (void) fputc(' ', stderr); 90 (void) fputs(strerror(tmperr), stderr); 91 warnfinish(); 92 } 93 94 /* PRINTFLIKE1 */ 95 void 96 warnx(const char *fmt, ...) 97 { 98 va_list args; 99 100 va_start(args, fmt); 101 vwarnx(fmt, args); 102 va_end(args); 103 } 104 105 /* PRINTFLIKE1 */ 106 void 107 warn(const char *fmt, ...) 108 { 109 va_list args; 110 111 va_start(args, fmt); 112 vwarn(fmt, args); 113 va_end(args); 114 } 115 116 /* PRINTFLIKE2 */ 117 void 118 err(int status, const char *fmt, ...) 119 { 120 va_list args; 121 122 va_start(args, fmt); 123 vwarn(fmt, args); 124 va_end(args); 125 exit(status); 126 } 127 128 void 129 verr(int status, const char *fmt, va_list args) 130 { 131 vwarn(fmt, args); 132 exit(status); 133 } 134 135 /* PRINTFLIKE2 */ 136 void 137 errx(int status, const char *fmt, ...) 138 { 139 va_list args; 140 141 va_start(args, fmt); 142 vwarnx(fmt, args); 143 va_end(args); 144 exit(status); 145 } 146 147 void 148 verrx(int status, const char *fmt, va_list args) 149 { 150 vwarnx(fmt, args); 151 exit(status); 152 } 153