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 "libuutil_common.h" 30 31 #include <libintl.h> 32 #include <limits.h> 33 #include <string.h> 34 #include <stdlib.h> 35 #include <stdarg.h> 36 #include <stdio.h> 37 #include <errno.h> 38 #include <wchar.h> 39 #include <unistd.h> 40 41 static const char PNAME_FMT[] = "%s: "; 42 static const char ERRNO_FMT[] = ": %s\n"; 43 44 static const char *pname; 45 46 int uu_exit_ok_value = EXIT_SUCCESS; 47 int uu_exit_fatal_value = EXIT_FAILURE; 48 int uu_exit_usage_value = 2; 49 50 int * 51 uu_exit_ok(void) 52 { 53 return (&uu_exit_ok_value); 54 } 55 56 int * 57 uu_exit_fatal(void) 58 { 59 return (&uu_exit_fatal_value); 60 } 61 62 int * 63 uu_exit_usage(void) 64 { 65 return (&uu_exit_usage_value); 66 } 67 68 void 69 uu_alt_exit(int profile) 70 { 71 switch (profile) { 72 case UU_PROFILE_DEFAULT: 73 uu_exit_ok_value = EXIT_SUCCESS; 74 uu_exit_fatal_value = EXIT_FAILURE; 75 uu_exit_usage_value = 2; 76 break; 77 case UU_PROFILE_LAUNCHER: 78 uu_exit_ok_value = EXIT_SUCCESS; 79 uu_exit_fatal_value = 124; 80 uu_exit_usage_value = 125; 81 break; 82 } 83 } 84 85 static void 86 uu_warn_internal(int err, const char *format, va_list alist) 87 { 88 if (pname != NULL) 89 (void) fprintf(stderr, PNAME_FMT, pname); 90 91 (void) vfprintf(stderr, format, alist); 92 93 if (strrchr(format, '\n') == NULL) 94 (void) fprintf(stderr, ERRNO_FMT, strerror(err)); 95 } 96 97 void 98 uu_vwarn(const char *format, va_list alist) 99 { 100 uu_warn_internal(errno, format, alist); 101 } 102 103 /*PRINTFLIKE1*/ 104 void 105 uu_warn(const char *format, ...) 106 { 107 va_list alist; 108 va_start(alist, format); 109 uu_warn_internal(errno, format, alist); 110 va_end(alist); 111 } 112 113 static void 114 uu_die_internal(int status, const char *format, va_list alist) 115 { 116 uu_warn_internal(errno, format, alist); 117 #ifdef DEBUG 118 { 119 char *cp; 120 121 if (!issetugid()) { 122 cp = getenv("UU_DIE_ABORTS"); 123 if (cp != NULL && *cp != '\0') 124 abort(); 125 } 126 } 127 #endif 128 exit(status); 129 } 130 131 void 132 uu_vdie(const char *format, va_list alist) 133 { 134 uu_die_internal(UU_EXIT_FATAL, format, alist); 135 } 136 137 /*PRINTFLIKE1*/ 138 void 139 uu_die(const char *format, ...) 140 { 141 va_list alist; 142 va_start(alist, format); 143 uu_die_internal(UU_EXIT_FATAL, format, alist); 144 va_end(alist); 145 } 146 147 void 148 uu_vxdie(int status, const char *format, va_list alist) 149 { 150 uu_die_internal(status, format, alist); 151 } 152 153 /*PRINTFLIKE2*/ 154 void 155 uu_xdie(int status, const char *format, ...) 156 { 157 va_list alist; 158 va_start(alist, format); 159 uu_die_internal(status, format, alist); 160 va_end(alist); 161 } 162 163 const char * 164 uu_setpname(char *arg0) 165 { 166 /* 167 * Having a NULL argv[0], while uncommon, is possible. It 168 * makes more sense to handle this event in uu_setpname rather 169 * than in each of its consumers. 170 */ 171 if (arg0 == NULL) { 172 pname = getexecname(); 173 if (pname == NULL) 174 pname = "unknown_command"; 175 return (pname); 176 } 177 178 /* 179 * Guard against '/' at end of command invocation. 180 */ 181 for (;;) { 182 char *p = strrchr(arg0, '/'); 183 if (p == NULL) { 184 pname = arg0; 185 break; 186 } else { 187 if (*(p + 1) == '\0') { 188 *p = '\0'; 189 continue; 190 } 191 192 pname = p + 1; 193 break; 194 } 195 } 196 197 return (pname); 198 } 199 200 const char * 201 uu_getpname(void) 202 { 203 return (pname); 204 } 205