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$");
24 #include <sys/param.h>
25 #include <sys/sysctl.h>
33 Boolean PrefixRecursive = FALSE;
35 Boolean NoInstall = FALSE;
36 Boolean NoRecord = FALSE;
37 Boolean Remote = FALSE;
38 Boolean KeepPackage = FALSE;
39 Boolean FailOnAlreadyInstalled = TRUE;
40 Boolean IgnoreDeps = FALSE;
46 char *PkgAddCmd = NULL;
47 char *Directory = NULL;
48 char FirstPen[FILENAME_MAX];
49 add_mode_t AddMode = NORMAL;
54 int lowver; /* Lowest version number to match */
55 int hiver; /* Highest version number to match */
56 const char *directory; /* Directory it lives in */
58 { 410000, 410000, "/packages-4.1-release" },
59 { 420000, 420000, "/packages-4.2-release" },
60 { 430000, 430000, "/packages-4.3-release" },
61 { 440000, 440000, "/packages-4.4-release" },
62 { 450000, 450000, "/packages-4.5-release" },
63 { 460000, 460001, "/packages-4.6-release" },
64 { 460002, 460099, "/packages-4.6.2-release" },
65 { 470000, 470099, "/packages-4.7-release" },
66 { 480000, 480099, "/packages-4.8-release" },
67 { 490000, 490099, "/packages-4.9-release" },
68 { 491000, 491099, "/packages-4.10-release" },
69 { 492000, 492099, "/packages-4.11-release" },
70 { 500000, 500099, "/packages-5.0-release" },
71 { 501000, 501099, "/packages-5.1-release" },
72 { 502000, 502009, "/packages-5.2-release" },
73 { 502010, 502099, "/packages-5.2.1-release" },
74 { 503000, 503099, "/packages-5.3-release" },
75 { 504000, 504099, "/packages-5.4-release" },
76 { 505000, 505099, "/packages-5.5-release" },
77 { 600000, 600099, "/packages-6.0-release" },
78 { 601000, 601099, "/packages-6.1-release" },
79 { 602000, 602099, "/packages-6.2-release" },
80 { 603000, 603099, "/packages-6.3-release" },
81 { 604000, 604099, "/packages-6.4-release" },
82 { 700000, 700099, "/packages-7.0-release" },
83 { 701000, 701099, "/packages-7.1-release" },
84 { 702000, 702099, "/packages-7.2-release" },
85 { 703000, 703099, "/packages-7.3-release" },
86 { 704000, 704099, "/packages-7.4-release" },
87 { 800000, 800499, "/packages-8.0-release" },
88 { 801000, 801499, "/packages-8.1-release" },
89 { 802000, 802499, "/packages-8.2-release" },
90 { 300000, 399000, "/packages-3-stable" },
91 { 400000, 499000, "/packages-4-stable" },
92 { 502100, 502128, "/packages-5-current" },
93 { 503100, 599000, "/packages-5-stable" },
94 { 600100, 699000, "/packages-6-stable" },
95 { 700100, 799000, "/packages-7-stable" },
96 { 800500, 899000, "/packages-8-stable" },
97 { 900000, 999000, "/packages-9-current" },
98 { 0, 9999999, "/packages-current" },
102 static char *getpackagesite(void);
103 int getosreldate(void);
105 static void usage(void);
107 static char opts[] = "hviIRfFnrp:P:SMt:C:K";
108 static struct option longopts[] = {
109 { "chroot", required_argument, NULL, 'C' },
110 { "dry-run", no_argument, NULL, 'n' },
111 { "force", no_argument, NULL, 'f' },
112 { "help", no_argument, NULL, 'h' },
113 { "keep", no_argument, NULL, 'K' },
114 { "master", no_argument, NULL, 'M' },
115 { "no-deps", no_argument, NULL, 'i' },
116 { "no-record", no_argument, NULL, 'R' },
117 { "no-script", no_argument, NULL, 'I' },
118 { "prefix", required_argument, NULL, 'p' },
119 { "remote", no_argument, NULL, 'r' },
120 { "template", required_argument, NULL, 't' },
121 { "slave", no_argument, NULL, 'S' },
122 { "verbose", no_argument, NULL, 'v' },
127 main(int argc, char **argv)
131 char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr;
132 static char temppackageroot[MAXPATHLEN];
133 static char pkgaddpath[MAXPATHLEN];
135 pkg_wrap(PKG_INSTALL_VERSION, argv);
137 if (*argv[0] != '/' && strchr(argv[0], '/') != NULL)
138 PkgAddCmd = realpath(argv[0], pkgaddpath);
143 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
151 PrefixRecursive = FALSE;
156 PrefixRecursive = TRUE;
172 FailOnAlreadyInstalled = FALSE;
188 if (strlcpy(FirstPen, optarg, sizeof(FirstPen)) >= sizeof(FirstPen))
189 errx(1, "-t Argument too long.");
217 if (AddMode != SLAVE) {
218 pkgs = (char **)malloc((argc+1) * sizeof(char *));
219 for (ch = 0; ch <= argc; pkgs[ch++] = NULL) ;
221 /* Get all the remaining package names, if any */
222 for (ch = 0; *argv; ch++, argv++) {
223 char temp[MAXPATHLEN];
225 if ((packagesite = getpackagesite()) == NULL)
226 errx(1, "package name too long");
227 if (strlcpy(temppackageroot, packagesite,
228 sizeof(temppackageroot)) >= sizeof(temppackageroot))
229 errx(1, "package name too long");
230 if (strlcat(temppackageroot, *argv, sizeof(temppackageroot))
231 >= sizeof(temppackageroot))
232 errx(1, "package name too long");
233 remotepkg = temppackageroot;
234 if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' &&
235 (ptr[2] == 'b' || ptr[2] == 'g' || ptr[2] == 'x') &&
236 ptr[3] == 'z' && !ptr[4]))
237 if (strlcat(remotepkg, ".tbz",
238 sizeof(temppackageroot)) >= sizeof(temppackageroot))
239 errx(1, "package name too long");
241 if (!strcmp(*argv, "-")) /* stdin? */
242 pkgs[ch] = (char *)"-";
243 else if (isURL(*argv)) { /* preserve URLs */
244 if (strlcpy(temp, *argv, sizeof(temp)) >= sizeof(temp))
245 errx(1, "package name too long");
246 pkgs[ch] = strdup(temp);
248 else if ((Remote) && isURL(remotepkg)) {
249 if (strlcpy(temp, remotepkg, sizeof(temp)) >= sizeof(temp))
250 errx(1, "package name too long");
251 pkgs[ch] = strdup(temp);
252 } else { /* expand all pathnames to fullnames */
253 if (fexists(*argv)) /* refers to a file directly */
254 pkgs[ch] = strdup(realpath(*argv, temp));
255 else { /* look for the file in the expected places */
256 if (!(cp = fileFindByPath(NULL, *argv))) {
257 /* let pkg_do() fail later, so that error is reported */
258 if (strlcpy(temp, *argv, sizeof(temp)) >= sizeof(temp))
259 errx(1, "package name too long");
260 pkgs[ch] = strdup(temp);
262 if (strlcpy(temp, cp, sizeof(temp)) >= sizeof(temp))
263 errx(1, "package name too long");
264 pkgs[ch] = strdup(temp);
268 if (packagesite != NULL)
269 packagesite[0] = '\0';
272 /* If no packages, yelp */
274 warnx("missing package name(s)");
277 else if (ch > 1 && AddMode == MASTER) {
278 warnx("only one package name may be specified with master mode");
281 /* Perform chroot if requested */
282 if (Chroot != NULL) {
284 errx(1, "chroot to %s failed", Chroot);
286 /* Make sure the sub-execs we invoke get found */
288 "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin",
291 /* Set a reasonable umask */
294 if ((error = pkg_perform(pkgs)) != 0) {
296 warnx("%d package addition(s) failed", error);
307 static char sitepath[MAXPATHLEN];
308 int archmib[] = { CTL_HW, HW_MACHINE_ARCH };
310 size_t archlen = sizeof(arch);
312 if (getenv("PACKAGESITE")) {
313 if (strlcpy(sitepath, getenv("PACKAGESITE"), sizeof(sitepath))
319 if (getenv("PACKAGEROOT")) {
320 if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath))
324 if (strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath))
329 if (strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath))
333 if (sysctl(archmib, 2, arch, &archlen, NULL, 0) == -1)
336 if (strlcat(sitepath, arch, sizeof(sitepath)) >= sizeof(sitepath))
339 reldate = getosreldate();
340 for(i = 0; releases[i].directory != NULL; i++) {
341 if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) {
342 if (strlcat(sitepath, releases[i].directory, sizeof(sitepath))
349 if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath))
359 fprintf(stderr, "%s\n%s\n",
360 "usage: pkg_add [-viInfFrRMSK] [-t template] [-p prefix] [-P prefix] [-C chrootdir]",
361 " pkg-name [pkg-name ...]");