diff options
| author | yamt <yamt@NetBSD.org> | 2002-07-19 19:04:33 +0000 |
|---|---|---|
| committer | yamt <yamt@NetBSD.org> | 2002-07-19 19:04:33 +0000 |
| commit | 5eb02a32cec5a2bd63c47fca677b642c447bfa23 (patch) | |
| tree | 38afedb8594a5fb2c1ea1c920371caad57ca56a5 /usr.sbin/pkg_install/lib | |
| parent | a486314d8ab67e39bea32852dc6177cc351259a5 (diff) | |
- remove handling of PKG_ADD_BASE.
- don't search current directory if PKG_PATH is set.
- don't prefer local directories.
- constify and cleanup.
discussed on tech-pkg.
Diffstat (limited to 'usr.sbin/pkg_install/lib')
| -rw-r--r-- | usr.sbin/pkg_install/lib/Makefile | 4 | ||||
| -rw-r--r-- | usr.sbin/pkg_install/lib/file.c | 406 | ||||
| -rw-r--r-- | usr.sbin/pkg_install/lib/ftpio.c | 11 | ||||
| -rw-r--r-- | usr.sbin/pkg_install/lib/lib.h | 26 | ||||
| -rw-r--r-- | usr.sbin/pkg_install/lib/path.c | 147 | ||||
| -rw-r--r-- | usr.sbin/pkg_install/lib/path.h | 38 | ||||
| -rw-r--r-- | usr.sbin/pkg_install/lib/pen.c | 8 | ||||
| -rw-r--r-- | usr.sbin/pkg_install/lib/plist.c | 8 | ||||
| -rw-r--r-- | usr.sbin/pkg_install/lib/str.c | 25 |
9 files changed, 372 insertions, 301 deletions
diff --git a/usr.sbin/pkg_install/lib/Makefile b/usr.sbin/pkg_install/lib/Makefile index 7825d11de19..29be5331d7e 100644 --- a/usr.sbin/pkg_install/lib/Makefile +++ b/usr.sbin/pkg_install/lib/Makefile @@ -1,9 +1,9 @@ -# $NetBSD: Makefile,v 1.17 2001/12/12 01:48:54 tv Exp $ +# $NetBSD: Makefile,v 1.18 2002/07/19 19:04:38 yamt Exp $ # Original from FreeBSD, no rcs id. LIB+= install SRCS+= exec.c file.c ftpio.c global.c lpkg.c pen.c pkgdb.c \ - plist.c str.c version.c + plist.c str.c version.c path.c NOLINT= # defined NOMAN= # defined diff --git a/usr.sbin/pkg_install/lib/file.c b/usr.sbin/pkg_install/lib/file.c index 52bc432ce45..c82be4a4a1f 100644 --- a/usr.sbin/pkg_install/lib/file.c +++ b/usr.sbin/pkg_install/lib/file.c @@ -1,11 +1,11 @@ -/* $NetBSD: file.c,v 1.53 2002/07/09 03:30:05 yamt Exp $ */ +/* $NetBSD: file.c,v 1.54 2002/07/19 19:04:39 yamt Exp $ */ #include <sys/cdefs.h> #ifndef lint #if 0 static const char *rcsid = "from FreeBSD Id: file.c,v 1.29 1997/10/08 07:47:54 charnier Exp"; #else -__RCSID("$NetBSD: file.c,v 1.53 2002/07/09 03:30:05 yamt Exp $"); +__RCSID("$NetBSD: file.c,v 1.54 2002/07/19 19:04:39 yamt Exp $"); #endif #endif @@ -174,12 +174,13 @@ URLlength(const char *fname) /* * Returns the host part of a URL */ -char * -fileURLHost(char *fname, char *where, int max) +const char * +fileURLHost(const char *fname, char *where, int max) { - char *ret; + const char *ret; int i; + assert(where != NULL); assert(max > 0); if ((i = URLlength(fname)) < 0) { /* invalid URL? */ @@ -187,29 +188,24 @@ fileURLHost(char *fname, char *where, int max) } fname += i; /* Do we have a place to stick our work? */ - if ((ret = where) != NULL) { - while (*fname && *fname != '/' && --max) - *where++ = *fname++; - *where = '\0'; - return ret; - } - /* If not, they must really want us to stomp the original string */ - ret = fname; - while (*fname && *fname != '/') - ++fname; - *fname = '\0'; + ret = where; + while (*fname && *fname != '/' && --max) + *where++ = *fname++; + *where = '\0'; + return ret; } /* * Returns the filename part of a URL */ -char * -fileURLFilename(char *fname, char *where, int max) +const char * +fileURLFilename(const char *fname, char *where, int max) { - char *ret; + const char *ret; int i; + assert(where != NULL); assert(max > 0); if ((i = URLlength(fname)) < 0) { /* invalid URL? */ @@ -217,82 +213,50 @@ fileURLFilename(char *fname, char *where, int max) } fname += i; /* Do we have a place to stick our work? */ - if ((ret = where) != NULL) { - while (*fname && *fname != '/') - ++fname; - if (*fname == '/') { - while (*fname && --max) - *where++ = *fname++; - } - *where = '\0'; - return ret; - } - /* If not, they must really want us to stomp the original string */ + ret = where; while (*fname && *fname != '/') ++fname; - return fname; + if (*fname == '/') { + while (*fname && --max) + *where++ = *fname++; + } + *where = '\0'; + + return ret; } /* - * Wrapper routine for fileGetURL to iterate over several "sfx"s + * Try and fetch a file by URL, returning the directory name for where + * it's unpacked, if successful. To be handed to leave_playpen() later. */ -static char * -fileGet1URL(const char *base, const char *spec, const char *sfx) +char * +fileGetURL(const char *spec) { char host[MAXHOSTNAMELEN], file[FILENAME_MAX]; - char *cp, *rp; - char fname[FILENAME_MAX]; + const char *cp; + char *rp; char pen[FILENAME_MAX]; int rc; - char *hint; rp = NULL; - /* Special tip that sysinstall left for us */ - hint = getenv("PKG_ADD_BASE"); if (!IS_URL(spec)) { - if (!base && !hint) - return NULL; - /* We've been given an existing URL (that's known-good) and - * now we need to construct a composite one out of that and - * the basename we were handed as a dependency. */ - if (base) { - strlcpy(fname, base, sizeof(fname)); - /* Advance back two slashes to get to the root of the package hierarchy */ - cp = strrchr(fname, '/'); - if (cp) { - *cp = '\0'; /* chop name */ - cp = strrchr(fname, '/'); - } - if (cp) { - size_t sz; - - cp++; /* next char of '/' */ - sz = fname + sizeof(fname) - cp; - snprintf(cp, sz, "All/%s%s", spec, sfx); - } else - return NULL; - } else { - /* Otherwise, we've been given an environment variable hinting at the right location from sysinstall */ - assert(hint != NULL); - snprintf(fname, sizeof(fname), "%s%s%s", hint, spec, sfx); - } - } else - strlcpy(fname, spec, sizeof(fname)); + errx(1, "fileGetURL was called with non-url arg '%s'\n", spec); + } /* Some sanity checks on the URL */ - cp = fileURLHost(fname, host, MAXHOSTNAMELEN); + cp = fileURLHost(spec, host, MAXHOSTNAMELEN); if (!*cp) { - warnx("URL `%s' has bad host part!", fname); + warnx("URL `%s' has bad host part!", spec); return NULL; } - cp = fileURLFilename(fname, file, FILENAME_MAX); + cp = fileURLFilename(spec, file, FILENAME_MAX); if (!*cp) { - warnx("URL `%s' has bad filename part!", fname); + warnx("URL `%s' has bad filename part!", spec); return NULL; } if (Verbose) - printf("Trying to fetch %s.\n", fname); + printf("Trying to fetch %s.\n", spec); pen[0] = '\0'; rp = make_playpen(pen, sizeof(pen), 0); @@ -302,233 +266,135 @@ fileGet1URL(const char *base, const char *spec, const char *sfx) } rp = strdup(pen); - rc = unpackURL(fname, pen); + rc = unpackURL(spec, pen); if (rc < 0) { leave_playpen(rp); /* Don't leave dir hang around! */ - printf("Error on unpackURL('%s', '%s')\n", fname, pen); + printf("Error on unpackURL('%s', '%s')\n", spec, pen); return NULL; } return rp; } -/* - * Try and fetch a file by URL, returning the directory name for where - * it's unpacked, if successful. To be handed to leave_playpen() later. - */ -char * -fileGetURL(char *base, char *spec) -{ - char *rp; - rp = fileGet1URL(base, spec, ".tbz"); - if (rp == NULL) { - rp = fileGet1URL(base, spec, ".tgz"); - } - - return rp; -} - -/* - * Look for filename/pattern "fname" in - * - current dir, and if not found there, look - * - $base/../All - * - all dirs in $PKG_PATH - * Returns a full path/URL where the pkg can be found - */ -char * -fileFindByPath(char *base, char *fname) +static char * +resolvepattern1(const char *name) { static char tmp[FILENAME_MAX]; - char *cp; + char *cp; -/* printf("HF: fileFindByPath(\"%s\", \"%s\")\n", base, fname); *//*HF*/ + if (IS_URL(name)) { + /* some package depends on a wildcard pkg */ + int rc; - /* The following code won't return a match if base is an URL - * Could save some cycles here - HF */ - if (ispkgpattern(fname)) { - if ((cp = findbestmatchingname(".", fname)) != NULL) { - strcpy(tmp, cp); + rc = expandURL(tmp, name); + if (rc < 0) { + return NULL; + } + if (Verbose) + printf("'%s' expanded to '%s'\n", name, tmp); + return tmp; /* return expanded URL w/ corrent pkg */ + } + else if (ispkgpattern(name)) { + cp = findbestmatchingname( + dirname_of(name), basename_of(name)); + if (cp) { + snprintf(tmp, sizeof(tmp), "%s/%s", dirname_of(name), cp); free(cp); return tmp; } } else { - if (fexists(fname) && isfile(fname)) { - strcpy(tmp, fname); + if (isfile(name)) { + strlcpy(tmp, name, sizeof(tmp)); return tmp; } } - if (base) { - strcpy(tmp, base); + return NULL; +} - cp = strrchr(tmp, '/'); - if (cp) { - *cp = '\0'; /* chop name */ - cp = strrchr(tmp, '/'); - } - if (cp) { - size_t sz; - - cp++; /* next char of '/' */ - sz = fname + sizeof(fname) - cp; - snprintf(cp, sz, "All/%s.t[bg]z", fname); - - if (ispkgpattern(tmp)) { - if (IS_URL(tmp)) { - /* some package depends on a wildcard pkg */ - int rc; - char url[FILENAME_MAX]; - - /* save url to expand, as tmp is the static var in which - * we return the result of the expansion. - */ - strcpy(url, tmp); - -/* printf("HF: expandURL('%s')'ing #1\n", url);*//*HF*/ - rc = expandURL(tmp, url); - if (rc < 0) { - warnx("fileFindByPath: expandURL('%s') failed\n", url); - return NULL; - } - if (Verbose) - printf("'%s' expanded to '%s'\n", url, tmp); - return tmp; /* return expanded URL w/ corrent pkg */ - } else { - cp = findbestmatchingname( - dirname_of(tmp), basename_of(tmp)); - if (cp) { - char *s; - s = strrchr(tmp, '/'); - assert(s != NULL); - strcpy(s + 1, cp); - free(cp); - return tmp; - } - } - } else { - if (fexists(tmp)) { - return tmp; - } - } - } - } else { - if (IS_URL(fname)) { - /* Wildcard-URL directly specified on command line */ - int rc; - -/* printf("HF: expandURL('%s')'ing #2\n", fname);*//*HF*/ - rc = expandURL(tmp, fname); - if (rc < 0) { - warnx("fileFindByPath: expandURL('%s') failed\n", fname); - return NULL; - } - if (Verbose) - printf("'%s' expanded to '%s'\n", fname, tmp); - return tmp; /* return expanded URL w/ corrent pkg */ - } - } +static char * +resolvepattern(const char *name) +{ + char tmp[FILENAME_MAX]; + char *cp; + const char *suf; - cp = getenv("PKG_PATH"); - while (cp && *cp) { - char *cp2 = strsep(&cp, ";"); + cp = resolvepattern1(name); + if (cp != NULL) + return cp; - if (Verbose) - printf("trying PKG_PATH %s\n", cp2?cp2:cp); + if (ispkgpattern(name)) + return NULL; - if (strstr(fname, ".tgz") || strstr(fname, ".tbz") || strstr(fname, ".t[bg]z")) { - /* There's already a ".t[bg]z" present, probably typed on the command line */ - (void) snprintf(tmp, sizeof(tmp), "%s/%s", cp2 ? cp2 : cp, fname); - } else { - /* Try this component, and tack on a ".t[bg]z" */ - (void) snprintf(tmp, sizeof(tmp), "%s/%s.t[bg]z", cp2 ? cp2 : cp, fname); - } - if (IS_URL(tmp)) { - char url[FILENAME_MAX]; - int rc; - - /* save url to expand, as tmp is the static var in which - * we return the result of the expansion. - */ - strcpy(url, tmp); - -/* printf("HF: expandURL('%s')'ing #3\n", url);*//*HF*/ - rc = expandURL(tmp, url); - if (rc >= 0) { - if (Verbose) - printf("fileFindByPath: success, expandURL('%s') returns '%s'\n", url, tmp); - return tmp; - } + suf = suffix_of(name); + if (!strcmp(suf, "tbz") || !strcmp(suf, "tgz")) + return NULL; - /* Second chance - maybe just a package name was given, without - * a version number. Remove the ".t*z" we tacked on above, and - * re-add it with a "-[0-9]*" before. Then see if this matches - * something. See also perform.c. - */ - { - char *s; - - if ((s = strstr(tmp, ".tgz")) || - (s = strstr(tmp, ".tgz")) || - (s = strstr(tmp, ".t[bg]z"))) { - *s = '\0'; - } - snprintf(url, FILENAME_MAX, "%s-[0-9]*.t[bg]z", tmp); - rc = expandURL(tmp, url); - if (rc >= 0) { - if (Verbose) - printf("fileFindByPath: late success, expandURL('%s') returns '%s'\n", - url, tmp); - return tmp; - } - } + /* add suffix and try */ + snprintf(tmp, sizeof(tmp), "%s.tbz", name); + cp = resolvepattern1(tmp); + if (cp != NULL) + return cp; + snprintf(tmp, sizeof(tmp), "%s.tgz", name); + cp = resolvepattern1(tmp); + if (cp != NULL) + return cp; + + /* add version number wildcard and try */ + snprintf(tmp, sizeof(tmp), "%s-[0-9]*.t[bg]z", name); + return resolvepattern1(tmp); +} - /* No luck with this parth of PKG_PATH - try next one */ - - } else { - if (ispkgpattern(tmp)) { - char *s; - s = findbestmatchingname(dirname_of(tmp), basename_of(tmp)); - if (s) { - char *t; - t = strrchr(tmp, '/'); - strcpy(t + 1, s); - free(s); - return tmp; - } - } else { - if (fexists(tmp) && isfile(tmp)) { - return tmp; - } +/* + * Look for filename/pattern "fname" in + * Returns a full path/URL where the pkg can be found + */ +char * +fileFindByPath(const char *fname) +{ + char tmp[FILENAME_MAX]; + struct path *path; - /* Second chance: seems just a pkg name was given, - * no wildcard, no .tgz. Tack something on and retry. - * (see above, and perform.c) - */ - { - char *s; - char buf2[FILENAME_MAX]; - - if ((s = strstr(tmp, ".tgz")) || - (s = strstr(tmp, ".tgz")) || - (s = strstr(tmp, ".t[bg]z"))) { - *s = '\0'; - } - snprintf(buf2, FILENAME_MAX, "%s-[0-9]*.t[bg]z", tmp); - s = findbestmatchingname(dirname_of(buf2), basename_of(buf2)); - if (s) { - char *t; - strcpy(tmp, buf2); /* now we can overwrite it */ - t = strrchr(tmp, '/'); - strcpy(t+1, s); - free(s); - return tmp; - } - } - } + /* + * 1. if fname is an absolute pathname or a url, + * just use it. + */ + if (IS_FULLPATH(fname) || IS_URL(fname)) + return resolvepattern(fname); + + /* + * 2. otherwise, use PKG_PATH. + */ + TAILQ_FOREACH(path, &PkgPath, pl_entry) { + char *cp; + const char *cp2 = path->pl_path; + + if (Verbose) + printf("trying PKG_PATH %s\n", cp2); + + if (IS_FULLPATH(cp2) || IS_URL(cp2)) { + snprintf(tmp, sizeof(tmp), "%s/%s", cp2, fname); } + else { + char cwdtmp[MAXPATHLEN]; + if (getcwd(cwdtmp, sizeof(cwdtmp)) == NULL) + errx(1, "getcwd"); + snprintf(tmp, sizeof(tmp), "%s/%s/%s", cwdtmp, cp2, fname); + } + cp = resolvepattern(tmp); + if (cp) + return cp; } +#if 0 + /* + * 3. finally, search current directory. + */ + snprintf(tmp, sizeof(tmp), "./%s", fname); + return resolvepattern(tmp); +#else return NULL; +#endif } /* @@ -657,7 +523,7 @@ move_file(char *dir, char *fname, char *to) * Unpack a tar file */ int -unpack(char *pkg, char *flist) +unpack(const char *pkg, const char *flist) { char args[10] = "-"; char *cp; @@ -666,7 +532,7 @@ unpack(char *pkg, char *flist) * Figure out by a crude heuristic whether this or not this is probably * compressed. */ - if (strcmp(pkg, "-")) { + if (!IS_STDIN(pkg)) { cp = strrchr(pkg, '.'); if (cp) { cp++; diff --git a/usr.sbin/pkg_install/lib/ftpio.c b/usr.sbin/pkg_install/lib/ftpio.c index 13b0532ae90..e970be5e034 100644 --- a/usr.sbin/pkg_install/lib/ftpio.c +++ b/usr.sbin/pkg_install/lib/ftpio.c @@ -1,8 +1,8 @@ -/* $NetBSD: ftpio.c,v 1.39 2002/07/09 03:30:06 yamt Exp $ */ +/* $NetBSD: ftpio.c,v 1.40 2002/07/19 19:04:39 yamt Exp $ */ #include <sys/cdefs.h> #ifndef lint -__RCSID("$NetBSD: ftpio.c,v 1.39 2002/07/09 03:30:06 yamt Exp $"); +__RCSID("$NetBSD: ftpio.c,v 1.40 2002/07/19 19:04:39 yamt Exp $"); #endif /* @@ -606,7 +606,7 @@ expandURL(char *expandedurl, const char *wildcardurl) } (void) fclose(f); - if (matches == 0) + if (matches == 0 && Verbose) warnx("nothing appropriate found\n"); } @@ -617,6 +617,8 @@ expandURL(char *expandedurl, const char *wildcardurl) printf("best match: '%s%s'\n", base, best); snprintf(expandedurl, FILENAME_MAX, "%s%s", base, best); } + else + return -1; } return 0; @@ -645,7 +647,7 @@ unpackURL(const char *url, const char *dir) return -1; } if (strcmp(exp, url) != 0) { - warnx("unpackURL: verificatinon expandURL failed, '%s'!='%s'", + warnx("unpackURL: verification expandURL failed, '%s'!='%s'", exp, url); return -1; } @@ -665,6 +667,7 @@ unpackURL(const char *url, const char *dir) /* Leave a hint for any depending pkgs that may need it */ if (getenv("PKG_PATH") == NULL) { setenv("PKG_PATH", pkg_path, 1); + path_create(pkg_path); /* XXX */ printf("setenv PKG_PATH='%s'\n",pkg_path); } diff --git a/usr.sbin/pkg_install/lib/lib.h b/usr.sbin/pkg_install/lib/lib.h index b16cd3e7bfa..2f13835134a 100644 --- a/usr.sbin/pkg_install/lib/lib.h +++ b/usr.sbin/pkg_install/lib/lib.h @@ -1,4 +1,4 @@ -/* $NetBSD: lib.h,v 1.42 2002/06/21 14:49:41 agc Exp $ */ +/* $NetBSD: lib.h,v 1.43 2002/07/19 19:04:40 yamt Exp $ */ /* from FreeBSD Id: lib.h,v 1.25 1997/10/08 07:48:03 charnier Exp */ @@ -38,6 +38,8 @@ #include <string.h> #include <unistd.h> +#include "path.h" + /* Macros */ #define SUCCESS (0) #define FAIL (-1) @@ -180,6 +182,9 @@ typedef int (*matchfn) (const char *, void *); * and this must be an URL. Hide this behind a more obvious name. */ #define IS_URL(str) (URLlength(str) > 0) +#define IS_STDIN(str) ((str) != NULL && !strcmp((str), "-")) +#define IS_FULLPATH(str) ((str) != NULL && (str)[0] == '/') + /* Prototypes */ /* Misc */ int vsystem(const char *,...) @@ -196,8 +201,9 @@ void show_version(void); /* String */ char *get_dash_string(char **); void str_lowercase(char *); -char *basename_of(char *); -char *dirname_of(const char *); +const char *basename_of(const char *); +const char *dirname_of(const char *); +const char *suffix_of(const char *); int pmatch(const char *, const char *); int findmatchingname(const char *, const char *, matchfn, void *); /* doesn't really belong to "strings" */ char *findbestmatchingname(const char *, const char *); /* neither */ @@ -220,17 +226,17 @@ Boolean isemptyfile(const char *); Boolean isfile(const char *); Boolean isempty(const char *); int URLlength(const char *); -char *fileGetURL(char *, char *); -char *fileURLFilename(char *, char *, int); -char *fileURLHost(char *, char *, int); -char *fileFindByPath(char *, char *); +char *fileGetURL(const char *); +const char *fileURLFilename(const char *, char *, int); +const char *fileURLHost(const char *, char *, int); +char *fileFindByPath(const char *); char *fileGetContents(char *); Boolean make_preserve_name(char *, size_t, char *, char *); void write_file(char *, char *); void copy_file(char *, char *, char *); void move_file(char *, char *, char *); int delete_hierarchy(char *, Boolean, Boolean); -int unpack(char *, char *); +int unpack(const char *, const char *); void format_cmd(char *, size_t, char *, char *, char *); /* ftpio.c: FTP handling */ @@ -249,8 +255,8 @@ void plist_delete(package_t *, Boolean, pl_ent_t, char *); void free_plist(package_t *); void mark_plist(package_t *); void csum_plist_entry(char *, plist_t *); -void add_plist(package_t *, pl_ent_t, char *); -void add_plist_top(package_t *, pl_ent_t, char *); +void add_plist(package_t *, pl_ent_t, const char *); +void add_plist_top(package_t *, pl_ent_t, const char *); void delete_plist(package_t *pkg, Boolean all, pl_ent_t type, char *name); void write_plist(package_t *, FILE *, char *); void read_plist(package_t *, FILE *); diff --git a/usr.sbin/pkg_install/lib/path.c b/usr.sbin/pkg_install/lib/path.c new file mode 100644 index 00000000000..c38be9044fa --- /dev/null +++ b/usr.sbin/pkg_install/lib/path.c @@ -0,0 +1,147 @@ +/* $NetBSD: path.c,v 1.1 2002/07/19 19:04:40 yamt Exp $ */ + +/*- + * Copyright (c)2002 YAMAMOTO Takashi, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +#ifndef lint +__RCSID("$NetBSD: path.c,v 1.1 2002/07/19 19:04:40 yamt Exp $"); +#endif + +#include <err.h> + +#include "lib.h" + +struct pathhead PkgPath = TAILQ_HEAD_INITIALIZER(PkgPath); + +void +path_create(const char *path) +{ + const char *cp; + size_t len; + char cwd[MAXPATHLEN]; + size_t cwdlen; + + path_free(); + + if (path == NULL) { + path = "."; /* XXX */ + } + + if (Verbose) + printf("parsing: %s\n", path); + + if (getcwd(cwd, sizeof(cwd)) == NULL) + err(1, "getcwd"); + cwdlen = strlen(cwd); + + cp = path; + while (*cp) { + len = strcspn(cp, ";"); + if (len > 0) { + /* add a new path */ + struct path *new; + + new = malloc(sizeof(*new)); + if (new == NULL) + err(1, "path_create"); + + if (!IS_FULLPATH(cp) && !IS_URL(cp)) { + /* this is a relative path */ + size_t total; + + total = cwdlen + 1 + len + 1; + new->pl_path = malloc(total); + if (new->pl_path == NULL) + err(1, "path_create"); + snprintf(new->pl_path, total, "%s/%*.*s", cwd, (int)len, (int)len, cp); + } + else { + new->pl_path = malloc(len + 1); + if (new->pl_path == NULL) + err(1, "path_create"); + memcpy(new->pl_path, cp, len); + new->pl_path[len] = '\0'; + } + + if (Verbose) + printf("path: %s\n", new->pl_path); + TAILQ_INSERT_TAIL(&PkgPath, new, pl_entry); + } + + cp += len; + if (*cp == '\0') + break; + cp++; + } +} + +void +path_free() +{ + struct path *p; + + while ((p = TAILQ_FIRST(&PkgPath)) != NULL) { + TAILQ_REMOVE(&PkgPath, p, pl_entry); + free(p->pl_path); + free(p); + } +} + +void +path_setenv(const char *envname) +{ + struct path *p; + ssize_t len = 0; + char *env, *env0, *envend; + char *sep; + + TAILQ_FOREACH(p, &PkgPath, pl_entry) + len += strlen(p->pl_path) + 1; + + env = malloc(len); + if (env == NULL) + err(1, "path_setenv"); + + env0 = env; + envend = env + len; + sep = ""; + TAILQ_FOREACH(p, &PkgPath, pl_entry) { + int r; + + r = snprintf(env, envend - env, "%s%s", sep, p->pl_path); + if (r < 0 || r >= envend - env) + err(1, "snprintf"); + env += r; + sep = ";"; + } + + if (Verbose) + printf("%s = %s\n", envname, env0); + if (setenv(envname, env0, 1) != 0) + err(1, "setenv"); + free(env0); +} diff --git a/usr.sbin/pkg_install/lib/path.h b/usr.sbin/pkg_install/lib/path.h new file mode 100644 index 00000000000..6615913aa09 --- /dev/null +++ b/usr.sbin/pkg_install/lib/path.h @@ -0,0 +1,38 @@ +/* $NetBSD: path.h,v 1.1 2002/07/19 19:04:41 yamt Exp $ */ + +/*- + * Copyright (c)2002 YAMAMOTO Takashi, + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +struct path { + TAILQ_ENTRY(path) pl_entry; + char *pl_path; +}; + +TAILQ_HEAD(pathhead, path); +extern struct pathhead PkgPath; +void path_create(const char *); +void path_free(void); +void path_setenv(const char *); diff --git a/usr.sbin/pkg_install/lib/pen.c b/usr.sbin/pkg_install/lib/pen.c index 0f8250dc0a3..99fe68b9f4f 100644 --- a/usr.sbin/pkg_install/lib/pen.c +++ b/usr.sbin/pkg_install/lib/pen.c @@ -1,11 +1,11 @@ -/* $NetBSD: pen.c,v 1.19 2001/01/05 03:27:28 lukem Exp $ */ +/* $NetBSD: pen.c,v 1.20 2002/07/19 19:04:42 yamt Exp $ */ #include <sys/cdefs.h> #ifndef lint #if 0 static const char *rcsid = "from FreeBSD Id: pen.c,v 1.25 1997/10/08 07:48:12 charnier Exp"; #else -__RCSID("$NetBSD: pen.c,v 1.19 2001/01/05 03:27:28 lukem Exp $"); +__RCSID("$NetBSD: pen.c,v 1.20 2002/07/19 19:04:42 yamt Exp $"); #endif #endif @@ -46,7 +46,7 @@ static int CurrentSet; /* rm -rf Current only if it's really set! */ /* * Backup Current and Previous into temp. strings that are later * restored & freed by restore_dirs - * This is to make nested calls to makeplaypen/leave_playpen work + * This is to make nested calls to make_playpen/leave_playpen work */ void save_dirs(char **c, char **p) @@ -58,7 +58,7 @@ save_dirs(char **c, char **p) /* * Restore Current and Previous from temp strings that were created * by safe_dirs. - * This is to make nested calls to makeplaypen/leave_playpen work + * This is to make nested calls to make_playpen/leave_playpen work */ void restore_dirs(char *c, char *p) diff --git a/usr.sbin/pkg_install/lib/plist.c b/usr.sbin/pkg_install/lib/plist.c index e9ad99be882..4a6d3431cb9 100644 --- a/usr.sbin/pkg_install/lib/plist.c +++ b/usr.sbin/pkg_install/lib/plist.c @@ -1,11 +1,11 @@ -/* $NetBSD: plist.c,v 1.33 2002/06/09 14:14:51 yamt Exp $ */ +/* $NetBSD: plist.c,v 1.34 2002/07/19 19:04:42 yamt Exp $ */ #include <sys/cdefs.h> #ifndef lint #if 0 static const char *rcsid = "from FreeBSD Id: plist.c,v 1.24 1997/10/08 07:48:15 charnier Exp"; #else -__RCSID("$NetBSD: plist.c,v 1.33 2002/06/09 14:14:51 yamt Exp $"); +__RCSID("$NetBSD: plist.c,v 1.34 2002/07/19 19:04:42 yamt Exp $"); #endif #endif @@ -70,7 +70,7 @@ static const cmd_t cmdv[] = { * Add an item to the end of a packing list */ void -add_plist(package_t *p, pl_ent_t type, char *arg) +add_plist(package_t *p, pl_ent_t type, const char *arg) { plist_t *tmp; @@ -90,7 +90,7 @@ add_plist(package_t *p, pl_ent_t type, char *arg) * Add an item to the start of a packing list */ void -add_plist_top(package_t *p, pl_ent_t type, char *arg) +add_plist_top(package_t *p, pl_ent_t type, const char *arg) { plist_t *tmp; diff --git a/usr.sbin/pkg_install/lib/str.c b/usr.sbin/pkg_install/lib/str.c index e25b7ac02a0..1039f5e02ed 100644 --- a/usr.sbin/pkg_install/lib/str.c +++ b/usr.sbin/pkg_install/lib/str.c @@ -1,11 +1,11 @@ -/* $NetBSD: str.c,v 1.38 2002/06/09 14:14:51 yamt Exp $ */ +/* $NetBSD: str.c,v 1.39 2002/07/19 19:04:43 yamt Exp $ */ #include <sys/cdefs.h> #ifndef lint #if 0 static const char *rcsid = "Id: str.c,v 1.5 1997/10/08 07:48:21 charnier Exp"; #else -__RCSID("$NetBSD: str.c,v 1.38 2002/06/09 14:14:51 yamt Exp $"); +__RCSID("$NetBSD: str.c,v 1.39 2002/07/19 19:04:43 yamt Exp $"); #endif #endif @@ -35,20 +35,31 @@ __RCSID("$NetBSD: str.c,v 1.38 2002/06/09 14:14:51 yamt Exp $"); #include "lib.h" /* + * Return the suffix portion of a path + */ +const char * +suffix_of(const char *str) +{ + const char *dot; + + return ((dot = strrchr(basename_of(str), '.')) == NULL) ? "" : dot + 1; +} + +/* * Return the filename portion of a path */ -char * -basename_of(char *str) +const char * +basename_of(const char *str) { - char *slash; + const char *slash; - return ((slash = strrchr(str, '/')) == (char *) NULL) ? str : slash + 1; + return ((slash = strrchr(str, '/')) == NULL) ? str : slash + 1; } /* * Return the dirname portion of a path */ -char * +const char * dirname_of(const char *path) { size_t cc; |
