8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / svr4pkg / pkgparam / pkgparam.c
blob42289f37302fb8e079032117335d4f900d6eb3d3
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #include <stdio.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <limits.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <sys/types.h>
38 #include <pkgstrct.h>
39 #include <pkginfo.h>
40 #include <locale.h>
41 #include <libintl.h>
43 #include <pkglib.h>
44 #include <libadm.h>
45 #include <libinst.h>
47 extern char *pkgfile;
49 #define ERR_ROOT_SET "Could not set install root from the environment."
50 #define ERR_ROOT_CMD "Command line install root contends with environment."
51 #define ERR_MESG "unable to locate parameter information for \"%s\""
52 #define ERR_FLT "parsing error in parameter file"
53 #define ERR_USAGE "usage:\n" \
54 "\t%s [-v] [-d device] pkginst [param [param ...]]\n" \
55 "\t%s [-v] -f file [param [param ...]]\n"
56 #define HASHSIZE 151
57 #define BSZ 4
60 static char *device = NULL;
61 static int errflg = 0;
62 static int vflag = 0;
64 static void print_entry(char *, char *);
66 static void
67 usage(void)
69 char *prog = get_prog_name();
71 (void) fprintf(stderr, gettext(ERR_USAGE), prog, prog);
72 exit(1);
75 int
76 main(int argc, char *argv[])
78 char *value, *pkginst;
79 char *param, parambuf[128];
80 int c;
82 pkgfile = NULL;
84 /* initialize locale mechanism */
86 (void) setlocale(LC_ALL, "");
88 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
89 #define TEXT_DOMAIN "SYS_TEST"
90 #endif
91 (void) textdomain(TEXT_DOMAIN);
93 /* determine program name */
95 (void) set_prog_name(argv[0]);
97 /* establish installation root directory */
99 if (!set_inst_root(getenv("PKG_INSTALL_ROOT"))) {
100 progerr(gettext(ERR_ROOT_SET));
101 exit(1);
104 while ((c = getopt(argc, argv, "R:vd:f:?")) != EOF) {
105 switch (c) {
106 case 'v':
107 vflag++;
108 break;
110 case 'f':
111 /* -f could specify filename to get parameters from */
112 pkgfile = optarg;
113 break;
115 case 'd':
116 /* -d could specify stream or mountable device */
117 device = flex_device(optarg, 1);
118 break;
120 case 'R':
121 if (!set_inst_root(optarg)) {
122 progerr(gettext(ERR_ROOT_CMD));
123 exit(1);
125 break;
127 default:
128 case '?':
129 usage();
133 set_PKGpaths(get_inst_root());
135 if (pkgfile) {
136 if (device)
137 usage();
138 pkginst = pkgfile;
139 } else {
140 if ((optind+1) > argc)
141 usage();
143 if (pkghead(device))
144 return (1); /* couldn't obtain info about device */
145 pkginst = argv[optind++];
148 /* If a filename was specified or install db does not exist */
149 do {
150 param = argv[optind];
151 if (!param) {
152 param = parambuf;
153 *param = '\0';
155 value = pkgparam(pkginst, param);
156 if (value == NULL) {
157 if (errno == EFAULT) {
158 progerr(gettext(ERR_FLT));
159 errflg++;
160 break;
161 } else if (errno != EINVAL) {
163 * some other error besides no value for this
164 * particular parameter
166 progerr(gettext(ERR_MESG), pkginst);
167 errflg++;
168 break;
170 if (!argv[optind])
171 break;
172 continue;
175 print_entry(param, value);
177 } while (!argv[optind] || (++optind < argc));
178 (void) pkgparam(NULL, NULL); /* close open FDs so umount won't fail */
180 (void) pkghead(NULL);
181 return (errflg ? 1 : 0);
184 static void
185 print_entry(char *param, char *value)
187 if (vflag) {
188 (void) printf("%s='", param);
189 while (*value) {
190 if (*value == '\'') {
191 (void) printf("'\"'\"'");
192 value++;
193 } else
194 (void) putchar(*value++);
196 (void) printf("'\n");
197 } else
198 (void) printf("%s\n", value);
201 void
202 quit(int retval)
204 exit(retval);