1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include "libuutil_common.h" 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #include <libintl.h> 32*0Sstevel@tonic-gate #include <limits.h> 33*0Sstevel@tonic-gate #include <string.h> 34*0Sstevel@tonic-gate #include <stdlib.h> 35*0Sstevel@tonic-gate #include <stdarg.h> 36*0Sstevel@tonic-gate #include <stdio.h> 37*0Sstevel@tonic-gate #include <errno.h> 38*0Sstevel@tonic-gate #include <wchar.h> 39*0Sstevel@tonic-gate #include <unistd.h> 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate static const char PNAME_FMT[] = "%s: "; 42*0Sstevel@tonic-gate static const char ERRNO_FMT[] = ": %s\n"; 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate static const char *pname; 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate int uu_exit_ok_value = EXIT_SUCCESS; 47*0Sstevel@tonic-gate int uu_exit_fatal_value = EXIT_FAILURE; 48*0Sstevel@tonic-gate int uu_exit_usage_value = 2; 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate int * 51*0Sstevel@tonic-gate uu_exit_ok(void) 52*0Sstevel@tonic-gate { 53*0Sstevel@tonic-gate return (&uu_exit_ok_value); 54*0Sstevel@tonic-gate } 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate int * 57*0Sstevel@tonic-gate uu_exit_fatal(void) 58*0Sstevel@tonic-gate { 59*0Sstevel@tonic-gate return (&uu_exit_fatal_value); 60*0Sstevel@tonic-gate } 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate int * 63*0Sstevel@tonic-gate uu_exit_usage(void) 64*0Sstevel@tonic-gate { 65*0Sstevel@tonic-gate return (&uu_exit_usage_value); 66*0Sstevel@tonic-gate } 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate void 69*0Sstevel@tonic-gate uu_alt_exit(int profile) 70*0Sstevel@tonic-gate { 71*0Sstevel@tonic-gate switch (profile) { 72*0Sstevel@tonic-gate case UU_PROFILE_DEFAULT: 73*0Sstevel@tonic-gate uu_exit_ok_value = EXIT_SUCCESS; 74*0Sstevel@tonic-gate uu_exit_fatal_value = EXIT_FAILURE; 75*0Sstevel@tonic-gate uu_exit_usage_value = 2; 76*0Sstevel@tonic-gate break; 77*0Sstevel@tonic-gate case UU_PROFILE_LAUNCHER: 78*0Sstevel@tonic-gate uu_exit_ok_value = EXIT_SUCCESS; 79*0Sstevel@tonic-gate uu_exit_fatal_value = 124; 80*0Sstevel@tonic-gate uu_exit_usage_value = 125; 81*0Sstevel@tonic-gate break; 82*0Sstevel@tonic-gate } 83*0Sstevel@tonic-gate } 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate static void 86*0Sstevel@tonic-gate uu_warn_internal(int err, const char *format, va_list alist) 87*0Sstevel@tonic-gate { 88*0Sstevel@tonic-gate if (pname != NULL) 89*0Sstevel@tonic-gate (void) fprintf(stderr, PNAME_FMT, pname); 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate (void) vfprintf(stderr, format, alist); 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate if (strrchr(format, '\n') == NULL) 94*0Sstevel@tonic-gate (void) fprintf(stderr, ERRNO_FMT, strerror(err)); 95*0Sstevel@tonic-gate } 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate void 98*0Sstevel@tonic-gate uu_vwarn(const char *format, va_list alist) 99*0Sstevel@tonic-gate { 100*0Sstevel@tonic-gate uu_warn_internal(errno, format, alist); 101*0Sstevel@tonic-gate } 102*0Sstevel@tonic-gate 103*0Sstevel@tonic-gate /*PRINTFLIKE1*/ 104*0Sstevel@tonic-gate void 105*0Sstevel@tonic-gate uu_warn(const char *format, ...) 106*0Sstevel@tonic-gate { 107*0Sstevel@tonic-gate va_list alist; 108*0Sstevel@tonic-gate va_start(alist, format); 109*0Sstevel@tonic-gate uu_warn_internal(errno, format, alist); 110*0Sstevel@tonic-gate va_end(alist); 111*0Sstevel@tonic-gate } 112*0Sstevel@tonic-gate 113*0Sstevel@tonic-gate static void 114*0Sstevel@tonic-gate uu_die_internal(int status, const char *format, va_list alist) 115*0Sstevel@tonic-gate { 116*0Sstevel@tonic-gate uu_warn_internal(errno, format, alist); 117*0Sstevel@tonic-gate #ifdef DEBUG 118*0Sstevel@tonic-gate { 119*0Sstevel@tonic-gate char *cp; 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate if (!issetugid()) { 122*0Sstevel@tonic-gate cp = getenv("UU_DIE_ABORTS"); 123*0Sstevel@tonic-gate if (cp != NULL && *cp != '\0') 124*0Sstevel@tonic-gate abort(); 125*0Sstevel@tonic-gate } 126*0Sstevel@tonic-gate } 127*0Sstevel@tonic-gate #endif 128*0Sstevel@tonic-gate exit(status); 129*0Sstevel@tonic-gate } 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate void 132*0Sstevel@tonic-gate uu_vdie(const char *format, va_list alist) 133*0Sstevel@tonic-gate { 134*0Sstevel@tonic-gate uu_die_internal(UU_EXIT_FATAL, format, alist); 135*0Sstevel@tonic-gate } 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate /*PRINTFLIKE1*/ 138*0Sstevel@tonic-gate void 139*0Sstevel@tonic-gate uu_die(const char *format, ...) 140*0Sstevel@tonic-gate { 141*0Sstevel@tonic-gate va_list alist; 142*0Sstevel@tonic-gate va_start(alist, format); 143*0Sstevel@tonic-gate uu_die_internal(UU_EXIT_FATAL, format, alist); 144*0Sstevel@tonic-gate va_end(alist); 145*0Sstevel@tonic-gate } 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate void 148*0Sstevel@tonic-gate uu_vxdie(int status, const char *format, va_list alist) 149*0Sstevel@tonic-gate { 150*0Sstevel@tonic-gate uu_die_internal(status, format, alist); 151*0Sstevel@tonic-gate } 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate /*PRINTFLIKE2*/ 154*0Sstevel@tonic-gate void 155*0Sstevel@tonic-gate uu_xdie(int status, const char *format, ...) 156*0Sstevel@tonic-gate { 157*0Sstevel@tonic-gate va_list alist; 158*0Sstevel@tonic-gate va_start(alist, format); 159*0Sstevel@tonic-gate uu_die_internal(status, format, alist); 160*0Sstevel@tonic-gate va_end(alist); 161*0Sstevel@tonic-gate } 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate const char * 164*0Sstevel@tonic-gate uu_setpname(char *arg0) 165*0Sstevel@tonic-gate { 166*0Sstevel@tonic-gate /* 167*0Sstevel@tonic-gate * Having a NULL argv[0], while uncommon, is possible. It 168*0Sstevel@tonic-gate * makes more sense to handle this event in uu_setpname rather 169*0Sstevel@tonic-gate * than in each of its consumers. 170*0Sstevel@tonic-gate */ 171*0Sstevel@tonic-gate if (arg0 == NULL) { 172*0Sstevel@tonic-gate pname = getexecname(); 173*0Sstevel@tonic-gate if (pname == NULL) 174*0Sstevel@tonic-gate pname = "unknown_command"; 175*0Sstevel@tonic-gate return (pname); 176*0Sstevel@tonic-gate } 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate /* 179*0Sstevel@tonic-gate * Guard against '/' at end of command invocation. 180*0Sstevel@tonic-gate */ 181*0Sstevel@tonic-gate for (;;) { 182*0Sstevel@tonic-gate char *p = strrchr(arg0, '/'); 183*0Sstevel@tonic-gate if (p == NULL) { 184*0Sstevel@tonic-gate pname = arg0; 185*0Sstevel@tonic-gate break; 186*0Sstevel@tonic-gate } else { 187*0Sstevel@tonic-gate if (*(p + 1) == '\0') { 188*0Sstevel@tonic-gate *p = '\0'; 189*0Sstevel@tonic-gate continue; 190*0Sstevel@tonic-gate } 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gate pname = p + 1; 193*0Sstevel@tonic-gate break; 194*0Sstevel@tonic-gate } 195*0Sstevel@tonic-gate } 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate return (pname); 198*0Sstevel@tonic-gate } 199*0Sstevel@tonic-gate 200*0Sstevel@tonic-gate const char * 201*0Sstevel@tonic-gate uu_getpname(void) 202*0Sstevel@tonic-gate { 203*0Sstevel@tonic-gate return (pname); 204*0Sstevel@tonic-gate } 205