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
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]
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"
40 static const char PNAME_FMT
[] = "%s: ";
41 static const char ERRNO_FMT
[] = ": %s\n";
43 static const char *pname
;
47 warn(const char *format
, ...)
53 (void) fprintf(stderr
, gettext(PNAME_FMT
), pname
);
55 va_start(alist
, format
);
56 (void) vfprintf(stderr
, format
, alist
);
59 if (strrchr(format
, '\n') == NULL
)
60 (void) fprintf(stderr
, gettext(ERRNO_FMT
), strerror(err
));
65 die(const char *format
, ...)
71 (void) fprintf(stderr
, gettext(PNAME_FMT
), pname
);
73 va_start(alist
, format
);
74 (void) vfprintf(stderr
, format
, alist
);
77 if (strrchr(format
, '\n') == NULL
)
78 (void) fprintf(stderr
, gettext(ERRNO_FMT
), strerror(err
));
84 getpname(const char *arg0
)
86 const char *p
= strrchr(arg0
, '/');
98 safe_malloc(size_t size
)
102 if ((a
= malloc(size
)) == NULL
)
103 die(gettext("out of memory\n"));
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.
122 getdefault(char *file
, char *varname
, char *prefix
)
126 char *tmp_cp
, *ret_str
= NULL
;
129 if ((fp
= fopen(file
, "r")) == NULL
)
131 varlen
= strlen(varname
);
132 while (fgets(cp
, PATH_MAX
, fp
) != NULL
) {
135 if (cp
[0] == '#' || cp
[0] == '\n')
138 if (cp
[len
- 1] == '\n') {
142 /* Find line containing varname */
143 if (strncmp(varname
, cp
, varlen
) == 0) {
144 char *cp2
, *strip_ptr
= NULL
;
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
== '\'') {
159 inquotes
== 0 ? 1 : 0;
161 if (strip_ptr
== NULL
) {
165 if (strip_ptr
!= NULL
) {
171 if (strip_ptr
!= NULL
) {
176 tlen
= len
+ strlen(prefix
) + 1;
177 ret_str
= safe_malloc(tlen
);
178 (void) snprintf(ret_str
, tlen
, "%s%s",
182 ret_str
= safe_malloc(tlen
);
183 (void) snprintf(ret_str
, tlen
, "%s", tmp_cp
);