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.29.2.20 2002/10/08 05:35:46 bmah Exp $
21 * $DragonFly: src/usr.sbin/pkg_install/add/Attic/main.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
25 #include <sys/param.h>
26 #include <sys/utsname.h>
30 static char Options[] = "hvIRfnrp:SMt:";
33 Boolean NoInstall = FALSE;
34 Boolean NoRecord = FALSE;
35 Boolean Remote = FALSE;
41 char *Directory = NULL;
42 char FirstPen[FILENAME_MAX];
43 add_mode_t AddMode = NORMAL;
46 char pkgnames[MAX_PKGS][MAXPATHLEN];
50 int lowver; /* Lowest version number to match */
51 int hiver; /* Highest version number to match */
52 const char *directory; /* Directory it lives in */
54 { 410000, 410000, "/packages-4.1-release" },
55 { 420000, 420000, "/packages-4.2-release" },
56 { 430000, 430000, "/packages-4.3-release" },
57 { 440000, 440000, "/packages-4.4-release" },
58 { 450000, 450000, "/packages-4.5-release" },
59 { 460000, 460001, "/packages-4.6-release" },
60 { 460002, 460099, "/packages-4.6.2-release" },
61 { 470000, 470099, "/packages-4.7-release" },
62 { 300000, 399000, "/packages-3-stable" },
63 { 400000, 499000, "/packages-4-stable" },
64 { 510000, 599000, "/packages-5-stable" },
65 { 0, 9999999, "/packages-current" },
69 static char *getpackagesite(void);
70 int getosreldate(void);
72 static void usage __P((void));
75 main(int argc, char **argv)
79 char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr;
80 static char temppackageroot[MAXPATHLEN];
83 while ((ch = getopt(argc, argv, Options)) != -1) {
115 if (strlcpy(FirstPen, optarg, sizeof(FirstPen)) >= sizeof(FirstPen))
116 errx(1, "-t Argument too long.");
137 if (argc > MAX_PKGS) {
138 errx(1, "too many packages (max %d)", MAX_PKGS);
141 if (AddMode != SLAVE) {
142 for (ch = 0; ch < MAX_PKGS; pkgs[ch++] = NULL) ;
144 /* Get all the remaining package names, if any */
145 for (ch = 0; *argv; ch++, argv++) {
147 if ((packagesite = getpackagesite()) == NULL)
148 errx(1, "package name too long");
149 if (strlcpy(temppackageroot, packagesite,
150 sizeof(temppackageroot)) >= sizeof(temppackageroot))
151 errx(1, "package name too long");
152 if (strlcat(temppackageroot, *argv, sizeof(temppackageroot))
153 >= sizeof(temppackageroot))
154 errx(1, "package name too long");
155 remotepkg = temppackageroot;
156 if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' &&
157 (ptr[2] == 'b' || ptr[2] == 'g') && ptr[3] == 'z' &&
159 if (strlcat(remotepkg, ".tgz", sizeof(temppackageroot))
160 >= sizeof(temppackageroot))
161 errx(1, "package name too long");
163 if (!strcmp(*argv, "-")) /* stdin? */
164 (const char *)pkgs[ch] = "-";
165 else if (isURL(*argv)) { /* preserve URLs */
166 if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))
167 >= sizeof(pkgnames[ch]))
168 errx(1, "package name too long");
169 pkgs[ch] = pkgnames[ch];
171 else if ((Remote) && isURL(remotepkg)) {
172 if (strlcpy(pkgnames[ch], remotepkg, sizeof(pkgnames[ch]))
173 >= sizeof(pkgnames[ch]))
174 errx(1, "package name too long");
175 pkgs[ch] = pkgnames[ch];
176 } else { /* expand all pathnames to fullnames */
177 if (fexists(*argv)) /* refers to a file directly */
178 pkgs[ch] = realpath(*argv, pkgnames[ch]);
179 else { /* look for the file in the expected places */
180 if (!(cp = fileFindByPath(NULL, *argv))) {
181 /* let pkg_do() fail later, so that error is reported */
182 if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))
183 >= sizeof(pkgnames[ch]))
184 errx(1, "package name too long");
185 pkgs[ch] = pkgnames[ch];
187 if (strlcpy(pkgnames[ch], cp, sizeof(pkgnames[ch]))
188 >= sizeof(pkgnames[ch]))
189 errx(1, "package name too long");
190 pkgs[ch] = pkgnames[ch];
194 if (packagesite != NULL)
195 packagesite[0] = '\0';
198 /* If no packages, yelp */
200 warnx("missing package name(s)");
203 else if (ch > 1 && AddMode == MASTER) {
204 warnx("only one package name may be specified with master mode");
207 /* Make sure the sub-execs we invoke get found */
209 "/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin",
212 /* Set a reasonable umask */
215 if ((error = pkg_perform(pkgs)) != 0) {
217 warnx("%d package addition(s) failed", error);
228 static char sitepath[MAXPATHLEN];
231 if (getenv("PACKAGESITE")) {
232 if (strlcpy(sitepath, getenv("PACKAGESITE"), sizeof(sitepath))
238 if (getenv("PACKAGEROOT")) {
239 if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath))
243 if (strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath))
248 if (strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath))
253 if (strlcat(sitepath, u.machine, sizeof(sitepath)) >= sizeof(sitepath))
256 reldate = getosreldate();
257 for(i = 0; releases[i].directory != NULL; i++) {
258 if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) {
259 if (strlcat(sitepath, releases[i].directory, sizeof(sitepath))
266 if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath))
276 fprintf(stderr, "%s\n%s\n",
277 "usage: pkg_add [-vInrfRMS] [-t template] [-p prefix]",
278 " pkg-name [pkg-name ...]");