17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
23*47644099Sgt29601 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24*47644099Sgt29601 * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <locale.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #define MAIN 1
367c478bd9Sstevel@tonic-gate #include "rules.h"
377c478bd9Sstevel@tonic-gate #include "elfrd.h"
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate int verbose = 0;
407c478bd9Sstevel@tonic-gate struct libpath *libp, libp_hd;
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)437c478bd9Sstevel@tonic-gate main(int argc, char **argv)
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate int prtfn();
467c478bd9Sstevel@tonic-gate int packfn();
477c478bd9Sstevel@tonic-gate int unpackfn();
487c478bd9Sstevel@tonic-gate int inquirefn();
497c478bd9Sstevel@tonic-gate FILE *open_rulesfile();
507c478bd9Sstevel@tonic-gate FILE *rfd;
517c478bd9Sstevel@tonic-gate int c;
527c478bd9Sstevel@tonic-gate int fflag = 0;
537c478bd9Sstevel@tonic-gate int Bflag = 0;
547c478bd9Sstevel@tonic-gate int index;
557c478bd9Sstevel@tonic-gate char *rulesfile;
567c478bd9Sstevel@tonic-gate int typearg = 0;
577c478bd9Sstevel@tonic-gate int (*wrkfunc)();
587c478bd9Sstevel@tonic-gate extern char *optarg;
597c478bd9Sstevel@tonic-gate extern int optind, opterr;
607c478bd9Sstevel@tonic-gate extern void bld_pack_list();
617c478bd9Sstevel@tonic-gate extern void usage();
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
647c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
657c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
667c478bd9Sstevel@tonic-gate #endif
677c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate global_flags = LF_NULL;
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate rfd = open_rulesfile();
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate libp = &libp_hd;
747c478bd9Sstevel@tonic-gate get_libsrch_path(libp);
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate /* create hash table for tracking libraries */
777c478bd9Sstevel@tonic-gate if (hcreate(10000) == 0) {
787c478bd9Sstevel@tonic-gate /* unlikely this ever happens or I would work around it */
797c478bd9Sstevel@tonic-gate fprintf(stderr,
807c478bd9Sstevel@tonic-gate gettext("cachefspack: can't create hash table\n"));
817c478bd9Sstevel@tonic-gate exit(1);
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "df:hiprsuvB:I:L:U:")) != -1) {
857c478bd9Sstevel@tonic-gate switch (c) {
867c478bd9Sstevel@tonic-gate case 'd':
877c478bd9Sstevel@tonic-gate wrkfunc = prtfn;
887c478bd9Sstevel@tonic-gate typearg++;
897c478bd9Sstevel@tonic-gate break;
907c478bd9Sstevel@tonic-gate case 'f':
917c478bd9Sstevel@tonic-gate fflag++;
927c478bd9Sstevel@tonic-gate rulesfile = strdup(optarg);
937c478bd9Sstevel@tonic-gate break;
947c478bd9Sstevel@tonic-gate case 'h':
957c478bd9Sstevel@tonic-gate usage();
967c478bd9Sstevel@tonic-gate exit(0);
977c478bd9Sstevel@tonic-gate break;
987c478bd9Sstevel@tonic-gate case 'i':
997c478bd9Sstevel@tonic-gate wrkfunc = inquirefn;
1007c478bd9Sstevel@tonic-gate typearg++;
1017c478bd9Sstevel@tonic-gate break;
1027c478bd9Sstevel@tonic-gate case 'p':
1037c478bd9Sstevel@tonic-gate wrkfunc = packfn;
1047c478bd9Sstevel@tonic-gate typearg++;
1057c478bd9Sstevel@tonic-gate break;
1067c478bd9Sstevel@tonic-gate case 'r':
1077c478bd9Sstevel@tonic-gate global_flags |= LF_REGEX;
1087c478bd9Sstevel@tonic-gate break;
1097c478bd9Sstevel@tonic-gate case 's':
1107c478bd9Sstevel@tonic-gate global_flags |= LF_STRIP_DOTSLASH;
1117c478bd9Sstevel@tonic-gate break;
1127c478bd9Sstevel@tonic-gate case 'u':
1137c478bd9Sstevel@tonic-gate wrkfunc = unpackfn;
1147c478bd9Sstevel@tonic-gate typearg++;
1157c478bd9Sstevel@tonic-gate break;
1167c478bd9Sstevel@tonic-gate case 'v':
1177c478bd9Sstevel@tonic-gate verbose = 1;
1187c478bd9Sstevel@tonic-gate break;
1197c478bd9Sstevel@tonic-gate case 'B':
1207c478bd9Sstevel@tonic-gate Bflag++;
1217c478bd9Sstevel@tonic-gate fprintf(rfd, "BASE %s\n", optarg);
1227c478bd9Sstevel@tonic-gate break;
1237c478bd9Sstevel@tonic-gate case 'I':
1247c478bd9Sstevel@tonic-gate fprintf(rfd, "IGNORE %s\n", optarg);
1257c478bd9Sstevel@tonic-gate break;
1267c478bd9Sstevel@tonic-gate case 'L':
1277c478bd9Sstevel@tonic-gate fprintf(rfd, "LIST %s\n", optarg);
1287c478bd9Sstevel@tonic-gate break;
1297c478bd9Sstevel@tonic-gate case 'U':
1307c478bd9Sstevel@tonic-gate typearg++;
1317c478bd9Sstevel@tonic-gate wrkfunc = unpackfn;
1327c478bd9Sstevel@tonic-gate bld_pack_list(rfd, optarg);
1337c478bd9Sstevel@tonic-gate break;
1347c478bd9Sstevel@tonic-gate default:
1357c478bd9Sstevel@tonic-gate usage();
1367c478bd9Sstevel@tonic-gate exit(1);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate def_lign_flags = LF_NULL;
1417c478bd9Sstevel@tonic-gate def_gign_flags = LF_NULL;
1427c478bd9Sstevel@tonic-gate def_list_flags = LF_REGEX;
1437c478bd9Sstevel@tonic-gate bang_list_flags = LF_STRIP_DOTSLASH;
1447c478bd9Sstevel@tonic-gate if (global_flags != 0) {
1457c478bd9Sstevel@tonic-gate def_list_flags = global_flags;
1467c478bd9Sstevel@tonic-gate bang_list_flags = global_flags;
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate if (fflag & Bflag) {
1507c478bd9Sstevel@tonic-gate fprintf(stderr, gettext(
1517c478bd9Sstevel@tonic-gate "cachefspack: B and f options are mutually exclusive\n"));
1527c478bd9Sstevel@tonic-gate exit(1);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate if (fflag) {
1567c478bd9Sstevel@tonic-gate fclose(rfd);
1577c478bd9Sstevel@tonic-gate rfd = fopen(rulesfile, "r");
1587c478bd9Sstevel@tonic-gate if (rfd == NULL) {
1597c478bd9Sstevel@tonic-gate fprintf(stderr, gettext(
1607c478bd9Sstevel@tonic-gate "cachefspack: can't open file associated"
1617c478bd9Sstevel@tonic-gate " with -f\n"));
1627c478bd9Sstevel@tonic-gate exit(1);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate if (typearg != 1) {
1677c478bd9Sstevel@tonic-gate if (typearg == 0) {
1687c478bd9Sstevel@tonic-gate wrkfunc = packfn;
1697c478bd9Sstevel@tonic-gate } else {
1707c478bd9Sstevel@tonic-gate fprintf(stderr,
1717c478bd9Sstevel@tonic-gate gettext(
1727c478bd9Sstevel@tonic-gate "cachefspack: only one 'd', 'i', 'p' or 'u' "));
1737c478bd9Sstevel@tonic-gate fprintf(stderr,
1747c478bd9Sstevel@tonic-gate gettext(" option allowed\n"));
1757c478bd9Sstevel@tonic-gate exit(1);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate if (optind < argc) {
1797c478bd9Sstevel@tonic-gate if (fflag || Bflag) {
1807c478bd9Sstevel@tonic-gate fprintf(stderr,
1817c478bd9Sstevel@tonic-gate gettext(
1827c478bd9Sstevel@tonic-gate "cachefspack: 'B' or 'f' specified "));
1837c478bd9Sstevel@tonic-gate fprintf(stderr,
1847c478bd9Sstevel@tonic-gate gettext("with filenames\n"));
1857c478bd9Sstevel@tonic-gate exit(1);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate for (index = optind; index < argc; index++) {
1887c478bd9Sstevel@tonic-gate #ifdef DEBUG
1897c478bd9Sstevel@tonic-gate printf("argv[%d] = %s\n", index, argv[index]);
1907c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1917c478bd9Sstevel@tonic-gate bld_pack_list(rfd, argv[index]);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate rewind(rfd);
1957c478bd9Sstevel@tonic-gate read_rules(rfd, wrkfunc);
1967c478bd9Sstevel@tonic-gate fclose(rfd);
197*47644099Sgt29601 return (0);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate /*
2017c478bd9Sstevel@tonic-gate * The bld_pack_list() function is used to write the temporary packing
2027c478bd9Sstevel@tonic-gate * list function. When the BASE directory changes, a new BASE command is
2037c478bd9Sstevel@tonic-gate * generated. If the filename argument(fnam) starts with a '/', then the
2047c478bd9Sstevel@tonic-gate * filename is assumed to be an absolute pathname. Otherwise, the filename
2057c478bd9Sstevel@tonic-gate * is assumed to be realtive to the current directory.
2067c478bd9Sstevel@tonic-gate */
2077c478bd9Sstevel@tonic-gate void
bld_pack_list(FILE * fd,char * filename)2087c478bd9Sstevel@tonic-gate bld_pack_list(FILE *fd, char *filename)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate static char last_base[MAXPATHLEN+1] = {" "};
2117c478bd9Sstevel@tonic-gate static char fnam[MAXPATHLEN+1];
2127c478bd9Sstevel@tonic-gate static int last_base_sz = 1;
2137c478bd9Sstevel@tonic-gate char *lastsl_pos;
2147c478bd9Sstevel@tonic-gate int sz;
2157c478bd9Sstevel@tonic-gate int endpos;
2167c478bd9Sstevel@tonic-gate char *cwd;
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate /* strip off any trailing /'s */
2197c478bd9Sstevel@tonic-gate strcpy(fnam, filename);
2207c478bd9Sstevel@tonic-gate for (endpos = strlen(fnam) - 1; endpos > 0; endpos--) {
2217c478bd9Sstevel@tonic-gate if (fnam[endpos] == '/')
2227c478bd9Sstevel@tonic-gate fnam[endpos] = '\0';
2237c478bd9Sstevel@tonic-gate else
2247c478bd9Sstevel@tonic-gate break;
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate if (*fnam == '/') { /* absolute pathname */
2287c478bd9Sstevel@tonic-gate lastsl_pos = strrchr(fnam, '/');
2297c478bd9Sstevel@tonic-gate sz = (int)lastsl_pos - (int)fnam + 1;
2307c478bd9Sstevel@tonic-gate if ((last_base_sz != sz) ||
2317c478bd9Sstevel@tonic-gate (strncmp(last_base, fnam, sz) != 0)) {
2327c478bd9Sstevel@tonic-gate fprintf(fd, "BASE %.*s\n", (sz <= 1 ? sz : sz-1), fnam);
2337c478bd9Sstevel@tonic-gate last_base_sz = sz;
2347c478bd9Sstevel@tonic-gate strncpy(last_base, fnam, sz);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate fprintf(fd, "LIST %s\n", &fnam[sz]);
2377c478bd9Sstevel@tonic-gate } else { /* relative pathname */
2387c478bd9Sstevel@tonic-gate /* Really only need to call this once, ... */
2397c478bd9Sstevel@tonic-gate cwd = getcwd(NULL, MAXPATHLEN+1);
2407c478bd9Sstevel@tonic-gate sz = strlen(cwd);
2417c478bd9Sstevel@tonic-gate if ((last_base_sz != sz) ||
2427c478bd9Sstevel@tonic-gate (strncmp(last_base, cwd, sz) != 0)) {
2437c478bd9Sstevel@tonic-gate fprintf(fd, "BASE %s\n", cwd);
2447c478bd9Sstevel@tonic-gate last_base_sz = sz;
2457c478bd9Sstevel@tonic-gate strncpy(last_base, cwd, sz);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate free(cwd);
2487c478bd9Sstevel@tonic-gate fprintf(fd, "LIST %s\n", fnam);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate void
usage()2537c478bd9Sstevel@tonic-gate usage()
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate #ifdef DEBUG
2567c478bd9Sstevel@tonic-gate printf(
2577c478bd9Sstevel@tonic-gate gettext("cachefspack -[dipu] -[fBIL] [-h] [-r] [-s] [-U dir]"));
2587c478bd9Sstevel@tonic-gate #else /* DEBUG */
2597c478bd9Sstevel@tonic-gate printf(
2607c478bd9Sstevel@tonic-gate gettext("cachefspack -[dipu] -[f] [-h] [-r] [-s] [-U dir]"));
2617c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2627c478bd9Sstevel@tonic-gate printf(gettext(" [files]\n"));
2637c478bd9Sstevel@tonic-gate printf("\n");
2647c478bd9Sstevel@tonic-gate printf(
2657c478bd9Sstevel@tonic-gate gettext("Must select 1 and only 1 of the following 5 options\n"));
2667c478bd9Sstevel@tonic-gate printf(gettext("-d Display selected filenames\n"));
2677c478bd9Sstevel@tonic-gate printf(gettext("-i Display selected filenames packing status\n"));
2687c478bd9Sstevel@tonic-gate printf(gettext("-p Pack selected filenames\n"));
2697c478bd9Sstevel@tonic-gate printf(gettext("-u Unpack selected filenames\n"));
2707c478bd9Sstevel@tonic-gate printf(gettext("-U Unpack all files in directory 'dir'\n"));
2717c478bd9Sstevel@tonic-gate printf(gettext("\n"));
2727c478bd9Sstevel@tonic-gate printf(gettext("-f Specify input file containing rules\n"));
2737c478bd9Sstevel@tonic-gate #ifdef DEBUG
2747c478bd9Sstevel@tonic-gate printf(gettext("-B Specify BASE rule on command line\n"));
2757c478bd9Sstevel@tonic-gate printf(gettext("-I Specify IGNORE rule on command line\n"));
2767c478bd9Sstevel@tonic-gate printf(gettext("-L Specify LIST rule on command line\n"));
2777c478bd9Sstevel@tonic-gate printf(gettext("\n"));
2787c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2797c478bd9Sstevel@tonic-gate printf(gettext("-h Print usage information\n"));
2807c478bd9Sstevel@tonic-gate printf(gettext(
2817c478bd9Sstevel@tonic-gate "-r Interpret strings in LIST rules as regular expressions\n"));
2827c478bd9Sstevel@tonic-gate printf(gettext("-s Strip './' from the beginning of a pattern name\n"));
2837c478bd9Sstevel@tonic-gate printf(gettext("-v Verbose option\n"));
2847c478bd9Sstevel@tonic-gate printf(gettext("files - a list of filenames to be packed/unpacked\n"));
2857c478bd9Sstevel@tonic-gate }
286