summaryrefslogtreecommitdiff
path: root/usr/src/cmd/newtask/utils.c
blob: 207b7f3e00e53e8db0b1fa82dab87ec3f7b1e6bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 1999-2002 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <libintl.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <wchar.h>

#include <utils.h>

static const char PNAME_FMT[] = "%s: ";
static const char ERRNO_FMT[] = ": %s\n";

static const char *pname;

/*PRINTFLIKE1*/
void
warn(const char *format, ...)
{
	int err = errno;
	va_list alist;

	if (pname != NULL)
		(void) fprintf(stderr, gettext(PNAME_FMT), pname);

	va_start(alist, format);
	(void) vfprintf(stderr, format, alist);
	va_end(alist);

	if (strrchr(format, '\n') == NULL)
		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
}

/*PRINTFLIKE1*/
void
die(const char *format, ...)
{
	int err = errno;
	va_list alist;

	if (pname != NULL)
		(void) fprintf(stderr, gettext(PNAME_FMT), pname);

	va_start(alist, format);
	(void) vfprintf(stderr, format, alist);
	va_end(alist);

	if (strrchr(format, '\n') == NULL)
		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));

	exit(E_ERROR);
}

const char *
getpname(const char *arg0)
{
	const char *p = strrchr(arg0, '/');

	if (p == NULL)
		p = arg0;
	else
		p++;

	pname = p;
	return (p);
}

void *
safe_malloc(size_t size)
{
	void *a;

	if ((a = malloc(size)) == NULL)
		die(gettext("out of memory\n"));

	return (a);
}

/*
 * getdefault() reads from one of the /etc/default files. It takes
 * input of the filename, a variable name to search for, and a
 * prefix to prepend to the result.
 *
 * The file and varname arguments are required. The varname argument
 * must be of the form "VAR=". If the prefix argument
 * is non-null, it will be prepended to the returned string.
 * Double and single quotes are stripped from the result.
 *
 * getdefault() returns NULL if the file cannot be opened, or the
 * variable cannot be found.
 */
char *
getdefault(char *file, char *varname, char *prefix)
{
	FILE *fp;
	char cp[PATH_MAX];
	char *tmp_cp, *ret_str = NULL;
	size_t varlen;

	if ((fp = fopen(file, "r")) == NULL)
		return (ret_str);
	varlen = strlen(varname);
	while (fgets(cp, PATH_MAX, fp) != NULL) {
		size_t len;

		if (cp[0] == '#' || cp[0] == '\n')
			continue;
		len = strlen(cp);
		if (cp[len - 1] == '\n') {
			len--;
			cp[len] = '\0';
		}
		/* Find line containing varname */
		if (strncmp(varname, cp, varlen) == 0) {
			char *cp2, *strip_ptr = NULL;
			size_t tlen;
			int inquotes = 0;

			cp2 = tmp_cp = cp + varlen;
			/*
			 * Remove extra characters after any space,
			 * tab, or unquoted semicolon, and strip quotes.
			 */
			while ((*cp2 != '\0') &&
			    (*cp2 != ' ') && (*cp2 != '\t') &&
			    !((*cp2 == ';') && (inquotes == 0))) {
				if (*cp2 == '\"' || *cp2 == '\'') {
					if (*cp2 == '\"') {
						inquotes =
						    inquotes == 0 ? 1 : 0;
					}
					if (strip_ptr == NULL) {
						strip_ptr = cp2;
					}
				} else {
					if (strip_ptr != NULL) {
						*strip_ptr++ = *cp2;
					}
				}
				cp2++;
			}
			if (strip_ptr != NULL) {
				*strip_ptr = '\0';
			}
			len = cp2 - tmp_cp;
			if (prefix) {
				tlen = len + strlen(prefix) + 1;
				ret_str = safe_malloc(tlen);
				(void) snprintf(ret_str, tlen, "%s%s",
				    prefix, tmp_cp);
			} else {
				tlen = len + 1;
				ret_str = safe_malloc(tlen);
				(void) snprintf(ret_str, tlen, "%s", tmp_cp);
			}
			break;
		}
	}
	(void) fclose(fp);
	return (ret_str);
}