main.c revision 38942
1#ifndef lint
2static const char rcsid[] =
3	"$Id: main.c,v 1.16 1997/10/08 07:45:43 charnier Exp $";
4#endif
5
6/*
7 *
8 * FreeBSD install - a package for the installation and maintainance
9 * of non-core utilities.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * Jordan K. Hubbard
21 * 18 July 1993
22 *
23 * This is the add module.
24 *
25 */
26
27#include <err.h>
28#include <sys/param.h>
29#include "lib.h"
30#include "add.h"
31
32static char Options[] = "hvIRfnp:SMt:";
33
34char	*Prefix		= NULL;
35Boolean	NoInstall	= FALSE;
36Boolean	NoRecord	= FALSE;
37
38char	*Mode		= NULL;
39char	*Owner		= NULL;
40char	*Group		= NULL;
41char	*PkgName	= NULL;
42char	*Directory	= NULL;
43char	FirstPen[FILENAME_MAX];
44add_mode_t AddMode	= NORMAL;
45
46#define MAX_PKGS	20
47char	pkgnames[MAX_PKGS][MAXPATHLEN];
48char	*pkgs[MAX_PKGS];
49
50static void usage __P((void));
51
52int
53main(int argc, char **argv)
54{
55    int ch, err;
56    char **start;
57    char *cp;
58
59    start = argv;
60    while ((ch = getopt(argc, argv, Options)) != -1) {
61	switch(ch) {
62	case 'v':
63	    Verbose = TRUE;
64	    break;
65
66	case 'p':
67	    Prefix = optarg;
68	    break;
69
70	case 'I':
71	    NoInstall = TRUE;
72	    break;
73
74	case 'R':
75	    NoRecord = TRUE;
76	    break;
77
78	case 'f':
79	    Force = TRUE;
80	    break;
81
82	case 'n':
83	    Fake = TRUE;
84	    Verbose = TRUE;
85	    break;
86
87	case 't':
88	    strcpy(FirstPen, optarg);
89	    break;
90
91	case 'S':
92	    AddMode = SLAVE;
93	    break;
94
95	case 'M':
96	    AddMode = MASTER;
97	    break;
98
99	case 'h':
100	case '?':
101	default:
102	    usage();
103	    break;
104	}
105    }
106    argc -= optind;
107    argv += optind;
108
109    if (argc > MAX_PKGS) {
110	warnx("too many packages (max %d)", MAX_PKGS);
111	return(1);
112    }
113
114    if (AddMode != SLAVE) {
115	for (ch = 0; ch < MAX_PKGS; pkgs[ch++] = NULL) ;
116
117	/* Get all the remaining package names, if any */
118	for (ch = 0; *argv; ch++, argv++) {
119	    if (!strcmp(*argv, "-"))	/* stdin? */
120		pkgs[ch] = "-";
121	    else if (isURL(*argv))	/* preserve URLs */
122		pkgs[ch] = strcpy(pkgnames[ch], *argv);
123	    else {			/* expand all pathnames to fullnames */
124		if (fexists(*argv)) /* refers to a file directly */
125		    pkgs[ch] = realpath(*argv, pkgnames[ch]);
126		else {		/* look for the file in the expected places */
127		    if (!(cp = fileFindByPath(NULL, *argv)))
128			warnx("can't find package '%s'", *argv);
129		    else
130			pkgs[ch] = strcpy(pkgnames[ch], cp);
131		}
132	    }
133	}
134    }
135    /* If no packages, yelp */
136    else if (!ch) {
137	warnx("missing package name(s)");
138	usage();
139    }
140    else if (ch > 1 && AddMode == MASTER) {
141	warnx("only one package name may be specified with master mode");
142	usage();
143    }
144    /* Make sure the sub-execs we invoke get found */
145    setenv("PATH", "/sbin:/usr/sbin:/bin:/usr/bin", 1);
146    if ((err = pkg_perform(pkgs)) != 0) {
147	if (Verbose)
148	    warnx("%d package addition(s) failed", err);
149	return err;
150    }
151    else
152	return 0;
153}
154
155static void
156usage()
157{
158    fprintf(stderr, "%s\n%s\n",
159		"usage: pkg_add [-vInfRMS] [-t template] [-p prefix]",
160		"               pkg-name [pkg-name ...]");
161    exit(1);
162}
163