main.c revision 76504
1/* 2 * 3 * FreeBSD install - a package for the installation and maintainance 4 * of non-core utilities. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 15 * Jordan K. Hubbard 16 * 18 July 1993 17 * 18 * This is the add module. 19 */ 20 21#ifndef lint 22static const char rcsid[] = 23 "$FreeBSD: head/usr.sbin/pkg_install/add/main.c 76504 2001-05-12 09:44:32Z kris $"; 24#endif 25 26#include <err.h> 27#include <sys/param.h> 28#include <sys/utsname.h> 29#include "lib.h" 30#include "add.h" 31 32static char Options[] = "hvIRfnrp:SMt:"; 33 34char *Prefix = NULL; 35Boolean NoInstall = FALSE; 36Boolean NoRecord = FALSE; 37Boolean Remote = FALSE; 38 39char *Mode = NULL; 40char *Owner = NULL; 41char *Group = NULL; 42char *PkgName = NULL; 43char *Directory = NULL; 44char FirstPen[FILENAME_MAX]; 45add_mode_t AddMode = NORMAL; 46 47#define MAX_PKGS 20 48char pkgnames[MAX_PKGS][MAXPATHLEN]; 49char *pkgs[MAX_PKGS]; 50 51struct { 52 int lowver; /* Lowest version number to match */ 53 int hiver; /* Highest version number to match */ 54 const char *directory; /* Directory it lives in */ 55} releases[] = { 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 { 300000, 399000, "/packages-3-stable" }, 62 { 400000, 499000, "/packages-4-stable" }, 63 { 510000, 599000, "/packages-5-stable" }, 64 { 0, 9999999, "/packages-current" }, 65 { 0, 0, NULL } 66}; 67 68static char *getpackagesite(void); 69int getosreldate(void); 70 71static void usage __P((void)); 72 73int 74main(int argc, char **argv) 75{ 76 int ch, error; 77 char **start; 78 char *cp, *packagesite, *remotepkg = NULL, *ptr; 79 static char temppackageroot[MAXPATHLEN]; 80 81 start = argv; 82 while ((ch = getopt(argc, argv, Options)) != -1) { 83 switch(ch) { 84 case 'v': 85 Verbose = TRUE; 86 break; 87 88 case 'p': 89 Prefix = optarg; 90 break; 91 92 case 'I': 93 NoInstall = TRUE; 94 break; 95 96 case 'R': 97 NoRecord = TRUE; 98 break; 99 100 case 'f': 101 Force = TRUE; 102 break; 103 104 case 'n': 105 Fake = TRUE; 106 Verbose = TRUE; 107 break; 108 109 case 'r': 110 Remote = TRUE; 111 break; 112 113 case 't': 114 if (s_strlcpy(FirstPen, optarg, sizeof(FirstPen))) 115 errx(1, "-t Argument too long."); 116 break; 117 118 case 'S': 119 AddMode = SLAVE; 120 break; 121 122 case 'M': 123 AddMode = MASTER; 124 break; 125 126 case 'h': 127 case '?': 128 default: 129 usage(); 130 break; 131 } 132 } 133 argc -= optind; 134 argv += optind; 135 136 if (argc > MAX_PKGS) { 137 errx(1, "too many packages (max %d)", MAX_PKGS); 138 } 139 140 if (AddMode != SLAVE) { 141 for (ch = 0; ch < MAX_PKGS; pkgs[ch++] = NULL) ; 142 143 /* Get all the remaining package names, if any */ 144 for (ch = 0; *argv; ch++, argv++) { 145 if (Remote) { 146 if ((packagesite = getpackagesite()) == NULL) 147 errx(1, "package name too long"); 148 if (s_strlcpy(temppackageroot, packagesite, 149 sizeof(temppackageroot))) 150 errx(1, "package name too long"); 151 if (s_strlcat(temppackageroot, *argv, 152 sizeof(temppackageroot))) 153 errx(1, "package name too long"); 154 remotepkg = temppackageroot; 155 if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' && 156 ptr[2] == 'g' && ptr[3] == 'z' && !ptr[4])) 157 if (s_strlcat(remotepkg, ".tgz", sizeof(temppackageroot))) 158 errx(1, "package name too long"); 159 } 160 if (!strcmp(*argv, "-")) /* stdin? */ 161 pkgs[ch] = "-"; 162 else if (isURL(*argv)) { /* preserve URLs */ 163 if (s_strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))) 164 errx(1, "package name too long"); 165 pkgs[ch] = pkgnames[ch]; 166 } 167 else if ((Remote) && isURL(remotepkg)) { 168 if (s_strlcpy(pkgnames[ch], remotepkg, sizeof(pkgnames[ch]))) 169 errx(1, "package name too long"); 170 pkgs[ch] = pkgnames[ch]; 171 } else { /* expand all pathnames to fullnames */ 172 if (fexists(*argv)) /* refers to a file directly */ 173 pkgs[ch] = realpath(*argv, pkgnames[ch]); 174 else { /* look for the file in the expected places */ 175 if (!(cp = fileFindByPath(NULL, *argv))) 176 warnx("can't find package '%s'", *argv); 177 else { 178 if (s_strlcpy(pkgnames[ch], cp, sizeof(pkgnames[ch]))) 179 errx(1, "package name too long"); 180 pkgs[ch] = pkgnames[ch]; 181 } 182 } 183 } 184 } 185 } 186 /* If no packages, yelp */ 187 else if (!ch) { 188 warnx("missing package name(s)"); 189 usage(); 190 } 191 else if (ch > 1 && AddMode == MASTER) { 192 warnx("only one package name may be specified with master mode"); 193 usage(); 194 } 195 /* Make sure the sub-execs we invoke get found */ 196 setenv("PATH", 197 "/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin", 198 1); 199 200 /* Set a reasonable umask */ 201 umask(022); 202 203 if ((error = pkg_perform(pkgs)) != 0) { 204 if (Verbose) 205 warnx("%d package addition(s) failed", error); 206 return error; 207 } 208 else 209 return 0; 210} 211 212static char * 213getpackagesite(void) 214{ 215 int reldate, i; 216 static char sitepath[MAXPATHLEN]; 217 struct utsname u; 218 219 if (getenv("PACKAGESITE")) { 220 if (s_strlcpy(sitepath, getenv("PACKAGESITE"), 221 sizeof(sitepath))) 222 return NULL; 223 return sitepath; 224 } 225 226 if (getenv("PACKAGEROOT")) { 227 if (s_strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath))) 228 return NULL; 229 } else { 230 if (s_strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath))) 231 return NULL; 232 } 233 234 if (s_strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath))) 235 return NULL; 236 237 uname(&u); 238 if (s_strlcat(sitepath, u.machine, sizeof(sitepath))) 239 return NULL; 240 241 reldate = getosreldate(); 242 for(i = 0; releases[i].directory != NULL; i++) { 243 if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) { 244 if (s_strlcat(sitepath, releases[i].directory, sizeof(sitepath))) 245 return NULL; 246 continue; 247 } 248 } 249 250 if (s_strlcat(sitepath, "/Latest/", sizeof(sitepath))) 251 return NULL; 252 253 return sitepath; 254 255} 256 257static void 258usage() 259{ 260 fprintf(stderr, "%s\n%s\n", 261 "usage: pkg_add [-vInrfRMS] [-t template] [-p prefix]", 262 " pkg-name [pkg-name ...]"); 263 exit(1); 264} 265