xref: /illumos-gate/usr/src/cmd/newtask/utils.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1999-2002 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <libintl.h>
30*7c478bd9Sstevel@tonic-gate #include <limits.h>
31*7c478bd9Sstevel@tonic-gate #include <string.h>
32*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
33*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
34*7c478bd9Sstevel@tonic-gate #include <stdio.h>
35*7c478bd9Sstevel@tonic-gate #include <errno.h>
36*7c478bd9Sstevel@tonic-gate #include <wchar.h>
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #include <utils.h>
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate static const char PNAME_FMT[] = "%s: ";
41*7c478bd9Sstevel@tonic-gate static const char ERRNO_FMT[] = ": %s\n";
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate static const char *pname;
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
46*7c478bd9Sstevel@tonic-gate void
47*7c478bd9Sstevel@tonic-gate warn(const char *format, ...)
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate 	int err = errno;
50*7c478bd9Sstevel@tonic-gate 	va_list alist;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	if (pname != NULL)
53*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	va_start(alist, format);
56*7c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
57*7c478bd9Sstevel@tonic-gate 	va_end(alist);
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	if (strrchr(format, '\n') == NULL)
60*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
61*7c478bd9Sstevel@tonic-gate }
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
64*7c478bd9Sstevel@tonic-gate void
65*7c478bd9Sstevel@tonic-gate die(const char *format, ...)
66*7c478bd9Sstevel@tonic-gate {
67*7c478bd9Sstevel@tonic-gate 	int err = errno;
68*7c478bd9Sstevel@tonic-gate 	va_list alist;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	if (pname != NULL)
71*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	va_start(alist, format);
74*7c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
75*7c478bd9Sstevel@tonic-gate 	va_end(alist);
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	if (strrchr(format, '\n') == NULL)
78*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	exit(E_ERROR);
81*7c478bd9Sstevel@tonic-gate }
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate const char *
84*7c478bd9Sstevel@tonic-gate getpname(const char *arg0)
85*7c478bd9Sstevel@tonic-gate {
86*7c478bd9Sstevel@tonic-gate 	const char *p = strrchr(arg0, '/');
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	if (p == NULL)
89*7c478bd9Sstevel@tonic-gate 		p = arg0;
90*7c478bd9Sstevel@tonic-gate 	else
91*7c478bd9Sstevel@tonic-gate 		p++;
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	pname = p;
94*7c478bd9Sstevel@tonic-gate 	return (p);
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate void *
98*7c478bd9Sstevel@tonic-gate safe_malloc(size_t size)
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate 	void *a;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	if ((a = malloc(size)) == NULL)
103*7c478bd9Sstevel@tonic-gate 		die(gettext("out of memory\n"));
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	return (a);
106*7c478bd9Sstevel@tonic-gate }
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate /*
109*7c478bd9Sstevel@tonic-gate  * getdefault() reads from one of the /etc/default files. It takes
110*7c478bd9Sstevel@tonic-gate  * input of the filename, a variable name to search for, and a
111*7c478bd9Sstevel@tonic-gate  * prefix to prepend to the result.
112*7c478bd9Sstevel@tonic-gate  *
113*7c478bd9Sstevel@tonic-gate  * The file and varname arguments are required. The varname argument
114*7c478bd9Sstevel@tonic-gate  * must be of the form "VAR=". If the prefix argument
115*7c478bd9Sstevel@tonic-gate  * is non-null, it will be prepended to the returned string.
116*7c478bd9Sstevel@tonic-gate  * Double and single quotes are stripped from the result.
117*7c478bd9Sstevel@tonic-gate  *
118*7c478bd9Sstevel@tonic-gate  * getdefault() returns NULL if the file cannot be opened, or the
119*7c478bd9Sstevel@tonic-gate  * variable cannot be found.
120*7c478bd9Sstevel@tonic-gate  */
121*7c478bd9Sstevel@tonic-gate char *
122*7c478bd9Sstevel@tonic-gate getdefault(char *file, char *varname, char *prefix)
123*7c478bd9Sstevel@tonic-gate {
124*7c478bd9Sstevel@tonic-gate 	FILE *fp;
125*7c478bd9Sstevel@tonic-gate 	char cp[PATH_MAX];
126*7c478bd9Sstevel@tonic-gate 	char *tmp_cp, *ret_str = NULL;
127*7c478bd9Sstevel@tonic-gate 	size_t varlen;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	if ((fp = fopen(file, "r")) == NULL)
130*7c478bd9Sstevel@tonic-gate 		return (ret_str);
131*7c478bd9Sstevel@tonic-gate 	varlen = strlen(varname);
132*7c478bd9Sstevel@tonic-gate 	while (fgets(cp, PATH_MAX, fp) != NULL) {
133*7c478bd9Sstevel@tonic-gate 		size_t len;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 		if (cp[0] == '#' || cp[0] == '\n')
136*7c478bd9Sstevel@tonic-gate 			continue;
137*7c478bd9Sstevel@tonic-gate 		len = strlen(cp);
138*7c478bd9Sstevel@tonic-gate 		if (cp[len - 1] == '\n') {
139*7c478bd9Sstevel@tonic-gate 			len--;
140*7c478bd9Sstevel@tonic-gate 			cp[len] = '\0';
141*7c478bd9Sstevel@tonic-gate 		}
142*7c478bd9Sstevel@tonic-gate 		/* Find line containing varname */
143*7c478bd9Sstevel@tonic-gate 		if (strncmp(varname, cp, varlen) == 0) {
144*7c478bd9Sstevel@tonic-gate 			char *cp2, *strip_ptr = NULL;
145*7c478bd9Sstevel@tonic-gate 			size_t tlen;
146*7c478bd9Sstevel@tonic-gate 			int inquotes = 0;
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 			cp2 = tmp_cp = cp + varlen;
149*7c478bd9Sstevel@tonic-gate 			/*
150*7c478bd9Sstevel@tonic-gate 			 * Remove extra characters after any space,
151*7c478bd9Sstevel@tonic-gate 			 * tab, or unquoted semicolon, and strip quotes.
152*7c478bd9Sstevel@tonic-gate 			 */
153*7c478bd9Sstevel@tonic-gate 			while ((*cp2 != '\0') &&
154*7c478bd9Sstevel@tonic-gate 			    (*cp2 != ' ') && (*cp2 != '\t') &&
155*7c478bd9Sstevel@tonic-gate 			    !((*cp2 == ';') && (inquotes == 0))) {
156*7c478bd9Sstevel@tonic-gate 				if (*cp2 == '\"' || *cp2 == '\'') {
157*7c478bd9Sstevel@tonic-gate 					if (*cp2 == '\"') {
158*7c478bd9Sstevel@tonic-gate 						inquotes =
159*7c478bd9Sstevel@tonic-gate 						    inquotes == 0 ? 1 : 0;
160*7c478bd9Sstevel@tonic-gate 					}
161*7c478bd9Sstevel@tonic-gate 					if (strip_ptr == NULL) {
162*7c478bd9Sstevel@tonic-gate 						strip_ptr = cp2;
163*7c478bd9Sstevel@tonic-gate 					}
164*7c478bd9Sstevel@tonic-gate 				} else {
165*7c478bd9Sstevel@tonic-gate 					if (strip_ptr != NULL) {
166*7c478bd9Sstevel@tonic-gate 						*strip_ptr++ = *cp2;
167*7c478bd9Sstevel@tonic-gate 					}
168*7c478bd9Sstevel@tonic-gate 				}
169*7c478bd9Sstevel@tonic-gate 				cp2++;
170*7c478bd9Sstevel@tonic-gate 			}
171*7c478bd9Sstevel@tonic-gate 			if (strip_ptr != NULL) {
172*7c478bd9Sstevel@tonic-gate 				*strip_ptr = '\0';
173*7c478bd9Sstevel@tonic-gate 			}
174*7c478bd9Sstevel@tonic-gate 			len = cp2 - tmp_cp;
175*7c478bd9Sstevel@tonic-gate 			if (prefix) {
176*7c478bd9Sstevel@tonic-gate 				tlen = len + strlen(prefix) + 1;
177*7c478bd9Sstevel@tonic-gate 				ret_str = safe_malloc(tlen);
178*7c478bd9Sstevel@tonic-gate 				(void) snprintf(ret_str, tlen, "%s%s",
179*7c478bd9Sstevel@tonic-gate 				    prefix, tmp_cp);
180*7c478bd9Sstevel@tonic-gate 			} else {
181*7c478bd9Sstevel@tonic-gate 				tlen = len + 1;
182*7c478bd9Sstevel@tonic-gate 				ret_str = safe_malloc(tlen);
183*7c478bd9Sstevel@tonic-gate 				(void) snprintf(ret_str, tlen, "%s", tmp_cp);
184*7c478bd9Sstevel@tonic-gate 			}
185*7c478bd9Sstevel@tonic-gate 			break;
186*7c478bd9Sstevel@tonic-gate 		}
187*7c478bd9Sstevel@tonic-gate 	}
188*7c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
189*7c478bd9Sstevel@tonic-gate 	return (ret_str);
190*7c478bd9Sstevel@tonic-gate }
191