17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * Copyright 1999-2002 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate#include <libintl.h>
307c478bdstevel@tonic-gate#include <limits.h>
317c478bdstevel@tonic-gate#include <string.h>
327c478bdstevel@tonic-gate#include <stdlib.h>
337c478bdstevel@tonic-gate#include <stdarg.h>
347c478bdstevel@tonic-gate#include <stdio.h>
357c478bdstevel@tonic-gate#include <errno.h>
367c478bdstevel@tonic-gate#include <wchar.h>
377c478bdstevel@tonic-gate
387c478bdstevel@tonic-gate#include <utils.h>
397c478bdstevel@tonic-gate
407c478bdstevel@tonic-gatestatic const char PNAME_FMT[] = "%s: ";
417c478bdstevel@tonic-gatestatic const char ERRNO_FMT[] = ": %s\n";
427c478bdstevel@tonic-gate
437c478bdstevel@tonic-gatestatic const char *pname;
447c478bdstevel@tonic-gate
457c478bdstevel@tonic-gate/*PRINTFLIKE1*/
467c478bdstevel@tonic-gatevoid
477c478bdstevel@tonic-gatewarn(const char *format, ...)
487c478bdstevel@tonic-gate{
497c478bdstevel@tonic-gate	int err = errno;
507c478bdstevel@tonic-gate	va_list alist;
517c478bdstevel@tonic-gate
527c478bdstevel@tonic-gate	if (pname != NULL)
537c478bdstevel@tonic-gate		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
547c478bdstevel@tonic-gate
557c478bdstevel@tonic-gate	va_start(alist, format);
567c478bdstevel@tonic-gate	(void) vfprintf(stderr, format, alist);
577c478bdstevel@tonic-gate	va_end(alist);
587c478bdstevel@tonic-gate
597c478bdstevel@tonic-gate	if (strrchr(format, '\n') == NULL)
607c478bdstevel@tonic-gate		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
617c478bdstevel@tonic-gate}
627c478bdstevel@tonic-gate
637c478bdstevel@tonic-gate/*PRINTFLIKE1*/
647c478bdstevel@tonic-gatevoid
657c478bdstevel@tonic-gatedie(const char *format, ...)
667c478bdstevel@tonic-gate{
677c478bdstevel@tonic-gate	int err = errno;
687c478bdstevel@tonic-gate	va_list alist;
697c478bdstevel@tonic-gate
707c478bdstevel@tonic-gate	if (pname != NULL)
717c478bdstevel@tonic-gate		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
727c478bdstevel@tonic-gate
737c478bdstevel@tonic-gate	va_start(alist, format);
747c478bdstevel@tonic-gate	(void) vfprintf(stderr, format, alist);
757c478bdstevel@tonic-gate	va_end(alist);
767c478bdstevel@tonic-gate
777c478bdstevel@tonic-gate	if (strrchr(format, '\n') == NULL)
787c478bdstevel@tonic-gate		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
797c478bdstevel@tonic-gate
807c478bdstevel@tonic-gate	exit(E_ERROR);
817c478bdstevel@tonic-gate}
827c478bdstevel@tonic-gate
837c478bdstevel@tonic-gateconst char *
847c478bdstevel@tonic-gategetpname(const char *arg0)
857c478bdstevel@tonic-gate{
867c478bdstevel@tonic-gate	const char *p = strrchr(arg0, '/');
877c478bdstevel@tonic-gate
887c478bdstevel@tonic-gate	if (p == NULL)
897c478bdstevel@tonic-gate		p = arg0;
907c478bdstevel@tonic-gate	else
917c478bdstevel@tonic-gate		p++;
927c478bdstevel@tonic-gate
937c478bdstevel@tonic-gate	pname = p;
947c478bdstevel@tonic-gate	return (p);
957c478bdstevel@tonic-gate}
967c478bdstevel@tonic-gate
977c478bdstevel@tonic-gatevoid *
987c478bdstevel@tonic-gatesafe_malloc(size_t size)
997c478bdstevel@tonic-gate{
1007c478bdstevel@tonic-gate	void *a;
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gate	if ((a = malloc(size)) == NULL)
1037c478bdstevel@tonic-gate		die(gettext("out of memory\n"));
1047c478bdstevel@tonic-gate
1057c478bdstevel@tonic-gate	return (a);
1067c478bdstevel@tonic-gate}
1077c478bdstevel@tonic-gate
1087c478bdstevel@tonic-gate/*
1097c478bdstevel@tonic-gate * getdefault() reads from one of the /etc/default files. It takes
1107c478bdstevel@tonic-gate * input of the filename, a variable name to search for, and a
1117c478bdstevel@tonic-gate * prefix to prepend to the result.
1127c478bdstevel@tonic-gate *
1137c478bdstevel@tonic-gate * The file and varname arguments are required. The varname argument
1147c478bdstevel@tonic-gate * must be of the form "VAR=". If the prefix argument
1157c478bdstevel@tonic-gate * is non-null, it will be prepended to the returned string.
1167c478bdstevel@tonic-gate * Double and single quotes are stripped from the result.
1177c478bdstevel@tonic-gate *
1187c478bdstevel@tonic-gate * getdefault() returns NULL if the file cannot be opened, or the
1197c478bdstevel@tonic-gate * variable cannot be found.
1207c478bdstevel@tonic-gate */
1217c478bdstevel@tonic-gatechar *
1227c478bdstevel@tonic-gategetdefault(char *file, char *varname, char *prefix)
1237c478bdstevel@tonic-gate{
1247c478bdstevel@tonic-gate	FILE *fp;
1257c478bdstevel@tonic-gate	char cp[PATH_MAX];
1267c478bdstevel@tonic-gate	char *tmp_cp, *ret_str = NULL;
1277c478bdstevel@tonic-gate	size_t varlen;
1287c478bdstevel@tonic-gate
1297c478bdstevel@tonic-gate	if ((fp = fopen(file, "r")) == NULL)
1307c478bdstevel@tonic-gate		return (ret_str);
1317c478bdstevel@tonic-gate	varlen = strlen(varname);
1327c478bdstevel@tonic-gate	while (fgets(cp, PATH_MAX, fp) != NULL) {
1337c478bdstevel@tonic-gate		size_t len;
1347c478bdstevel@tonic-gate
1357c478bdstevel@tonic-gate		if (cp[0] == '#' || cp[0] == '\n')
1367c478bdstevel@tonic-gate			continue;
1377c478bdstevel@tonic-gate		len = strlen(cp);
1387c478bdstevel@tonic-gate		if (cp[len - 1] == '\n') {
1397c478bdstevel@tonic-gate			len--;
1407c478bdstevel@tonic-gate			cp[len] = '\0';
1417c478bdstevel@tonic-gate		}
1427c478bdstevel@tonic-gate		/* Find line containing varname */
1437c478bdstevel@tonic-gate		if (strncmp(varname, cp, varlen) == 0) {
1447c478bdstevel@tonic-gate			char *cp2, *strip_ptr = NULL;
1457c478bdstevel@tonic-gate			size_t tlen;
1467c478bdstevel@tonic-gate			int inquotes = 0;
1477c478bdstevel@tonic-gate
1487c478bdstevel@tonic-gate			cp2 = tmp_cp = cp + varlen;
1497c478bdstevel@tonic-gate			/*
1507c478bdstevel@tonic-gate			 * Remove extra characters after any space,
1517c478bdstevel@tonic-gate			 * tab, or unquoted semicolon, and strip quotes.
1527c478bdstevel@tonic-gate			 */
1537c478bdstevel@tonic-gate			while ((*cp2 != '\0') &&
1547c478bdstevel@tonic-gate			    (*cp2 != ' ') && (*cp2 != '\t') &&
1557c478bdstevel@tonic-gate			    !((*cp2 == ';') && (inquotes == 0))) {
1567c478bdstevel@tonic-gate				if (*cp2 == '\"' || *cp2 == '\'') {
1577c478bdstevel@tonic-gate					if (*cp2 == '\"') {
1587c478bdstevel@tonic-gate						inquotes =
1597c478bdstevel@tonic-gate						    inquotes == 0 ? 1 : 0;
1607c478bdstevel@tonic-gate					}
1617c478bdstevel@tonic-gate					if (strip_ptr == NULL) {
1627c478bdstevel@tonic-gate						strip_ptr = cp2;
1637c478bdstevel@tonic-gate					}
1647c478bdstevel@tonic-gate				} else {
1657c478bdstevel@tonic-gate					if (strip_ptr != NULL) {
1667c478bdstevel@tonic-gate						*strip_ptr++ = *cp2;
1677c478bdstevel@tonic-gate					}
1687c478bdstevel@tonic-gate				}
1697c478bdstevel@tonic-gate				cp2++;
1707c478bdstevel@tonic-gate			}
1717c478bdstevel@tonic-gate			if (strip_ptr != NULL) {
1727c478bdstevel@tonic-gate				*strip_ptr = '\0';
1737c478bdstevel@tonic-gate			}
1747c478bdstevel@tonic-gate			len = cp2 - tmp_cp;
1757c478bdstevel@tonic-gate			if (prefix) {
1767c478bdstevel@tonic-gate				tlen = len + strlen(prefix) + 1;
1777c478bdstevel@tonic-gate				ret_str = safe_malloc(tlen);
1787c478bdstevel@tonic-gate				(void) snprintf(ret_str, tlen, "%s%s",
1797c478bdstevel@tonic-gate				    prefix, tmp_cp);
1807c478bdstevel@tonic-gate			} else {
1817c478bdstevel@tonic-gate				tlen = len + 1;
1827c478bdstevel@tonic-gate				ret_str = safe_malloc(tlen);
1837c478bdstevel@tonic-gate				(void) snprintf(ret_str, tlen, "%s", tmp_cp);
1847c478bdstevel@tonic-gate			}
1857c478bdstevel@tonic-gate			break;
1867c478bdstevel@tonic-gate		}
1877c478bdstevel@tonic-gate	}
1887c478bdstevel@tonic-gate	(void) fclose(fp);
1897c478bdstevel@tonic-gate	return (ret_str);
1907c478bdstevel@tonic-gate}
191