15c51f124SMoriah Waterland /*
25c51f124SMoriah Waterland * CDDL HEADER START
35c51f124SMoriah Waterland *
45c51f124SMoriah Waterland * The contents of this file are subject to the terms of the
55c51f124SMoriah Waterland * Common Development and Distribution License (the "License").
65c51f124SMoriah Waterland * You may not use this file except in compliance with the License.
75c51f124SMoriah Waterland *
85c51f124SMoriah Waterland * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95c51f124SMoriah Waterland * or http://www.opensolaris.org/os/licensing.
105c51f124SMoriah Waterland * See the License for the specific language governing permissions
115c51f124SMoriah Waterland * and limitations under the License.
125c51f124SMoriah Waterland *
135c51f124SMoriah Waterland * When distributing Covered Code, include this CDDL HEADER in each
145c51f124SMoriah Waterland * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155c51f124SMoriah Waterland * If applicable, add the following below this CDDL HEADER, with the
165c51f124SMoriah Waterland * fields enclosed by brackets "[]" replaced with your own identifying
175c51f124SMoriah Waterland * information: Portions Copyright [yyyy] [name of copyright owner]
185c51f124SMoriah Waterland *
195c51f124SMoriah Waterland * CDDL HEADER END
205c51f124SMoriah Waterland */
215c51f124SMoriah Waterland
222e10def1SPeter Tribble /*
232e10def1SPeter Tribble * Copyright (c) 2017 Peter Tribble.
242e10def1SPeter Tribble */
252e10def1SPeter Tribble
265c51f124SMoriah Waterland /*
275c51f124SMoriah Waterland * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
285c51f124SMoriah Waterland * Use is subject to license terms.
295c51f124SMoriah Waterland */
305c51f124SMoriah Waterland
315c51f124SMoriah Waterland /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
325c51f124SMoriah Waterland /* All Rights Reserved */
335c51f124SMoriah Waterland
345c51f124SMoriah Waterland
355c51f124SMoriah Waterland #define __EXTENTIONS__
365c51f124SMoriah Waterland
375c51f124SMoriah Waterland #include <stdio.h>
385c51f124SMoriah Waterland #include <limits.h>
395c51f124SMoriah Waterland #include <unistd.h>
405c51f124SMoriah Waterland #include <stdlib.h>
415c51f124SMoriah Waterland #include <locale.h>
425c51f124SMoriah Waterland #include <libintl.h>
435c51f124SMoriah Waterland #include <strings.h>
445c51f124SMoriah Waterland #include <string.h>
455c51f124SMoriah Waterland #include <dirent.h>
465c51f124SMoriah Waterland #include <sys/param.h>
475c51f124SMoriah Waterland #include <sys/stat.h>
485c51f124SMoriah Waterland #include <pkginfo.h>
495c51f124SMoriah Waterland #include <fcntl.h>
505c51f124SMoriah Waterland #include <sys/types.h>
515c51f124SMoriah Waterland #include <sys/stat.h>
525c51f124SMoriah Waterland #include <sys/param.h>
535c51f124SMoriah Waterland #include <sys/mman.h>
545c51f124SMoriah Waterland #include <pkgstrct.h>
555c51f124SMoriah Waterland #include <pkglocs.h>
565c51f124SMoriah Waterland #include <errno.h>
575c51f124SMoriah Waterland #include <ctype.h>
585c51f124SMoriah Waterland
595c51f124SMoriah Waterland #include <pkglib.h>
605c51f124SMoriah Waterland #include <instzones_api.h>
615c51f124SMoriah Waterland #include <libadm.h>
625c51f124SMoriah Waterland #include <libinst.h>
635c51f124SMoriah Waterland
645c51f124SMoriah Waterland extern char *pkgdir;
655c51f124SMoriah Waterland extern int pkginfofind(char *path, char *pkg_dir, char *pkginst);
665c51f124SMoriah Waterland
675c51f124SMoriah Waterland #define ERR_USAGE "usage:\n" \
685c51f124SMoriah Waterland "%s [-q] [-pi] [-x|l] [options] [pkg ...]\n" \
695c51f124SMoriah Waterland "%s -d device [-q] [-x|l] [options] [pkg ...]\n" \
705c51f124SMoriah Waterland "where\n" \
715c51f124SMoriah Waterland " -q #quiet mode\n" \
725c51f124SMoriah Waterland " -p #select partially installed packages\n" \
735c51f124SMoriah Waterland " -i #select completely installed packages\n" \
745c51f124SMoriah Waterland " -x #extracted listing\n" \
755c51f124SMoriah Waterland " -l #long listing\n" \
765c51f124SMoriah Waterland " -r #relocation base \n" \
775c51f124SMoriah Waterland "and options may include:\n" \
785c51f124SMoriah Waterland " -c category, [category...]\n" \
795c51f124SMoriah Waterland " -a architecture\n" \
805c51f124SMoriah Waterland " -v version\n"
815c51f124SMoriah Waterland
825c51f124SMoriah Waterland #define ERR_INCOMP0 "-L and -l/-x/-r flags are incompatible"
835c51f124SMoriah Waterland #define ERR_INCOMP1 "-l and -x/-r flags are not compatible"
845c51f124SMoriah Waterland #define ERR_INCOMP2 "-x and -l/-r flags are not compatible"
855c51f124SMoriah Waterland #define ERR_INCOMP3 "-r and -x/-x flags are not compatible"
865c51f124SMoriah Waterland #define ERR_NOINFO "ERROR: information for \"%s\" was not found"
875c51f124SMoriah Waterland #define ERR_NOPINFO "ERROR: No partial information for \"%s\" was found"
885c51f124SMoriah Waterland #define ERR_BADINFO "pkginfo file is corrupt or missing"
895c51f124SMoriah Waterland #define ERR_ROOT_SET "Could not set install root from the environment."
905c51f124SMoriah Waterland #define ERR_ROOT_CMD "Command line install root contends with environment."
915c51f124SMoriah Waterland
925c51f124SMoriah Waterland /* Format for dumping package attributes in dumpinfo() */
935c51f124SMoriah Waterland #define FMT "%10s: %s\n"
945c51f124SMoriah Waterland #define SFMT "%-11.11s %-*.*s %s\n"
955c51f124SMoriah Waterland #define CFMT "%*.*s "
965c51f124SMoriah Waterland #define XFMT "%-*.*s %s\n"
975c51f124SMoriah Waterland
985c51f124SMoriah Waterland #define nblock(size) ((size + (DEV_BSIZE - 1)) / DEV_BSIZE)
995c51f124SMoriah Waterland #define MAXCATG 64
1005c51f124SMoriah Waterland
1015c51f124SMoriah Waterland static char *device = NULL;
1025c51f124SMoriah Waterland static char *parmlst[] = {
1035c51f124SMoriah Waterland "DESC", "PSTAMP", "INSTDATE", "VSTOCK", "SERIALNUM", "HOTLINE",
1045c51f124SMoriah Waterland "EMAIL", NULL
1055c51f124SMoriah Waterland };
1065c51f124SMoriah Waterland
1075c51f124SMoriah Waterland static int errflg = 0;
1085c51f124SMoriah Waterland static int qflag = 0;
1095c51f124SMoriah Waterland static int iflag = -1;
1105c51f124SMoriah Waterland static int pflag = -1;
1115c51f124SMoriah Waterland static int lflag = 0;
1125c51f124SMoriah Waterland static int Lflag = 0;
1135c51f124SMoriah Waterland static int Nflag = 0;
1145c51f124SMoriah Waterland static int xflag = 0;
1155c51f124SMoriah Waterland static int rflag = 0; /* bug # 1081606 */
1165c51f124SMoriah Waterland static struct cfent entry;
1175c51f124SMoriah Waterland static char **pkg = NULL;
1185c51f124SMoriah Waterland static int pkgcnt = 0;
1195c51f124SMoriah Waterland static char *ckcatg[MAXCATG] = {NULL};
1205c51f124SMoriah Waterland static int ncatg = 0;
1215c51f124SMoriah Waterland static char *ckvers = NULL;
1225c51f124SMoriah Waterland static char *ckarch = NULL;
1235c51f124SMoriah Waterland
1245c51f124SMoriah Waterland static struct cfstat {
1255c51f124SMoriah Waterland char pkginst[32];
1265c51f124SMoriah Waterland short exec;
1275c51f124SMoriah Waterland short dirs;
1285c51f124SMoriah Waterland short link;
1295c51f124SMoriah Waterland short partial;
1305c51f124SMoriah Waterland long spooled;
1315c51f124SMoriah Waterland long installed;
1325c51f124SMoriah Waterland short info;
1335c51f124SMoriah Waterland short shared;
1345c51f124SMoriah Waterland short setuid;
1355c51f124SMoriah Waterland long tblks;
1365c51f124SMoriah Waterland struct cfstat *next;
1375c51f124SMoriah Waterland } *data;
1385c51f124SMoriah Waterland static struct pkginfo info;
1395c51f124SMoriah Waterland
1405c51f124SMoriah Waterland static struct cfstat *fpkg(char *pkginst);
1415c51f124SMoriah Waterland static int iscatg(char *list);
1425c51f124SMoriah Waterland static int selectp(char *p);
1435c51f124SMoriah Waterland static void usage(void), look_for_installed(void),
1445c51f124SMoriah Waterland report(void), rdcontents(void);
1455c51f124SMoriah Waterland static void pkgusage(struct cfstat *dp, struct cfent *pentry);
1465c51f124SMoriah Waterland static void getinfo(struct cfstat *dp);
1475c51f124SMoriah Waterland static void dumpinfo(struct cfstat *dp, int pkgLngth);
1485c51f124SMoriah Waterland
1495c51f124SMoriah Waterland int
main(int argc,char ** argv)1505c51f124SMoriah Waterland main(int argc, char **argv)
1515c51f124SMoriah Waterland {
1525c51f124SMoriah Waterland int c;
1535c51f124SMoriah Waterland
1545c51f124SMoriah Waterland pkgdir = NULL;
1555c51f124SMoriah Waterland setErrstr(NULL);
1565c51f124SMoriah Waterland
1575c51f124SMoriah Waterland /* initialize locale mechanism */
1585c51f124SMoriah Waterland
1595c51f124SMoriah Waterland (void) setlocale(LC_ALL, "");
1605c51f124SMoriah Waterland
1615c51f124SMoriah Waterland #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
1625c51f124SMoriah Waterland #define TEXT_DOMAIN "SYS_TEST"
1635c51f124SMoriah Waterland #endif
1645c51f124SMoriah Waterland (void) textdomain(TEXT_DOMAIN);
1655c51f124SMoriah Waterland
1665c51f124SMoriah Waterland /* determine program name */
1675c51f124SMoriah Waterland
1685c51f124SMoriah Waterland (void) set_prog_name(argv[0]);
1695c51f124SMoriah Waterland
1705c51f124SMoriah Waterland /* tell spmi zones interface how to access package output functions */
1715c51f124SMoriah Waterland
1725c51f124SMoriah Waterland z_set_output_functions(echo, echoDebug, progerr);
1735c51f124SMoriah Waterland
1745c51f124SMoriah Waterland /* establish installation root directory */
1755c51f124SMoriah Waterland
1765c51f124SMoriah Waterland if (!set_inst_root(getenv("PKG_INSTALL_ROOT"))) {
1775c51f124SMoriah Waterland progerr(gettext(ERR_ROOT_SET));
1785c51f124SMoriah Waterland exit(1);
1795c51f124SMoriah Waterland }
1805c51f124SMoriah Waterland
1815c51f124SMoriah Waterland while ((c = getopt(argc, argv, "LNR:xv:a:d:qrpilc:?")) != EOF) {
1825c51f124SMoriah Waterland switch (c) {
1832e10def1SPeter Tribble case 'v':
1845c51f124SMoriah Waterland ckvers = optarg;
1855c51f124SMoriah Waterland break;
1865c51f124SMoriah Waterland
1872e10def1SPeter Tribble case 'a':
1885c51f124SMoriah Waterland ckarch = optarg;
1895c51f124SMoriah Waterland break;
1905c51f124SMoriah Waterland
1912e10def1SPeter Tribble case 'd':
1925c51f124SMoriah Waterland /* -d could specify stream or mountable device */
1935c51f124SMoriah Waterland device = flex_device(optarg, 1);
1945c51f124SMoriah Waterland break;
1955c51f124SMoriah Waterland
1962e10def1SPeter Tribble case 'q':
1975c51f124SMoriah Waterland qflag++;
1985c51f124SMoriah Waterland break;
1995c51f124SMoriah Waterland
2002e10def1SPeter Tribble case 'i':
2015c51f124SMoriah Waterland iflag = 1;
2025c51f124SMoriah Waterland if (pflag > 0)
2035c51f124SMoriah Waterland usage();
2045c51f124SMoriah Waterland pflag = 0;
2055c51f124SMoriah Waterland break;
2065c51f124SMoriah Waterland
2072e10def1SPeter Tribble case 'p':
2085c51f124SMoriah Waterland pflag = 1;
2095c51f124SMoriah Waterland if (iflag > 0)
2105c51f124SMoriah Waterland usage();
2115c51f124SMoriah Waterland iflag = 0;
2125c51f124SMoriah Waterland break;
2135c51f124SMoriah Waterland
2142e10def1SPeter Tribble case 'N':
2155c51f124SMoriah Waterland Nflag++;
2165c51f124SMoriah Waterland break;
2175c51f124SMoriah Waterland
2182e10def1SPeter Tribble case 'L':
2195c51f124SMoriah Waterland if (xflag || lflag || rflag) {
2205c51f124SMoriah Waterland progerr(gettext(ERR_INCOMP0));
2215c51f124SMoriah Waterland usage();
2225c51f124SMoriah Waterland }
2235c51f124SMoriah Waterland Lflag++;
2245c51f124SMoriah Waterland break;
2255c51f124SMoriah Waterland
2262e10def1SPeter Tribble case 'l':
2275c51f124SMoriah Waterland if (xflag || rflag) {
2285c51f124SMoriah Waterland progerr(gettext(ERR_INCOMP1));
2295c51f124SMoriah Waterland usage();
2305c51f124SMoriah Waterland }
2315c51f124SMoriah Waterland lflag++;
2325c51f124SMoriah Waterland break;
2335c51f124SMoriah Waterland
2342e10def1SPeter Tribble case 'x':
2355c51f124SMoriah Waterland /* bug # 1081606 */
2365c51f124SMoriah Waterland if (lflag || rflag) {
2375c51f124SMoriah Waterland progerr(gettext(ERR_INCOMP2));
2385c51f124SMoriah Waterland usage();
2395c51f124SMoriah Waterland }
2405c51f124SMoriah Waterland xflag++;
2415c51f124SMoriah Waterland break;
2425c51f124SMoriah Waterland
2432e10def1SPeter Tribble case 'r':
2445c51f124SMoriah Waterland if (lflag || xflag || Lflag) {
2455c51f124SMoriah Waterland progerr(gettext(ERR_INCOMP0));
2465c51f124SMoriah Waterland usage();
2475c51f124SMoriah Waterland }
2485c51f124SMoriah Waterland rflag++;
2495c51f124SMoriah Waterland break;
2505c51f124SMoriah Waterland
2512e10def1SPeter Tribble case 'c':
2525c51f124SMoriah Waterland ckcatg[ncatg++] = strtok(optarg, " \t\n, ");
2535c51f124SMoriah Waterland while (ckcatg[ncatg] = strtok(NULL, " \t\n, "))
2545c51f124SMoriah Waterland ncatg++;
2555c51f124SMoriah Waterland break;
2565c51f124SMoriah Waterland
2575c51f124SMoriah Waterland /* added for newroot functions */
2582e10def1SPeter Tribble case 'R':
2595c51f124SMoriah Waterland if (!set_inst_root(optarg)) {
2605c51f124SMoriah Waterland progerr(gettext(ERR_ROOT_CMD));
2615c51f124SMoriah Waterland exit(1);
2625c51f124SMoriah Waterland }
2635c51f124SMoriah Waterland break;
2645c51f124SMoriah Waterland
2652e10def1SPeter Tribble default:
2665c51f124SMoriah Waterland usage();
2675c51f124SMoriah Waterland }
2685c51f124SMoriah Waterland }
2695c51f124SMoriah Waterland
2705c51f124SMoriah Waterland /*
2715c51f124SMoriah Waterland * implement the newroot option
2725c51f124SMoriah Waterland */
2735c51f124SMoriah Waterland set_PKGpaths(get_inst_root()); /* set up /var... directories */
2745c51f124SMoriah Waterland
2755c51f124SMoriah Waterland /*
2765c51f124SMoriah Waterland * Open the install DB, if one exists.
2775c51f124SMoriah Waterland */
2785c51f124SMoriah Waterland
2795c51f124SMoriah Waterland pkg = &argv[optind];
2805c51f124SMoriah Waterland pkgcnt = (argc - optind);
2815c51f124SMoriah Waterland
282*c57b7750SToomas Soome if (pkg[0] && strcmp(pkg[0], "all") == 0) {
2835c51f124SMoriah Waterland pkgcnt = 0;
2845c51f124SMoriah Waterland pkg[0] = NULL;
2855c51f124SMoriah Waterland }
2865c51f124SMoriah Waterland
2875c51f124SMoriah Waterland if (pkgdir == NULL)
2885c51f124SMoriah Waterland pkgdir = get_PKGLOC(); /* we need this later */
2895c51f124SMoriah Waterland
2905c51f124SMoriah Waterland /* convert device appropriately */
2915c51f124SMoriah Waterland if (pkghead(device))
2925c51f124SMoriah Waterland exit(1);
2935c51f124SMoriah Waterland
2945c51f124SMoriah Waterland /*
2955c51f124SMoriah Waterland * If we are to inspect a spooled package we are only interested in
29662224350SCasper H.S. Dik * the pkginfo file in the spooled pkg. We have a spooled pkg if
2975c51f124SMoriah Waterland * device is not NULL.
2985c51f124SMoriah Waterland */
2995c51f124SMoriah Waterland
3005c51f124SMoriah Waterland look_for_installed();
3015c51f124SMoriah Waterland
30262224350SCasper H.S. Dik if (lflag && strcmp(pkgdir, get_PKGLOC()) == 0) {
3035c51f124SMoriah Waterland /* look at contents file */
3045c51f124SMoriah Waterland rdcontents();
3055c51f124SMoriah Waterland
3065c51f124SMoriah Waterland }
3075c51f124SMoriah Waterland
3085c51f124SMoriah Waterland /*
3095c51f124SMoriah Waterland * If we are to inspect a spooled package we are only interested in
3105c51f124SMoriah Waterland * the pkginfo file in the spooled pkg so we skip any Reg 4 DB
3115c51f124SMoriah Waterland * lookups and use the old algorithm. We have a spooled pkg if
3125c51f124SMoriah Waterland * device is not NULL.
3135c51f124SMoriah Waterland */
3145c51f124SMoriah Waterland
3155c51f124SMoriah Waterland report();
3165c51f124SMoriah Waterland
3175c51f124SMoriah Waterland (void) pkghead(NULL);
3185c51f124SMoriah Waterland
3195c51f124SMoriah Waterland return (errflg ? 1 : 0);
3205c51f124SMoriah Waterland }
3215c51f124SMoriah Waterland
3225c51f124SMoriah Waterland static void
report(void)3235c51f124SMoriah Waterland report(void)
3245c51f124SMoriah Waterland {
3255c51f124SMoriah Waterland struct cfstat *dp, *choice;
3265c51f124SMoriah Waterland int i;
3275c51f124SMoriah Waterland int pkgLgth = 0;
3285c51f124SMoriah Waterland int longestPkg = 0;
3295c51f124SMoriah Waterland boolean_t output = B_FALSE;
3305c51f124SMoriah Waterland
3315c51f124SMoriah Waterland for (;;) {
3325c51f124SMoriah Waterland choice = (struct cfstat *)0;
3335c51f124SMoriah Waterland for (dp = data; dp; dp = dp->next) {
3345c51f124SMoriah Waterland pkgLgth = strlen(dp->pkginst);
3355c51f124SMoriah Waterland if (pkgLgth > longestPkg)
3365c51f124SMoriah Waterland longestPkg = pkgLgth;
3375c51f124SMoriah Waterland }
3385c51f124SMoriah Waterland for (dp = data; dp; dp = dp->next) {
3395c51f124SMoriah Waterland /* get information about this package */
3405c51f124SMoriah Waterland if (dp->installed < 0)
3415c51f124SMoriah Waterland continue; /* already used */
3425c51f124SMoriah Waterland if (Lflag && pkgcnt) {
3435c51f124SMoriah Waterland choice = dp;
3445c51f124SMoriah Waterland break;
3455c51f124SMoriah Waterland } else if (!choice ||
3465c51f124SMoriah Waterland (strcmp(choice->pkginst, dp->pkginst) > 0))
3475c51f124SMoriah Waterland choice = dp;
3485c51f124SMoriah Waterland }
3495c51f124SMoriah Waterland if (!choice)
3505c51f124SMoriah Waterland break; /* no more packages */
3515c51f124SMoriah Waterland
3525c51f124SMoriah Waterland if (pkginfo(&info, choice->pkginst, ckarch, ckvers)) {
3535c51f124SMoriah Waterland choice->installed = (-1);
3545c51f124SMoriah Waterland continue;
3555c51f124SMoriah Waterland }
3565c51f124SMoriah Waterland
3575c51f124SMoriah Waterland /*
3585c51f124SMoriah Waterland * Confirm that the pkginfo file contains the
3595c51f124SMoriah Waterland * required information.
3605c51f124SMoriah Waterland */
361*c57b7750SToomas Soome if (info.name == NULL || *(info.name) == '\0' ||
362*c57b7750SToomas Soome info.arch == NULL || *(info.arch) == '\0' ||
363*c57b7750SToomas Soome info.version == NULL || *(info.version) == '\0' ||
364*c57b7750SToomas Soome info.catg == NULL || *(info.catg) == '\0') {
3655c51f124SMoriah Waterland progerr(gettext(ERR_BADINFO));
3665c51f124SMoriah Waterland errflg++;
3675c51f124SMoriah Waterland return;
3685c51f124SMoriah Waterland }
3695c51f124SMoriah Waterland
3705c51f124SMoriah Waterland /* is it in an appropriate catgory? */
3715c51f124SMoriah Waterland if (iscatg(info.catg)) {
3725c51f124SMoriah Waterland choice->installed = (-1);
3735c51f124SMoriah Waterland continue;
3745c51f124SMoriah Waterland }
3755c51f124SMoriah Waterland
3765c51f124SMoriah Waterland if (!pflag &&
3772e10def1SPeter Tribble (choice->partial || (info.status == PI_PARTIAL) ||
3782e10def1SPeter Tribble (info.status == PI_UNKNOWN))) {
3795c51f124SMoriah Waterland /* don't include partially installed packages */
3805c51f124SMoriah Waterland choice->installed = (-1);
3815c51f124SMoriah Waterland continue;
3825c51f124SMoriah Waterland }
3835c51f124SMoriah Waterland
3849ab815e1SGarrett D'Amore if (!iflag && (info.status == PI_INSTALLED)) {
3855c51f124SMoriah Waterland /* don't include completely installed packages */
3865c51f124SMoriah Waterland choice->installed = (-1);
3875c51f124SMoriah Waterland continue;
3885c51f124SMoriah Waterland }
3895c51f124SMoriah Waterland
3905c51f124SMoriah Waterland output = B_TRUE;
3915c51f124SMoriah Waterland dumpinfo(choice, longestPkg);
3925c51f124SMoriah Waterland choice->installed = (-1);
3935c51f124SMoriah Waterland if (pkgcnt) {
3945c51f124SMoriah Waterland i = selectp(choice->pkginst);
3955c51f124SMoriah Waterland if (i >= 0)
3965c51f124SMoriah Waterland pkg[i] = NULL;
3975c51f124SMoriah Waterland else {
3985c51f124SMoriah Waterland if (qflag) {
3995c51f124SMoriah Waterland errflg++;
4005c51f124SMoriah Waterland return;
4015c51f124SMoriah Waterland }
4025c51f124SMoriah Waterland }
4035c51f124SMoriah Waterland }
4045c51f124SMoriah Waterland }
4055c51f124SMoriah Waterland
4065c51f124SMoriah Waterland /* If no package matched and no output produced set error flag */
4075c51f124SMoriah Waterland if (!output)
4085c51f124SMoriah Waterland errflg++;
4095c51f124SMoriah Waterland
4105c51f124SMoriah Waterland /* verify that each package listed on command line was output */
4115c51f124SMoriah Waterland for (i = 0; i < pkgcnt; ++i) {
4125c51f124SMoriah Waterland if (pkg[i]) {
4135c51f124SMoriah Waterland errflg++;
4145c51f124SMoriah Waterland if (!qflag) {
4155c51f124SMoriah Waterland if (pflag == 1)
4165c51f124SMoriah Waterland logerr(gettext(ERR_NOPINFO), pkg[i]);
4175c51f124SMoriah Waterland else
4185c51f124SMoriah Waterland logerr(gettext(ERR_NOINFO), pkg[i]);
4195c51f124SMoriah Waterland } else
4205c51f124SMoriah Waterland return;
4215c51f124SMoriah Waterland }
4225c51f124SMoriah Waterland }
4235c51f124SMoriah Waterland (void) pkginfo(&info, NULL); /* free up all memory and open fds */
4245c51f124SMoriah Waterland }
4255c51f124SMoriah Waterland
4265c51f124SMoriah Waterland static void
dumpinfo(struct cfstat * dp,int pkgLngth)4275c51f124SMoriah Waterland dumpinfo(struct cfstat *dp, int pkgLngth)
4285c51f124SMoriah Waterland {
4295c51f124SMoriah Waterland register int i;
4305c51f124SMoriah Waterland char *pt;
4315c51f124SMoriah Waterland char category[128];
4325c51f124SMoriah Waterland
4335c51f124SMoriah Waterland if (qflag) {
4345c51f124SMoriah Waterland return; /* print nothing */
4355c51f124SMoriah Waterland }
4365c51f124SMoriah Waterland
4375c51f124SMoriah Waterland if (rflag) {
4385c51f124SMoriah Waterland (void) puts((info.basedir) ? info.basedir : "none");
4395c51f124SMoriah Waterland return;
4405c51f124SMoriah Waterland }
4415c51f124SMoriah Waterland
4425c51f124SMoriah Waterland if (Lflag) {
4435c51f124SMoriah Waterland (void) puts(info.pkginst);
4445c51f124SMoriah Waterland return;
4455c51f124SMoriah Waterland } else if (xflag) {
4465c51f124SMoriah Waterland (void) printf(XFMT, pkgLngth, pkgLngth, info.pkginst,
4475c51f124SMoriah Waterland info.name);
4485c51f124SMoriah Waterland
4495c51f124SMoriah Waterland if (info.arch || info.version) {
4505c51f124SMoriah Waterland (void) printf(CFMT, pkgLngth, pkgLngth, "");
4515c51f124SMoriah Waterland if (info.arch)
4525c51f124SMoriah Waterland (void) printf("(%s) ", info.arch);
4535c51f124SMoriah Waterland if (info.version)
4545c51f124SMoriah Waterland (void) printf("%s", info.version);
4555c51f124SMoriah Waterland (void) printf("\n");
4565c51f124SMoriah Waterland }
4575c51f124SMoriah Waterland return;
4585c51f124SMoriah Waterland } else if (!lflag) {
4595c51f124SMoriah Waterland if (info.catg) {
4605c51f124SMoriah Waterland (void) sscanf(info.catg, "%[^, \t\n]", category);
4615c51f124SMoriah Waterland } else {
4625c51f124SMoriah Waterland (void) strcpy(category, "(unknown)");
4635c51f124SMoriah Waterland }
4645c51f124SMoriah Waterland (void) printf(SFMT, category, pkgLngth, pkgLngth, info.pkginst,
4655c51f124SMoriah Waterland info.name);
4665c51f124SMoriah Waterland return;
4675c51f124SMoriah Waterland }
4685c51f124SMoriah Waterland if (info.pkginst)
4695c51f124SMoriah Waterland (void) printf(FMT, "PKGINST", info.pkginst);
4705c51f124SMoriah Waterland if (info.name)
4715c51f124SMoriah Waterland (void) printf(FMT, "NAME", info.name);
4725c51f124SMoriah Waterland if (lflag && info.catg)
4735c51f124SMoriah Waterland (void) printf(FMT, "CATEGORY", info.catg);
4745c51f124SMoriah Waterland if (lflag && info.arch)
4755c51f124SMoriah Waterland (void) printf(FMT, "ARCH", info.arch);
4765c51f124SMoriah Waterland if (info.version)
4775c51f124SMoriah Waterland (void) printf(FMT, "VERSION", info.version);
4785c51f124SMoriah Waterland if (info.basedir)
4795c51f124SMoriah Waterland (void) printf(FMT, "BASEDIR", info.basedir);
4805c51f124SMoriah Waterland if (info.vendor)
4815c51f124SMoriah Waterland (void) printf(FMT, "VENDOR", info.vendor);
4825c51f124SMoriah Waterland
4839ab815e1SGarrett D'Amore for (i = 0; parmlst[i]; ++i) {
4849ab815e1SGarrett D'Amore if ((pt = pkgparam(info.pkginst, parmlst[i])) != NULL && *pt)
4859ab815e1SGarrett D'Amore (void) printf(FMT, parmlst[i], pt);
4865c51f124SMoriah Waterland }
4879ab815e1SGarrett D'Amore if (info.status == PI_SPOOLED)
4889ab815e1SGarrett D'Amore (void) printf(FMT, "STATUS", gettext("spooled"));
4899ab815e1SGarrett D'Amore else if (info.status == PI_PARTIAL)
4909ab815e1SGarrett D'Amore (void) printf(FMT, "STATUS",
4919ab815e1SGarrett D'Amore gettext("partially installed"));
4929ab815e1SGarrett D'Amore else if (info.status == PI_INSTALLED)
4939ab815e1SGarrett D'Amore (void) printf(FMT, "STATUS",
4949ab815e1SGarrett D'Amore gettext("completely installed"));
4959ab815e1SGarrett D'Amore else
4969ab815e1SGarrett D'Amore (void) printf(FMT, "STATUS", gettext("(unknown)"));
4979ab815e1SGarrett D'Amore
4985c51f124SMoriah Waterland (void) pkgparam(NULL, NULL);
4995c51f124SMoriah Waterland
5005c51f124SMoriah Waterland if (!lflag) {
5015c51f124SMoriah Waterland (void) putchar('\n');
5025c51f124SMoriah Waterland return;
5035c51f124SMoriah Waterland }
5045c51f124SMoriah Waterland
5059ab815e1SGarrett D'Amore if (strcmp(pkgdir, get_PKGLOC()))
5069ab815e1SGarrett D'Amore getinfo(dp);
5079ab815e1SGarrett D'Amore
5089ab815e1SGarrett D'Amore if (dp->spooled)
5099ab815e1SGarrett D'Amore (void) printf(gettext("%10s: %7ld spooled pathnames\n"),
5109ab815e1SGarrett D'Amore "FILES", dp->spooled);
5119ab815e1SGarrett D'Amore if (dp->installed)
5129ab815e1SGarrett D'Amore (void) printf(gettext("%10s: %7ld installed pathnames\n"),
5139ab815e1SGarrett D'Amore "FILES", dp->installed);
5149ab815e1SGarrett D'Amore if (dp->partial)
5159ab815e1SGarrett D'Amore (void) printf(gettext("%20d partially installed pathnames\n"),
5169ab815e1SGarrett D'Amore dp->partial);
5179ab815e1SGarrett D'Amore if (dp->shared)
5189ab815e1SGarrett D'Amore (void) printf(gettext("%20d shared pathnames\n"), dp->shared);
5199ab815e1SGarrett D'Amore if (dp->link)
5209ab815e1SGarrett D'Amore (void) printf(gettext("%20d linked files\n"), dp->link);
5219ab815e1SGarrett D'Amore if (dp->dirs)
5229ab815e1SGarrett D'Amore (void) printf(gettext("%20d directories\n"), dp->dirs);
5239ab815e1SGarrett D'Amore if (dp->exec)
5249ab815e1SGarrett D'Amore (void) printf(gettext("%20d executables\n"), dp->exec);
5259ab815e1SGarrett D'Amore if (dp->setuid)
5269ab815e1SGarrett D'Amore (void) printf(gettext("%20d setuid/setgid executables\n"),
5279ab815e1SGarrett D'Amore dp->setuid);
5289ab815e1SGarrett D'Amore if (dp->info)
5299ab815e1SGarrett D'Amore (void) printf(gettext("%20d package information files\n"),
5309ab815e1SGarrett D'Amore dp->info+1); /* pkgmap counts! */
5319ab815e1SGarrett D'Amore
5329ab815e1SGarrett D'Amore if (dp->tblks)
5339ab815e1SGarrett D'Amore (void) printf(gettext("%20ld blocks used (approx)\n"),
5349ab815e1SGarrett D'Amore dp->tblks);
5359ab815e1SGarrett D'Amore
5365c51f124SMoriah Waterland (void) putchar('\n');
5375c51f124SMoriah Waterland }
5385c51f124SMoriah Waterland
5395c51f124SMoriah Waterland static struct cfstat *
fpkg(char * pkginst)5405c51f124SMoriah Waterland fpkg(char *pkginst)
5415c51f124SMoriah Waterland {
5425c51f124SMoriah Waterland struct cfstat *dp, *last;
5435c51f124SMoriah Waterland
5445c51f124SMoriah Waterland dp = data;
545*c57b7750SToomas Soome last = NULL;
5465c51f124SMoriah Waterland while (dp) {
547*c57b7750SToomas Soome if (strcmp(dp->pkginst, pkginst) == 0)
5485c51f124SMoriah Waterland return (dp);
5495c51f124SMoriah Waterland last = dp;
5505c51f124SMoriah Waterland dp = dp->next;
5515c51f124SMoriah Waterland }
5525c51f124SMoriah Waterland dp = (struct cfstat *)calloc(1, sizeof (struct cfstat));
5535c51f124SMoriah Waterland if (!dp) {
5545c51f124SMoriah Waterland progerr(gettext("no memory, malloc() failed"));
5555c51f124SMoriah Waterland exit(1);
5565c51f124SMoriah Waterland }
5575c51f124SMoriah Waterland if (!last)
5585c51f124SMoriah Waterland data = dp;
5595c51f124SMoriah Waterland else
5605c51f124SMoriah Waterland last->next = dp; /* link list */
5615c51f124SMoriah Waterland (void) strcpy(dp->pkginst, pkginst);
5625c51f124SMoriah Waterland return (dp);
5635c51f124SMoriah Waterland }
5645c51f124SMoriah Waterland
5655c51f124SMoriah Waterland #define SEPAR ','
5665c51f124SMoriah Waterland
5675c51f124SMoriah Waterland static int
iscatg(char * list)5685c51f124SMoriah Waterland iscatg(char *list)
5695c51f124SMoriah Waterland {
5705c51f124SMoriah Waterland register int i;
5715c51f124SMoriah Waterland register char *pt;
5725c51f124SMoriah Waterland int match;
5735c51f124SMoriah Waterland
5745c51f124SMoriah Waterland if (!ckcatg[0])
5755c51f124SMoriah Waterland return (0); /* no specification implies all packages */
5769ab815e1SGarrett D'Amore
5775c51f124SMoriah Waterland if (!list)
5785c51f124SMoriah Waterland return (1); /* no category specified in pkginfo is a bug */
5795c51f124SMoriah Waterland
5805c51f124SMoriah Waterland match = 0;
5815c51f124SMoriah Waterland do {
5825c51f124SMoriah Waterland if (pt = strchr(list, ','))
5835c51f124SMoriah Waterland *pt = '\0';
5845c51f124SMoriah Waterland
5855c51f124SMoriah Waterland for (i = 0; ckcatg[i]; /* void */) {
5865c51f124SMoriah Waterland /* bug id 1081607 */
5875c51f124SMoriah Waterland if (!strcasecmp(list, ckcatg[i++])) {
5885c51f124SMoriah Waterland match++;
5895c51f124SMoriah Waterland break;
5905c51f124SMoriah Waterland }
5915c51f124SMoriah Waterland }
5925c51f124SMoriah Waterland
5935c51f124SMoriah Waterland if (pt)
5945c51f124SMoriah Waterland *pt++ = ',';
5955c51f124SMoriah Waterland if (match)
5965c51f124SMoriah Waterland return (0);
5975c51f124SMoriah Waterland list = pt; /* points to next one */
5985c51f124SMoriah Waterland } while (pt);
5995c51f124SMoriah Waterland return (1);
6005c51f124SMoriah Waterland }
6015c51f124SMoriah Waterland
6025c51f124SMoriah Waterland static void
look_for_installed(void)6035c51f124SMoriah Waterland look_for_installed(void)
6045c51f124SMoriah Waterland {
6055c51f124SMoriah Waterland struct dirent *drp;
6065c51f124SMoriah Waterland DIR *dirfp;
6075c51f124SMoriah Waterland char path[PATH_MAX];
6085c51f124SMoriah Waterland
6095c51f124SMoriah Waterland if ((dirfp = opendir(pkgdir)) == NULL)
6105c51f124SMoriah Waterland return;
6115c51f124SMoriah Waterland
6125c51f124SMoriah Waterland while (drp = readdir(dirfp)) {
6135c51f124SMoriah Waterland if (drp->d_name[0] == '.')
6145c51f124SMoriah Waterland continue;
6155c51f124SMoriah Waterland
6165c51f124SMoriah Waterland if (pkgcnt && (selectp(drp->d_name) < 0))
6175c51f124SMoriah Waterland continue;
6185c51f124SMoriah Waterland
6195c51f124SMoriah Waterland if (!pkginfofind(path, pkgdir, drp->d_name))
6205c51f124SMoriah Waterland continue; /* doesn't appear to be a package */
6215c51f124SMoriah Waterland
6225c51f124SMoriah Waterland (void) fpkg(drp->d_name);
6235c51f124SMoriah Waterland }
6245c51f124SMoriah Waterland (void) closedir(dirfp);
6255c51f124SMoriah Waterland }
6265c51f124SMoriah Waterland
6275c51f124SMoriah Waterland static int
selectp(char * p)6285c51f124SMoriah Waterland selectp(char *p)
6295c51f124SMoriah Waterland {
6305c51f124SMoriah Waterland register int i;
6315c51f124SMoriah Waterland
6325c51f124SMoriah Waterland for (i = 0; i < pkgcnt; ++i) {
6335c51f124SMoriah Waterland if (pkg[i] && pkgnmchk(p, pkg[i], 1) == 0)
6345c51f124SMoriah Waterland return (i);
6355c51f124SMoriah Waterland }
6365c51f124SMoriah Waterland return (-1);
6375c51f124SMoriah Waterland }
6385c51f124SMoriah Waterland
6395c51f124SMoriah Waterland static void
rdcontents(void)6405c51f124SMoriah Waterland rdcontents(void)
6415c51f124SMoriah Waterland {
6425c51f124SMoriah Waterland struct cfstat *dp;
6435c51f124SMoriah Waterland struct pinfo *pinfo;
6445c51f124SMoriah Waterland int n;
64562224350SCasper H.S. Dik PKGserver server;
6465c51f124SMoriah Waterland
64762224350SCasper H.S. Dik if (!socfile(&server, B_TRUE) ||
64862224350SCasper H.S. Dik pkgopenfilter(server, pkgcnt == 1 ? pkg[0] : NULL) != 0)
6495c51f124SMoriah Waterland exit(1);
6505c51f124SMoriah Waterland
6515c51f124SMoriah Waterland /* check the contents file to look for referenced packages */
65262224350SCasper H.S. Dik while ((n = srchcfile(&entry, "*", server)) > 0) {
6535c51f124SMoriah Waterland for (pinfo = entry.pinfo; pinfo; pinfo = pinfo->next) {
6545c51f124SMoriah Waterland /* see if entry is used by indicated packaged */
6555c51f124SMoriah Waterland if (pkgcnt && (selectp(pinfo->pkg) < 0))
6565c51f124SMoriah Waterland continue;
6575c51f124SMoriah Waterland
6585c51f124SMoriah Waterland dp = fpkg(pinfo->pkg);
6595c51f124SMoriah Waterland pkgusage(dp, &entry);
6605c51f124SMoriah Waterland
6615c51f124SMoriah Waterland if (entry.npkgs > 1)
6625c51f124SMoriah Waterland dp->shared++;
6635c51f124SMoriah Waterland
6645c51f124SMoriah Waterland /*
6655c51f124SMoriah Waterland * Only objects specifically tagged with '!' event
6665c51f124SMoriah Waterland * character are considered "partial", everything
6675c51f124SMoriah Waterland * else is considered "installed" (even server
6685c51f124SMoriah Waterland * objects).
6695c51f124SMoriah Waterland */
6705c51f124SMoriah Waterland switch (pinfo->status) {
6715c51f124SMoriah Waterland case '!' :
6725c51f124SMoriah Waterland dp->partial++;
6735c51f124SMoriah Waterland break;
6745c51f124SMoriah Waterland default :
6755c51f124SMoriah Waterland dp->installed++;
6765c51f124SMoriah Waterland break;
6775c51f124SMoriah Waterland }
6785c51f124SMoriah Waterland }
6795c51f124SMoriah Waterland }
6805c51f124SMoriah Waterland if (n < 0) {
6815c51f124SMoriah Waterland char *errstr = getErrstr();
6825c51f124SMoriah Waterland progerr(gettext("bad entry read in contents file"));
6835c51f124SMoriah Waterland logerr(gettext("pathname: %s"),
6845c51f124SMoriah Waterland (entry.path && *entry.path) ? entry.path : "Unknown");
6855c51f124SMoriah Waterland logerr(gettext("problem: %s"),
6865c51f124SMoriah Waterland (errstr && *errstr) ? errstr : "Unknown");
6875c51f124SMoriah Waterland exit(1);
6885c51f124SMoriah Waterland }
68962224350SCasper H.S. Dik pkgcloseserver(server);
6905c51f124SMoriah Waterland }
6915c51f124SMoriah Waterland
6925c51f124SMoriah Waterland static void
getinfo(struct cfstat * dp)6935c51f124SMoriah Waterland getinfo(struct cfstat *dp)
6945c51f124SMoriah Waterland {
6955c51f124SMoriah Waterland int n;
6965c51f124SMoriah Waterland char pkgmap[MAXPATHLEN];
6975c51f124SMoriah Waterland VFP_T *vfp;
6985c51f124SMoriah Waterland
6995c51f124SMoriah Waterland (void) snprintf(pkgmap, sizeof (pkgmap),
7002e10def1SPeter Tribble "%s/%s/pkgmap", pkgdir, dp->pkginst);
7015c51f124SMoriah Waterland
7025c51f124SMoriah Waterland if (vfpOpen(&vfp, pkgmap, "r", VFP_NEEDNOW) != 0) {
7035c51f124SMoriah Waterland progerr(gettext("unable open \"%s\" for reading"), pkgmap);
7045c51f124SMoriah Waterland exit(1);
7055c51f124SMoriah Waterland }
7065c51f124SMoriah Waterland
7075c51f124SMoriah Waterland dp->spooled = 1; /* pkgmap counts! */
7085c51f124SMoriah Waterland
7095c51f124SMoriah Waterland while ((n = gpkgmapvfp(&entry, vfp)) > 0) {
7105c51f124SMoriah Waterland dp->spooled++;
7115c51f124SMoriah Waterland pkgusage(dp, &entry);
7125c51f124SMoriah Waterland }
7135c51f124SMoriah Waterland
7145c51f124SMoriah Waterland if (n < 0) {
7155c51f124SMoriah Waterland char *errstr = getErrstr();
7165c51f124SMoriah Waterland progerr(gettext("bad entry read in pkgmap file"));
7175c51f124SMoriah Waterland logerr(gettext("pathname: %s"),
7185c51f124SMoriah Waterland (entry.path && *entry.path) ? entry.path : "Unknown");
7195c51f124SMoriah Waterland logerr(gettext("problem: %s"),
7205c51f124SMoriah Waterland (errstr && *errstr) ? errstr : "Unknown");
7215c51f124SMoriah Waterland exit(1);
7225c51f124SMoriah Waterland }
7235c51f124SMoriah Waterland
7245c51f124SMoriah Waterland (void) vfpClose(&vfp);
7255c51f124SMoriah Waterland }
7265c51f124SMoriah Waterland
7275c51f124SMoriah Waterland static void
pkgusage(struct cfstat * dp,struct cfent * pentry)7285c51f124SMoriah Waterland pkgusage(struct cfstat *dp, struct cfent *pentry)
7295c51f124SMoriah Waterland {
7305c51f124SMoriah Waterland if (pentry->ftype == 'i') {
7315c51f124SMoriah Waterland dp->info++;
7325c51f124SMoriah Waterland return;
7335c51f124SMoriah Waterland } else if (pentry->ftype == 'l') {
7345c51f124SMoriah Waterland dp->link++;
7355c51f124SMoriah Waterland } else {
7365c51f124SMoriah Waterland if ((pentry->ftype == 'd') || (pentry->ftype == 'x'))
7375c51f124SMoriah Waterland dp->dirs++;
7385c51f124SMoriah Waterland
7395c51f124SMoriah Waterland /* Only collect mode stats if they would be meaningful. */
7405c51f124SMoriah Waterland if (pentry->ainfo.mode != BADMODE) {
7415c51f124SMoriah Waterland if (pentry->ainfo.mode & 06000)
7425c51f124SMoriah Waterland dp->setuid++;
7435c51f124SMoriah Waterland if (!strchr("dxcbp", pentry->ftype) &&
7442e10def1SPeter Tribble (pentry->ainfo.mode & 0111))
7455c51f124SMoriah Waterland dp->exec++;
7465c51f124SMoriah Waterland }
7475c51f124SMoriah Waterland }
7485c51f124SMoriah Waterland
7495c51f124SMoriah Waterland if (strchr("ifve", pentry->ftype))
7505c51f124SMoriah Waterland dp->tblks += nblock(pentry->cinfo.size);
7515c51f124SMoriah Waterland }
7525c51f124SMoriah Waterland
7535c51f124SMoriah Waterland static void
usage(void)7545c51f124SMoriah Waterland usage(void)
7555c51f124SMoriah Waterland {
7565c51f124SMoriah Waterland char *prog = get_prog_name();
7575c51f124SMoriah Waterland
7585c51f124SMoriah Waterland /* bug # 1081606 */
7595c51f124SMoriah Waterland (void) fprintf(stderr, gettext(ERR_USAGE), prog, prog);
7605c51f124SMoriah Waterland
7615c51f124SMoriah Waterland exit(1);
7625c51f124SMoriah Waterland }
7635c51f124SMoriah Waterland
7645c51f124SMoriah Waterland void
quit(int retval)7655c51f124SMoriah Waterland quit(int retval)
7665c51f124SMoriah Waterland {
7675c51f124SMoriah Waterland exit(retval);
7685c51f124SMoriah Waterland }
769