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.
20 * $FreeBSD: src/usr.sbin/pkg_install/add/main.c,v 1.59 2004/07/28 07:19:15 kan Exp $
21 * $DragonFly: src/usr.sbin/pkg_install/add/Attic/main.c,v 1.8 2004/08/01 04:22:49 drhodus Exp $
25 #include <sys/param.h>
26 #include <sys/utsname.h>
30 static char Options[] = "hvIRfnrp:SMt:C:";
34 Boolean NoInstall = FALSE;
35 Boolean NoRecord = FALSE;
36 Boolean Remote = FALSE;
42 char *PkgAddCmd = NULL;
43 char *Directory = NULL;
44 char FirstPen[FILENAME_MAX];
45 add_mode_t AddMode = NORMAL;
48 char pkgnames[MAX_PKGS][MAXPATHLEN];
52 int lowver; /* Lowest version number to match */
53 int hiver; /* Highest version number to match */
54 const char *directory; /* Directory it lives in */
56 { 410000, 410000, "/packages-4.1-release" },
57 { 420000, 420000, "/packages-4.2-release" },
58 { 430000, 430000, "/packages-4.3-release" },
59 { 440000, 440000, "/packages-4.4-release" },
60 { 450000, 450000, "/packages-4.5-release" },
61 { 460000, 460001, "/packages-4.6-release" },
62 { 460002, 460099, "/packages-4.6.2-release" },
63 { 470000, 470099, "/packages-4.7-release" },
64 { 480000, 480099, "/packages-4.8-release" },
65 { 490000, 490099, "/packages-4.9-release" },
66 { 491000, 491099, "/packages-4.10-release" },
67 { 492000, 492099, "/packages-4.11-release" },
68 { 500000, 500099, "/packages-5.0-release" },
69 { 501000, 501099, "/packages-5.1-release" },
70 { 502000, 502009, "/packages-5.2-release" },
71 { 502010, 502099, "/packages-5.2.1-release" },
72 { 503000, 503099, "/packages-5.3-release" },
73 { 300000, 399000, "/packages-3-stable" },
74 { 400000, 499000, "/packages-4-stable" },
75 { 502100, 599000, "/packages-5-current" },
80 static char *getpackagesite(void);
81 int getosreldate(void);
83 static void usage(void);
86 main(int argc, char **argv)
90 char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr;
91 static char temppackageroot[MAXPATHLEN];
92 static char pkgaddpath[MAXPATHLEN];
94 if (*argv[0] != '/' && strchr(argv[0], '/') != NULL)
95 PkgAddCmd = realpath(argv[0], pkgaddpath);
100 while ((ch = getopt(argc, argv, Options)) != -1) {
132 if (strlcpy(FirstPen, optarg, sizeof(FirstPen)) >= sizeof(FirstPen))
133 errx(1, "-t Argument too long.");
158 if (argc > MAX_PKGS) {
159 errx(1, "too many packages (max %d)", MAX_PKGS);
162 if (AddMode != SLAVE) {
163 for (ch = 0; ch < MAX_PKGS; pkgs[ch++] = NULL) ;
165 /* Get all the remaining package names, if any */
166 for (ch = 0; *argv; ch++, argv++) {
168 if ((packagesite = getpackagesite()) == NULL)
169 errx(1, "package name too long");
170 if (strlcpy(temppackageroot, packagesite,
171 sizeof(temppackageroot)) >= sizeof(temppackageroot))
172 errx(1, "package name too long");
173 if (strlcat(temppackageroot, *argv, sizeof(temppackageroot))
174 >= sizeof(temppackageroot))
175 errx(1, "package name too long");
176 remotepkg = temppackageroot;
177 if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' &&
178 (ptr[2] == 'b' || ptr[2] == 'g') && ptr[3] == 'z' &&
180 if (strlcat(remotepkg,
181 #if defined(__FreeBSD_version) && __FreeBSD_version >= 500039
186 sizeof(temppackageroot)) >= sizeof(temppackageroot))
187 errx(1, "package name too long");
189 if (!strcmp(*argv, "-")) /* stdin? */
190 pkgs[ch] = (char *)"-";
191 else if (isURL(*argv)) { /* preserve URLs */
192 if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))
193 >= sizeof(pkgnames[ch]))
194 errx(1, "package name too long");
195 pkgs[ch] = pkgnames[ch];
197 else if ((Remote) && isURL(remotepkg)) {
198 if (strlcpy(pkgnames[ch], remotepkg, sizeof(pkgnames[ch]))
199 >= sizeof(pkgnames[ch]))
200 errx(1, "package name too long");
201 pkgs[ch] = pkgnames[ch];
202 } else { /* expand all pathnames to fullnames */
203 if (fexists(*argv)) /* refers to a file directly */
204 pkgs[ch] = realpath(*argv, pkgnames[ch]);
205 else { /* look for the file in the expected places */
206 if (!(cp = fileFindByPath(NULL, *argv))) {
207 /* let pkg_do() fail later, so that error is reported */
208 if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))
209 >= sizeof(pkgnames[ch]))
210 errx(1, "package name too long");
211 pkgs[ch] = pkgnames[ch];
213 if (strlcpy(pkgnames[ch], cp, sizeof(pkgnames[ch]))
214 >= sizeof(pkgnames[ch]))
215 errx(1, "package name too long");
216 pkgs[ch] = pkgnames[ch];
220 if (packagesite != NULL)
221 packagesite[0] = '\0';
224 /* If no packages, yelp */
226 warnx("missing package name(s)");
229 else if (ch > 1 && AddMode == MASTER) {
230 warnx("only one package name may be specified with master mode");
233 /* Perform chroot if requested */
234 if (Chroot != NULL) {
236 errx(1, "chroot to %s failed", Chroot);
238 /* Make sure the sub-execs we invoke get found */
240 "/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin",
243 /* Set a reasonable umask */
246 if ((error = pkg_perform(pkgs)) != 0) {
248 warnx("%d package addition(s) failed", error);
259 static char sitepath[MAXPATHLEN];
262 if (getenv("PACKAGESITE")) {
263 if (strlcpy(sitepath, getenv("PACKAGESITE"), sizeof(sitepath))
269 if (getenv("PACKAGEROOT")) {
270 if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath))
274 if (strlcat(sitepath, "http://gobsd.com", sizeof(sitepath))
279 if (strlcat(sitepath, "/packages", sizeof(sitepath))
285 if (strlcat(sitepath, u.machine, sizeof(sitepath)) >= sizeof(sitepath))
289 reldate = getosreldate();
290 for(i = 0; releases[i].directory != NULL; i++) {
291 if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) {
292 if (strlcat(sitepath, releases[i].directory, sizeof(sitepath))
299 if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath))
309 fprintf(stderr, "%s\n%s\n",
310 "usage: pkg_add [-vInrfRMS] [-t template] [-p prefix] [-C chrootdir]",
311 " pkg-name [pkg-name ...]");