main.c revision 77730
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 77730 2001-06-04 21:35:08Z pirzyk $"; 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 /* let pkg_do() fail later, so that error is reported */ 177 pkgs[ch] = strcpy(pkgnames[ch], *argv); 178 else { 179 if (s_strlcpy(pkgnames[ch], cp, sizeof(pkgnames[ch]))) 180 errx(1, "package name too long"); 181 pkgs[ch] = pkgnames[ch]; 182 } 183 } 184 } 185 } 186 } 187 /* If no packages, yelp */ 188 else if (!ch) { 189 warnx("missing package name(s)"); 190 usage(); 191 } 192 else if (ch > 1 && AddMode == MASTER) { 193 warnx("only one package name may be specified with master mode"); 194 usage(); 195 } 196 /* Make sure the sub-execs we invoke get found */ 197 setenv("PATH", 198 "/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin", 199 1); 200 201 /* Set a reasonable umask */ 202 umask(022); 203 204 if ((error = pkg_perform(pkgs)) != 0) { 205 if (Verbose) 206 warnx("%d package addition(s) failed", error); 207 return error; 208 } 209 else 210 return 0; 211} 212 213static char * 214getpackagesite(void) 215{ 216 int reldate, i; 217 static char sitepath[MAXPATHLEN]; 218 struct utsname u; 219 220 if (getenv("PACKAGESITE")) { 221 if (s_strlcpy(sitepath, getenv("PACKAGESITE"), 222 sizeof(sitepath))) 223 return NULL; 224 return sitepath; 225 } 226 227 if (getenv("PACKAGEROOT")) { 228 if (s_strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath))) 229 return NULL; 230 } else { 231 if (s_strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath))) 232 return NULL; 233 } 234 235 if (s_strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath))) 236 return NULL; 237 238 uname(&u); 239 if (s_strlcat(sitepath, u.machine, sizeof(sitepath))) 240 return NULL; 241 242 reldate = getosreldate(); 243 for(i = 0; releases[i].directory != NULL; i++) { 244 if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) { 245 if (s_strlcat(sitepath, releases[i].directory, sizeof(sitepath))) 246 return NULL; 247 continue; 248 } 249 } 250 251 if (s_strlcat(sitepath, "/Latest/", sizeof(sitepath))) 252 return NULL; 253 254 return sitepath; 255 256} 257 258static void 259usage() 260{ 261 fprintf(stderr, "%s\n%s\n", 262 "usage: pkg_add [-vInrfRMS] [-t template] [-p prefix]", 263 " pkg-name [pkg-name ...]"); 264 exit(1); 265} 266