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