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.60 2004/10/17 14:36:30 obrien Exp $
21 * $DragonFly: src/usr.sbin/pkg_install/add/main.c,v 1.11 2005/08/12 17:57:22 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, 502128, "/packages-5-current" },
76 { 503000, 599000, "/packages-5-stable" },
77 { 600000, 699000, "/packages-6-current" },
82 static char *getpackagesite(void);
83 int getosreldate(void);
85 static void usage(void);
88 main(int argc
, char **argv
)
92 char *cp
, *packagesite
= NULL
, *remotepkg
= NULL
, *ptr
;
93 static char temppackageroot
[MAXPATHLEN
];
94 static char pkgaddpath
[MAXPATHLEN
];
96 if (*argv
[0] != '/' && strchr(argv
[0], '/') != NULL
)
97 PkgAddCmd
= realpath(argv
[0], pkgaddpath
);
102 while ((ch
= getopt(argc
, argv
, Options
)) != -1) {
134 if (strlcpy(FirstPen
, optarg
, sizeof(FirstPen
)) >= sizeof(FirstPen
))
135 errx(1, "-t Argument too long.");
160 if (argc
> MAX_PKGS
) {
161 errx(1, "too many packages (max %d)", MAX_PKGS
);
164 if (AddMode
!= SLAVE
) {
165 for (ch
= 0; ch
< MAX_PKGS
; pkgs
[ch
++] = NULL
) ;
167 /* Get all the remaining package names, if any */
168 for (ch
= 0; *argv
; ch
++, argv
++) {
170 if ((packagesite
= getpackagesite()) == NULL
)
171 errx(1, "package name too long");
172 if (strlcpy(temppackageroot
, packagesite
,
173 sizeof(temppackageroot
)) >= sizeof(temppackageroot
))
174 errx(1, "package name too long");
175 if (strlcat(temppackageroot
, *argv
, sizeof(temppackageroot
))
176 >= sizeof(temppackageroot
))
177 errx(1, "package name too long");
178 remotepkg
= temppackageroot
;
179 if (!((ptr
= strrchr(remotepkg
, '.')) && ptr
[1] == 't' &&
180 (ptr
[2] == 'b' || ptr
[2] == 'g') && ptr
[3] == 'z' &&
182 if (strlcat(remotepkg
,
183 #if defined(__FreeBSD_version) && __FreeBSD_version >= 500039
188 sizeof(temppackageroot
)) >= sizeof(temppackageroot
))
189 errx(1, "package name too long");
191 if (!strcmp(*argv
, "-")) /* stdin? */
192 pkgs
[ch
] = (char *)"-";
193 else if (isURL(*argv
)) { /* preserve URLs */
194 if (strlcpy(pkgnames
[ch
], *argv
, sizeof(pkgnames
[ch
]))
195 >= sizeof(pkgnames
[ch
]))
196 errx(1, "package name too long");
197 pkgs
[ch
] = pkgnames
[ch
];
199 else if ((Remote
) && isURL(remotepkg
)) {
200 if (strlcpy(pkgnames
[ch
], remotepkg
, sizeof(pkgnames
[ch
]))
201 >= sizeof(pkgnames
[ch
]))
202 errx(1, "package name too long");
203 pkgs
[ch
] = pkgnames
[ch
];
204 } else { /* expand all pathnames to fullnames */
205 if (fexists(*argv
)) /* refers to a file directly */
206 pkgs
[ch
] = realpath(*argv
, pkgnames
[ch
]);
207 else { /* look for the file in the expected places */
208 if (!(cp
= fileFindByPath(NULL
, *argv
))) {
209 /* let pkg_do() fail later, so that error is reported */
210 if (strlcpy(pkgnames
[ch
], *argv
, sizeof(pkgnames
[ch
]))
211 >= sizeof(pkgnames
[ch
]))
212 errx(1, "package name too long");
213 pkgs
[ch
] = pkgnames
[ch
];
215 if (strlcpy(pkgnames
[ch
], cp
, sizeof(pkgnames
[ch
]))
216 >= sizeof(pkgnames
[ch
]))
217 errx(1, "package name too long");
218 pkgs
[ch
] = pkgnames
[ch
];
222 if (packagesite
!= NULL
)
223 packagesite
[0] = '\0';
226 /* If no packages, yelp */
228 warnx("missing package name(s)");
231 else if (ch
> 1 && AddMode
== MASTER
) {
232 warnx("only one package name may be specified with master mode");
235 /* Perform chroot if requested */
236 if (Chroot
!= NULL
) {
238 errx(1, "chroot to %s failed", Chroot
);
240 /* Make sure the sub-execs we invoke get found */
242 "/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin",
245 /* Set a reasonable umask */
248 if ((error
= pkg_perform(pkgs
)) != 0) {
250 warnx("%d package addition(s) failed", error
);
261 static char sitepath
[MAXPATHLEN
];
264 if (getenv("PACKAGESITE")) {
265 if (strlcpy(sitepath
, getenv("PACKAGESITE"), sizeof(sitepath
))
271 if (getenv("PACKAGEROOT")) {
272 if (strlcpy(sitepath
, getenv("PACKAGEROOT"), sizeof(sitepath
))
276 if (strlcat(sitepath
, "http://fireflybsd.com", sizeof(sitepath
))
281 if (strlcat(sitepath
, "/packages", sizeof(sitepath
))
287 if (strlcat(sitepath
, u
.machine
, sizeof(sitepath
)) >= sizeof(sitepath
))
291 reldate
= getosreldate();
292 for(i
= 0; releases
[i
].directory
!= NULL
; i
++) {
293 if (reldate
>= releases
[i
].lowver
&& reldate
<= releases
[i
].hiver
) {
294 if (strlcat(sitepath
, releases
[i
].directory
, sizeof(sitepath
))
301 if (strlcat(sitepath
, "/Latest/", sizeof(sitepath
)) >= sizeof(sitepath
))
311 fprintf(stderr
, "%s\n%s\n",
312 "usage: pkg_add [-vInrfRMS] [-t template] [-p prefix] [-C chrootdir]",
313 " pkg-name [pkg-name ...]");