xref: /onnv-gate/usr/src/lib/libuutil/common/uu_pname.c (revision 471:fb6202c3da23)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*471Shg115875  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include "libuutil_common.h"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <libintl.h>
320Sstevel@tonic-gate #include <limits.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <stdarg.h>
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate #include <wchar.h>
390Sstevel@tonic-gate #include <unistd.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate static const char PNAME_FMT[] = "%s: ";
420Sstevel@tonic-gate static const char ERRNO_FMT[] = ": %s\n";
430Sstevel@tonic-gate 
440Sstevel@tonic-gate static const char *pname;
450Sstevel@tonic-gate 
46*471Shg115875 static void
47*471Shg115875 uu_die_internal(int status, const char *format, va_list alist) __NORETURN;
48*471Shg115875 
490Sstevel@tonic-gate int uu_exit_ok_value = EXIT_SUCCESS;
500Sstevel@tonic-gate int uu_exit_fatal_value = EXIT_FAILURE;
510Sstevel@tonic-gate int uu_exit_usage_value = 2;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate int *
uu_exit_ok(void)540Sstevel@tonic-gate uu_exit_ok(void)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate 	return (&uu_exit_ok_value);
570Sstevel@tonic-gate }
580Sstevel@tonic-gate 
590Sstevel@tonic-gate int *
uu_exit_fatal(void)600Sstevel@tonic-gate uu_exit_fatal(void)
610Sstevel@tonic-gate {
620Sstevel@tonic-gate 	return (&uu_exit_fatal_value);
630Sstevel@tonic-gate }
640Sstevel@tonic-gate 
650Sstevel@tonic-gate int *
uu_exit_usage(void)660Sstevel@tonic-gate uu_exit_usage(void)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	return (&uu_exit_usage_value);
690Sstevel@tonic-gate }
700Sstevel@tonic-gate 
710Sstevel@tonic-gate void
uu_alt_exit(int profile)720Sstevel@tonic-gate uu_alt_exit(int profile)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	switch (profile) {
750Sstevel@tonic-gate 	case UU_PROFILE_DEFAULT:
760Sstevel@tonic-gate 		uu_exit_ok_value = EXIT_SUCCESS;
770Sstevel@tonic-gate 		uu_exit_fatal_value = EXIT_FAILURE;
780Sstevel@tonic-gate 		uu_exit_usage_value = 2;
790Sstevel@tonic-gate 		break;
800Sstevel@tonic-gate 	case UU_PROFILE_LAUNCHER:
810Sstevel@tonic-gate 		uu_exit_ok_value = EXIT_SUCCESS;
820Sstevel@tonic-gate 		uu_exit_fatal_value = 124;
830Sstevel@tonic-gate 		uu_exit_usage_value = 125;
840Sstevel@tonic-gate 		break;
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static void
uu_warn_internal(int err,const char * format,va_list alist)890Sstevel@tonic-gate uu_warn_internal(int err, const char *format, va_list alist)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	if (pname != NULL)
920Sstevel@tonic-gate 		(void) fprintf(stderr, PNAME_FMT, pname);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if (strrchr(format, '\n') == NULL)
970Sstevel@tonic-gate 		(void) fprintf(stderr, ERRNO_FMT, strerror(err));
980Sstevel@tonic-gate }
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate void
uu_vwarn(const char * format,va_list alist)1010Sstevel@tonic-gate uu_vwarn(const char *format, va_list alist)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate 	uu_warn_internal(errno, format, alist);
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /*PRINTFLIKE1*/
1070Sstevel@tonic-gate void
uu_warn(const char * format,...)1080Sstevel@tonic-gate uu_warn(const char *format, ...)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	va_list alist;
1110Sstevel@tonic-gate 	va_start(alist, format);
1120Sstevel@tonic-gate 	uu_warn_internal(errno, format, alist);
1130Sstevel@tonic-gate 	va_end(alist);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate static void
uu_die_internal(int status,const char * format,va_list alist)1170Sstevel@tonic-gate uu_die_internal(int status, const char *format, va_list alist)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	uu_warn_internal(errno, format, alist);
1200Sstevel@tonic-gate #ifdef DEBUG
1210Sstevel@tonic-gate 	{
1220Sstevel@tonic-gate 		char *cp;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 		if (!issetugid()) {
1250Sstevel@tonic-gate 			cp = getenv("UU_DIE_ABORTS");
1260Sstevel@tonic-gate 			if (cp != NULL && *cp != '\0')
1270Sstevel@tonic-gate 				abort();
1280Sstevel@tonic-gate 		}
1290Sstevel@tonic-gate 	}
1300Sstevel@tonic-gate #endif
1310Sstevel@tonic-gate 	exit(status);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate void
uu_vdie(const char * format,va_list alist)1350Sstevel@tonic-gate uu_vdie(const char *format, va_list alist)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate 	uu_die_internal(UU_EXIT_FATAL, format, alist);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate /*PRINTFLIKE1*/
1410Sstevel@tonic-gate void
uu_die(const char * format,...)1420Sstevel@tonic-gate uu_die(const char *format, ...)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate 	va_list alist;
1450Sstevel@tonic-gate 	va_start(alist, format);
1460Sstevel@tonic-gate 	uu_die_internal(UU_EXIT_FATAL, format, alist);
1470Sstevel@tonic-gate 	va_end(alist);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate void
uu_vxdie(int status,const char * format,va_list alist)1510Sstevel@tonic-gate uu_vxdie(int status, const char *format, va_list alist)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	uu_die_internal(status, format, alist);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate /*PRINTFLIKE2*/
1570Sstevel@tonic-gate void
uu_xdie(int status,const char * format,...)1580Sstevel@tonic-gate uu_xdie(int status, const char *format, ...)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	va_list alist;
1610Sstevel@tonic-gate 	va_start(alist, format);
1620Sstevel@tonic-gate 	uu_die_internal(status, format, alist);
1630Sstevel@tonic-gate 	va_end(alist);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate const char *
uu_setpname(char * arg0)1670Sstevel@tonic-gate uu_setpname(char *arg0)
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate 	/*
1700Sstevel@tonic-gate 	 * Having a NULL argv[0], while uncommon, is possible.  It
1710Sstevel@tonic-gate 	 * makes more sense to handle this event in uu_setpname rather
1720Sstevel@tonic-gate 	 * than in each of its consumers.
1730Sstevel@tonic-gate 	 */
1740Sstevel@tonic-gate 	if (arg0 == NULL) {
1750Sstevel@tonic-gate 		pname = getexecname();
1760Sstevel@tonic-gate 		if (pname == NULL)
1770Sstevel@tonic-gate 			pname = "unknown_command";
1780Sstevel@tonic-gate 		return (pname);
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	/*
1820Sstevel@tonic-gate 	 * Guard against '/' at end of command invocation.
1830Sstevel@tonic-gate 	 */
1840Sstevel@tonic-gate 	for (;;) {
1850Sstevel@tonic-gate 		char *p = strrchr(arg0, '/');
1860Sstevel@tonic-gate 		if (p == NULL) {
1870Sstevel@tonic-gate 			pname = arg0;
1880Sstevel@tonic-gate 			break;
1890Sstevel@tonic-gate 		} else {
1900Sstevel@tonic-gate 			if (*(p + 1) == '\0') {
1910Sstevel@tonic-gate 				*p = '\0';
1920Sstevel@tonic-gate 				continue;
1930Sstevel@tonic-gate 			}
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 			pname = p + 1;
1960Sstevel@tonic-gate 			break;
1970Sstevel@tonic-gate 		}
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	return (pname);
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate const char *
uu_getpname(void)2040Sstevel@tonic-gate uu_getpname(void)
2050Sstevel@tonic-gate {
2060Sstevel@tonic-gate 	return (pname);
2070Sstevel@tonic-gate }
208