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