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