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