3 * FreeBSD install - a package for the installation and maintainance
4 * of non-core utilities.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
18 * This is the add module.
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
25 #include <sys/param.h>
26 #include <sys/utsname.h>
30 static char Options[] = "hvIRfFnrp:P:SMt:C:K";
33 Boolean PrefixRecursive = FALSE;
35 Boolean NoInstall = FALSE;
36 Boolean NoRecord = FALSE;
37 Boolean Remote = FALSE;
38 Boolean KeepPackage = FALSE;
39 Boolean FailOnAlreadyInstalled = TRUE;
45 char *PkgAddCmd = NULL;
46 char *Directory = NULL;
47 char FirstPen[FILENAME_MAX];
48 add_mode_t AddMode = NORMAL;
51 char pkgnames[MAX_PKGS][MAXPATHLEN];
55 int lowver; /* Lowest version number to match */
56 int hiver; /* Highest version number to match */
57 const char *directory; /* Directory it lives in */
59 { 410000, 410000, "/packages-4.1-release" },
60 { 420000, 420000, "/packages-4.2-release" },
61 { 430000, 430000, "/packages-4.3-release" },
62 { 440000, 440000, "/packages-4.4-release" },
63 { 450000, 450000, "/packages-4.5-release" },
64 { 460000, 460001, "/packages-4.6-release" },
65 { 460002, 460099, "/packages-4.6.2-release" },
66 { 470000, 470099, "/packages-4.7-release" },
67 { 480000, 480099, "/packages-4.8-release" },
68 { 490000, 490099, "/packages-4.9-release" },
69 { 491000, 491099, "/packages-4.10-release" },
70 { 492000, 492099, "/packages-4.11-release" },
71 { 500000, 500099, "/packages-5.0-release" },
72 { 501000, 501099, "/packages-5.1-release" },
73 { 502000, 502009, "/packages-5.2-release" },
74 { 502010, 502099, "/packages-5.2.1-release" },
75 { 503000, 503099, "/packages-5.3-release" },
76 { 504000, 504099, "/packages-5.4-release" },
77 { 505000, 505099, "/packages-5.5-release" },
78 { 600000, 600099, "/packages-6.0-release" },
79 { 601000, 601099, "/packages-6.1-release" },
80 { 602000, 602099, "/packages-6.2-release" },
81 { 300000, 399000, "/packages-3-stable" },
82 { 400000, 499000, "/packages-4-stable" },
83 { 502100, 502128, "/packages-5-current" },
84 { 503100, 599000, "/packages-5-stable" },
85 { 600100, 699000, "/packages-6-stable" },
86 { 700000, 799000, "/packages-7-current" },
87 { 0, 9999999, "/packages-current" },
91 static char *getpackagesite(void);
92 int getosreldate(void);
94 static void usage __P((void));
97 main(int argc, char **argv)
101 char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr;
102 static char temppackageroot[MAXPATHLEN];
103 static char pkgaddpath[MAXPATHLEN];
105 if (*argv[0] != '/' && strchr(argv[0], '/') != NULL)
106 PkgAddCmd = realpath(argv[0], pkgaddpath);
111 while ((ch = getopt(argc, argv, Options)) != -1) {
119 PrefixRecursive = FALSE;
124 PrefixRecursive = TRUE;
140 FailOnAlreadyInstalled = FALSE;
156 if (strlcpy(FirstPen, optarg, sizeof(FirstPen)) >= sizeof(FirstPen))
157 errx(1, "-t Argument too long.");
182 if (argc > MAX_PKGS) {
183 errx(1, "too many packages (max %d)", MAX_PKGS);
186 if (AddMode != SLAVE) {
187 for (ch = 0; ch < MAX_PKGS; pkgs[ch++] = NULL) ;
189 /* Get all the remaining package names, if any */
190 for (ch = 0; *argv; ch++, argv++) {
192 if ((packagesite = getpackagesite()) == NULL)
193 errx(1, "package name too long");
194 if (strlcpy(temppackageroot, packagesite,
195 sizeof(temppackageroot)) >= sizeof(temppackageroot))
196 errx(1, "package name too long");
197 if (strlcat(temppackageroot, *argv, sizeof(temppackageroot))
198 >= sizeof(temppackageroot))
199 errx(1, "package name too long");
200 remotepkg = temppackageroot;
201 if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' &&
202 (ptr[2] == 'b' || ptr[2] == 'g') && ptr[3] == 'z' &&
204 if (strlcat(remotepkg,
205 #if defined(__FreeBSD_version) && __FreeBSD_version >= 500039
210 sizeof(temppackageroot)) >= sizeof(temppackageroot))
211 errx(1, "package name too long");
213 if (!strcmp(*argv, "-")) /* stdin? */
214 pkgs[ch] = (char *)"-";
215 else if (isURL(*argv)) { /* preserve URLs */
216 if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))
217 >= sizeof(pkgnames[ch]))
218 errx(1, "package name too long");
219 pkgs[ch] = pkgnames[ch];
221 else if ((Remote) && isURL(remotepkg)) {
222 if (strlcpy(pkgnames[ch], remotepkg, sizeof(pkgnames[ch]))
223 >= sizeof(pkgnames[ch]))
224 errx(1, "package name too long");
225 pkgs[ch] = pkgnames[ch];
226 } else { /* expand all pathnames to fullnames */
227 if (fexists(*argv)) /* refers to a file directly */
228 pkgs[ch] = realpath(*argv, pkgnames[ch]);
229 else { /* look for the file in the expected places */
230 if (!(cp = fileFindByPath(NULL, *argv))) {
231 /* let pkg_do() fail later, so that error is reported */
232 if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))
233 >= sizeof(pkgnames[ch]))
234 errx(1, "package name too long");
235 pkgs[ch] = pkgnames[ch];
237 if (strlcpy(pkgnames[ch], cp, sizeof(pkgnames[ch]))
238 >= sizeof(pkgnames[ch]))
239 errx(1, "package name too long");
240 pkgs[ch] = pkgnames[ch];
244 if (packagesite != NULL)
245 packagesite[0] = '\0';
248 /* If no packages, yelp */
250 warnx("missing package name(s)");
253 else if (ch > 1 && AddMode == MASTER) {
254 warnx("only one package name may be specified with master mode");
257 /* Perform chroot if requested */
258 if (Chroot != NULL) {
260 errx(1, "chroot to %s failed", Chroot);
262 /* Make sure the sub-execs we invoke get found */
264 "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin",
267 /* Set a reasonable umask */
270 if ((error = pkg_perform(pkgs)) != 0) {
272 warnx("%d package addition(s) failed", error);
283 static char sitepath[MAXPATHLEN];
286 if (getenv("PACKAGESITE")) {
287 if (strlcpy(sitepath, getenv("PACKAGESITE"), sizeof(sitepath))
293 if (getenv("PACKAGEROOT")) {
294 if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath))
298 if (strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath))
303 if (strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath))
308 if (strlcat(sitepath, u.machine, sizeof(sitepath)) >= sizeof(sitepath))
311 reldate = getosreldate();
312 for(i = 0; releases[i].directory != NULL; i++) {
313 if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) {
314 if (strlcat(sitepath, releases[i].directory, sizeof(sitepath))
321 if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath))
331 fprintf(stderr, "%s\n%s\n",
332 "usage: pkg_add [-vInfFrRMSK] [-t template] [-p prefix] [-P prefix] [-C chrootdir]",
333 " pkg-name [pkg-name ...]");