Switch from _Noreturn to __attribute__((noreturn))
[zfs.git] / lib / libuutil / uu_pname.c
blob610b8585dcbf183a2490315abe5e63bfe4b2fbe1
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
29 #include "libuutil_common.h"
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>
41 static const char *pname;
43 static __attribute__((noreturn)) void
44 uu_die_internal(int status, const char *format, va_list alist);
46 int uu_exit_ok_value = EXIT_SUCCESS;
47 int uu_exit_fatal_value = EXIT_FAILURE;
48 int uu_exit_usage_value = 2;
50 int *
51 uu_exit_ok(void)
53 return (&uu_exit_ok_value);
56 int *
57 uu_exit_fatal(void)
59 return (&uu_exit_fatal_value);
62 int *
63 uu_exit_usage(void)
65 return (&uu_exit_usage_value);
68 void
69 uu_alt_exit(int profile)
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;
85 static __attribute__((format(printf, 2, 0))) void
86 uu_warn_internal(int err, const char *format, va_list alist)
88 if (pname != NULL)
89 (void) fprintf(stderr, "%s: ", pname);
91 if (format != NULL)
92 (void) vfprintf(stderr, format, alist);
94 if (strrchr(format, '\n') == NULL)
95 (void) fprintf(stderr, ": %s\n", strerror(err));
98 void
99 uu_vwarn(const char *format, va_list alist)
101 uu_warn_internal(errno, format, alist);
104 void
105 uu_warn(const char *format, ...)
107 va_list alist;
108 va_start(alist, format);
109 uu_warn_internal(errno, format, alist);
110 va_end(alist);
113 static __attribute__((format(printf, 2, 0))) __attribute__((noreturn)) void
114 uu_die_internal(int status, const char *format, va_list alist)
116 uu_warn_internal(errno, format, alist);
117 #ifdef DEBUG
119 char *cp;
121 if (!issetugid()) {
122 cp = getenv("UU_DIE_ABORTS");
123 if (cp != NULL && *cp != '\0')
124 abort();
127 #endif
128 exit(status);
131 void
132 uu_vdie(const char *format, va_list alist)
134 uu_die_internal(UU_EXIT_FATAL, format, alist);
137 void
138 uu_die(const char *format, ...)
140 va_list alist;
141 va_start(alist, format);
142 uu_die_internal(UU_EXIT_FATAL, format, alist);
143 va_end(alist);
146 void
147 uu_vxdie(int status, const char *format, va_list alist)
149 uu_die_internal(status, format, alist);
152 void
153 uu_xdie(int status, const char *format, ...)
155 va_list alist;
156 va_start(alist, format);
157 uu_die_internal(status, format, alist);
158 va_end(alist);
161 const char *
162 uu_setpname(char *arg0)
165 * Having a NULL argv[0], while uncommon, is possible. It
166 * makes more sense to handle this event in uu_setpname rather
167 * than in each of its consumers.
169 if (arg0 == NULL) {
170 pname = getexecname();
171 if (pname == NULL)
172 pname = "unknown_command";
173 return (pname);
177 * Guard against '/' at end of command invocation.
179 for (;;) {
180 char *p = strrchr(arg0, '/');
181 if (p == NULL) {
182 pname = arg0;
183 break;
184 } else {
185 if (*(p + 1) == '\0') {
186 *p = '\0';
187 continue;
190 pname = p + 1;
191 break;
195 return (pname);
198 const char *
199 uu_getpname(void)
201 return (pname);