xref: /titanic_44/usr/src/lib/libuutil/common/uu_pname.c (revision 0b5c9250b17799ab6af5fd285c7f4a3ba5b97822)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*0b5c9250Shg115875  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include "libuutil_common.h"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <libintl.h>
327c478bd9Sstevel@tonic-gate #include <limits.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <stdlib.h>
357c478bd9Sstevel@tonic-gate #include <stdarg.h>
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <wchar.h>
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate static const char PNAME_FMT[] = "%s: ";
427c478bd9Sstevel@tonic-gate static const char ERRNO_FMT[] = ": %s\n";
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static const char *pname;
457c478bd9Sstevel@tonic-gate 
46*0b5c9250Shg115875 static void
47*0b5c9250Shg115875 uu_die_internal(int status, const char *format, va_list alist) __NORETURN;
48*0b5c9250Shg115875 
497c478bd9Sstevel@tonic-gate int uu_exit_ok_value = EXIT_SUCCESS;
507c478bd9Sstevel@tonic-gate int uu_exit_fatal_value = EXIT_FAILURE;
517c478bd9Sstevel@tonic-gate int uu_exit_usage_value = 2;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate int *
uu_exit_ok(void)547c478bd9Sstevel@tonic-gate uu_exit_ok(void)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	return (&uu_exit_ok_value);
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate int *
uu_exit_fatal(void)607c478bd9Sstevel@tonic-gate uu_exit_fatal(void)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	return (&uu_exit_fatal_value);
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate int *
uu_exit_usage(void)667c478bd9Sstevel@tonic-gate uu_exit_usage(void)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	return (&uu_exit_usage_value);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate void
uu_alt_exit(int profile)727c478bd9Sstevel@tonic-gate uu_alt_exit(int profile)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	switch (profile) {
757c478bd9Sstevel@tonic-gate 	case UU_PROFILE_DEFAULT:
767c478bd9Sstevel@tonic-gate 		uu_exit_ok_value = EXIT_SUCCESS;
777c478bd9Sstevel@tonic-gate 		uu_exit_fatal_value = EXIT_FAILURE;
787c478bd9Sstevel@tonic-gate 		uu_exit_usage_value = 2;
797c478bd9Sstevel@tonic-gate 		break;
807c478bd9Sstevel@tonic-gate 	case UU_PROFILE_LAUNCHER:
817c478bd9Sstevel@tonic-gate 		uu_exit_ok_value = EXIT_SUCCESS;
827c478bd9Sstevel@tonic-gate 		uu_exit_fatal_value = 124;
837c478bd9Sstevel@tonic-gate 		uu_exit_usage_value = 125;
847c478bd9Sstevel@tonic-gate 		break;
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static void
uu_warn_internal(int err,const char * format,va_list alist)897c478bd9Sstevel@tonic-gate uu_warn_internal(int err, const char *format, va_list alist)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	if (pname != NULL)
927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, PNAME_FMT, pname);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if (strrchr(format, '\n') == NULL)
977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ERRNO_FMT, strerror(err));
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate void
uu_vwarn(const char * format,va_list alist)1017c478bd9Sstevel@tonic-gate uu_vwarn(const char *format, va_list alist)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	uu_warn_internal(errno, format, alist);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1077c478bd9Sstevel@tonic-gate void
uu_warn(const char * format,...)1087c478bd9Sstevel@tonic-gate uu_warn(const char *format, ...)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	va_list alist;
1117c478bd9Sstevel@tonic-gate 	va_start(alist, format);
1127c478bd9Sstevel@tonic-gate 	uu_warn_internal(errno, format, alist);
1137c478bd9Sstevel@tonic-gate 	va_end(alist);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static void
uu_die_internal(int status,const char * format,va_list alist)1177c478bd9Sstevel@tonic-gate uu_die_internal(int status, const char *format, va_list alist)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	uu_warn_internal(errno, format, alist);
1207c478bd9Sstevel@tonic-gate #ifdef DEBUG
1217c478bd9Sstevel@tonic-gate 	{
1227c478bd9Sstevel@tonic-gate 		char *cp;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 		if (!issetugid()) {
1257c478bd9Sstevel@tonic-gate 			cp = getenv("UU_DIE_ABORTS");
1267c478bd9Sstevel@tonic-gate 			if (cp != NULL && *cp != '\0')
1277c478bd9Sstevel@tonic-gate 				abort();
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate #endif
1317c478bd9Sstevel@tonic-gate 	exit(status);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate void
uu_vdie(const char * format,va_list alist)1357c478bd9Sstevel@tonic-gate uu_vdie(const char *format, va_list alist)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	uu_die_internal(UU_EXIT_FATAL, format, alist);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1417c478bd9Sstevel@tonic-gate void
uu_die(const char * format,...)1427c478bd9Sstevel@tonic-gate uu_die(const char *format, ...)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	va_list alist;
1457c478bd9Sstevel@tonic-gate 	va_start(alist, format);
1467c478bd9Sstevel@tonic-gate 	uu_die_internal(UU_EXIT_FATAL, format, alist);
1477c478bd9Sstevel@tonic-gate 	va_end(alist);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate void
uu_vxdie(int status,const char * format,va_list alist)1517c478bd9Sstevel@tonic-gate uu_vxdie(int status, const char *format, va_list alist)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	uu_die_internal(status, format, alist);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
1577c478bd9Sstevel@tonic-gate void
uu_xdie(int status,const char * format,...)1587c478bd9Sstevel@tonic-gate uu_xdie(int status, const char *format, ...)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	va_list alist;
1617c478bd9Sstevel@tonic-gate 	va_start(alist, format);
1627c478bd9Sstevel@tonic-gate 	uu_die_internal(status, format, alist);
1637c478bd9Sstevel@tonic-gate 	va_end(alist);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate const char *
uu_setpname(char * arg0)1677c478bd9Sstevel@tonic-gate uu_setpname(char *arg0)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * Having a NULL argv[0], while uncommon, is possible.  It
1717c478bd9Sstevel@tonic-gate 	 * makes more sense to handle this event in uu_setpname rather
1727c478bd9Sstevel@tonic-gate 	 * than in each of its consumers.
1737c478bd9Sstevel@tonic-gate 	 */
1747c478bd9Sstevel@tonic-gate 	if (arg0 == NULL) {
1757c478bd9Sstevel@tonic-gate 		pname = getexecname();
1767c478bd9Sstevel@tonic-gate 		if (pname == NULL)
1777c478bd9Sstevel@tonic-gate 			pname = "unknown_command";
1787c478bd9Sstevel@tonic-gate 		return (pname);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/*
1827c478bd9Sstevel@tonic-gate 	 * Guard against '/' at end of command invocation.
1837c478bd9Sstevel@tonic-gate 	 */
1847c478bd9Sstevel@tonic-gate 	for (;;) {
1857c478bd9Sstevel@tonic-gate 		char *p = strrchr(arg0, '/');
1867c478bd9Sstevel@tonic-gate 		if (p == NULL) {
1877c478bd9Sstevel@tonic-gate 			pname = arg0;
1887c478bd9Sstevel@tonic-gate 			break;
1897c478bd9Sstevel@tonic-gate 		} else {
1907c478bd9Sstevel@tonic-gate 			if (*(p + 1) == '\0') {
1917c478bd9Sstevel@tonic-gate 				*p = '\0';
1927c478bd9Sstevel@tonic-gate 				continue;
1937c478bd9Sstevel@tonic-gate 			}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 			pname = p + 1;
1967c478bd9Sstevel@tonic-gate 			break;
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	return (pname);
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate const char *
uu_getpname(void)2047c478bd9Sstevel@tonic-gate uu_getpname(void)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	return (pname);
2077c478bd9Sstevel@tonic-gate }
208