xref: /illumos-gate/usr/src/cmd/newtask/utils.c (revision 2a8bcb4e)
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 #include <libintl.h>
28*7c478bd9Sstevel@tonic-gate #include <limits.h>
29*7c478bd9Sstevel@tonic-gate #include <string.h>
30*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
32*7c478bd9Sstevel@tonic-gate #include <stdio.h>
33*7c478bd9Sstevel@tonic-gate #include <errno.h>
34*7c478bd9Sstevel@tonic-gate #include <wchar.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include <utils.h>
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate static const char PNAME_FMT[] = "%s: ";
39*7c478bd9Sstevel@tonic-gate static const char ERRNO_FMT[] = ": %s\n";
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate static const char *pname;
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
44*7c478bd9Sstevel@tonic-gate void
warn(const char * format,...)45*7c478bd9Sstevel@tonic-gate warn(const char *format, ...)
46*7c478bd9Sstevel@tonic-gate {
47*7c478bd9Sstevel@tonic-gate 	int err = errno;
48*7c478bd9Sstevel@tonic-gate 	va_list alist;
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 	if (pname != NULL)
51*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate 	va_start(alist, format);
54*7c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
55*7c478bd9Sstevel@tonic-gate 	va_end(alist);
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	if (strrchr(format, '\n') == NULL)
58*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
59*7c478bd9Sstevel@tonic-gate }
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
62*7c478bd9Sstevel@tonic-gate void
die(const char * format,...)63*7c478bd9Sstevel@tonic-gate die(const char *format, ...)
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	int err = errno;
66*7c478bd9Sstevel@tonic-gate 	va_list alist;
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	if (pname != NULL)
69*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	va_start(alist, format);
72*7c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
73*7c478bd9Sstevel@tonic-gate 	va_end(alist);
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	if (strrchr(format, '\n') == NULL)
76*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	exit(E_ERROR);
79*7c478bd9Sstevel@tonic-gate }
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate const char *
getpname(const char * arg0)82*7c478bd9Sstevel@tonic-gate getpname(const char *arg0)
83*7c478bd9Sstevel@tonic-gate {
84*7c478bd9Sstevel@tonic-gate 	const char *p = strrchr(arg0, '/');
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	if (p == NULL)
87*7c478bd9Sstevel@tonic-gate 		p = arg0;
88*7c478bd9Sstevel@tonic-gate 	else
89*7c478bd9Sstevel@tonic-gate 		p++;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	pname = p;
92*7c478bd9Sstevel@tonic-gate 	return (p);
93*7c478bd9Sstevel@tonic-gate }
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate void *
safe_malloc(size_t size)96*7c478bd9Sstevel@tonic-gate safe_malloc(size_t size)
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate 	void *a;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	if ((a = malloc(size)) == NULL)
101*7c478bd9Sstevel@tonic-gate 		die(gettext("out of memory\n"));
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	return (a);
104*7c478bd9Sstevel@tonic-gate }
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate /*
107*7c478bd9Sstevel@tonic-gate  * getdefault() reads from one of the /etc/default files. It takes
108*7c478bd9Sstevel@tonic-gate  * input of the filename, a variable name to search for, and a
109*7c478bd9Sstevel@tonic-gate  * prefix to prepend to the result.
110*7c478bd9Sstevel@tonic-gate  *
111*7c478bd9Sstevel@tonic-gate  * The file and varname arguments are required. The varname argument
112*7c478bd9Sstevel@tonic-gate  * must be of the form "VAR=". If the prefix argument
113*7c478bd9Sstevel@tonic-gate  * is non-null, it will be prepended to the returned string.
114*7c478bd9Sstevel@tonic-gate  * Double and single quotes are stripped from the result.
115*7c478bd9Sstevel@tonic-gate  *
116*7c478bd9Sstevel@tonic-gate  * getdefault() returns NULL if the file cannot be opened, or the
117*7c478bd9Sstevel@tonic-gate  * variable cannot be found.
118*7c478bd9Sstevel@tonic-gate  */
119*7c478bd9Sstevel@tonic-gate char *
getdefault(char * file,char * varname,char * prefix)120*7c478bd9Sstevel@tonic-gate getdefault(char *file, char *varname, char *prefix)
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	FILE *fp;
123*7c478bd9Sstevel@tonic-gate 	char cp[PATH_MAX];
124*7c478bd9Sstevel@tonic-gate 	char *tmp_cp, *ret_str = NULL;
125*7c478bd9Sstevel@tonic-gate 	size_t varlen;
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 	if ((fp = fopen(file, "r")) == NULL)
128*7c478bd9Sstevel@tonic-gate 		return (ret_str);
129*7c478bd9Sstevel@tonic-gate 	varlen = strlen(varname);
130*7c478bd9Sstevel@tonic-gate 	while (fgets(cp, PATH_MAX, fp) != NULL) {
131*7c478bd9Sstevel@tonic-gate 		size_t len;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 		if (cp[0] == '#' || cp[0] == '\n')
134*7c478bd9Sstevel@tonic-gate 			continue;
135*7c478bd9Sstevel@tonic-gate 		len = strlen(cp);
136*7c478bd9Sstevel@tonic-gate 		if (cp[len - 1] == '\n') {
137*7c478bd9Sstevel@tonic-gate 			len--;
138*7c478bd9Sstevel@tonic-gate 			cp[len] = '\0';
139*7c478bd9Sstevel@tonic-gate 		}
140*7c478bd9Sstevel@tonic-gate 		/* Find line containing varname */
141*7c478bd9Sstevel@tonic-gate 		if (strncmp(varname, cp, varlen) == 0) {
142*7c478bd9Sstevel@tonic-gate 			char *cp2, *strip_ptr = NULL;
143*7c478bd9Sstevel@tonic-gate 			size_t tlen;
144*7c478bd9Sstevel@tonic-gate 			int inquotes = 0;
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 			cp2 = tmp_cp = cp + varlen;
147*7c478bd9Sstevel@tonic-gate 			/*
148*7c478bd9Sstevel@tonic-gate 			 * Remove extra characters after any space,
149*7c478bd9Sstevel@tonic-gate 			 * tab, or unquoted semicolon, and strip quotes.
150*7c478bd9Sstevel@tonic-gate 			 */
151*7c478bd9Sstevel@tonic-gate 			while ((*cp2 != '\0') &&
152*7c478bd9Sstevel@tonic-gate 			    (*cp2 != ' ') && (*cp2 != '\t') &&
153*7c478bd9Sstevel@tonic-gate 			    !((*cp2 == ';') && (inquotes == 0))) {
154*7c478bd9Sstevel@tonic-gate 				if (*cp2 == '\"' || *cp2 == '\'') {
155*7c478bd9Sstevel@tonic-gate 					if (*cp2 == '\"') {
156*7c478bd9Sstevel@tonic-gate 						inquotes =
157*7c478bd9Sstevel@tonic-gate 						    inquotes == 0 ? 1 : 0;
158*7c478bd9Sstevel@tonic-gate 					}
159*7c478bd9Sstevel@tonic-gate 					if (strip_ptr == NULL) {
160*7c478bd9Sstevel@tonic-gate 						strip_ptr = cp2;
161*7c478bd9Sstevel@tonic-gate 					}
162*7c478bd9Sstevel@tonic-gate 				} else {
163*7c478bd9Sstevel@tonic-gate 					if (strip_ptr != NULL) {
164*7c478bd9Sstevel@tonic-gate 						*strip_ptr++ = *cp2;
165*7c478bd9Sstevel@tonic-gate 					}
166*7c478bd9Sstevel@tonic-gate 				}
167*7c478bd9Sstevel@tonic-gate 				cp2++;
168*7c478bd9Sstevel@tonic-gate 			}
169*7c478bd9Sstevel@tonic-gate 			if (strip_ptr != NULL) {
170*7c478bd9Sstevel@tonic-gate 				*strip_ptr = '\0';
171*7c478bd9Sstevel@tonic-gate 			}
172*7c478bd9Sstevel@tonic-gate 			len = cp2 - tmp_cp;
173*7c478bd9Sstevel@tonic-gate 			if (prefix) {
174*7c478bd9Sstevel@tonic-gate 				tlen = len + strlen(prefix) + 1;
175*7c478bd9Sstevel@tonic-gate 				ret_str = safe_malloc(tlen);
176*7c478bd9Sstevel@tonic-gate 				(void) snprintf(ret_str, tlen, "%s%s",
177*7c478bd9Sstevel@tonic-gate 				    prefix, tmp_cp);
178*7c478bd9Sstevel@tonic-gate 			} else {
179*7c478bd9Sstevel@tonic-gate 				tlen = len + 1;
180*7c478bd9Sstevel@tonic-gate 				ret_str = safe_malloc(tlen);
181*7c478bd9Sstevel@tonic-gate 				(void) snprintf(ret_str, tlen, "%s", tmp_cp);
182*7c478bd9Sstevel@tonic-gate 			}
183*7c478bd9Sstevel@tonic-gate 			break;
184*7c478bd9Sstevel@tonic-gate 		}
185*7c478bd9Sstevel@tonic-gate 	}
186*7c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
187*7c478bd9Sstevel@tonic-gate 	return (ret_str);
188*7c478bd9Sstevel@tonic-gate }
189