diff options
author | Vincent Labrecque <vincent@cvs.openbsd.org> | 2002-07-25 16:37:55 +0000 |
---|---|---|
committer | Vincent Labrecque <vincent@cvs.openbsd.org> | 2002-07-25 16:37:55 +0000 |
commit | 4ddef896553dfa013b86c100b72f54645767fa38 (patch) | |
tree | 3a9601779a3057c0c7fb547963f86d276329f9e8 /usr.bin/mg | |
parent | 46e317fa17af020f1947cd02ac6e9aa3a46ac08b (diff) |
replace the ugly and buggy adjustname function by a simple one using
simple APIs. makes mg not crash with 65k filenames...
ok art@
Diffstat (limited to 'usr.bin/mg')
-rw-r--r-- | usr.bin/mg/file.c | 16 | ||||
-rw-r--r-- | usr.bin/mg/fileio.c | 160 | ||||
-rw-r--r-- | usr.bin/mg/grep.c | 4 | ||||
-rw-r--r-- | usr.bin/mg/main.c | 10 |
4 files changed, 62 insertions, 128 deletions
diff --git a/usr.bin/mg/file.c b/usr.bin/mg/file.c index 6bbcf11e8ad..8cd19d3fd73 100644 --- a/usr.bin/mg/file.c +++ b/usr.bin/mg/file.c @@ -1,4 +1,4 @@ -/* $OpenBSD: file.c,v 1.21 2002/07/03 03:47:59 vincent Exp $ */ +/* $OpenBSD: file.c,v 1.22 2002/07/25 16:37:54 vincent Exp $ */ /* * File commands. @@ -16,13 +16,15 @@ int fileinsert(int f, int n) { int s; - char fname[NFILEN]; + char fname[NFILEN], *adjf; s = eread("Insert file: ", fname, NFILEN, EFNEW | EFCR | EFFILE); if (s != TRUE) return (s); - return insertfile(adjustname(fname), NULL, FALSE); - /* don't set buffer name */ + adjf = adjustname(fname); + if (adjf == NULL) + return (FALSE); + return insertfile(adjf, NULL, FALSE); } /* @@ -44,6 +46,8 @@ filevisit(int f, int n) if (s != TRUE) return s; adjf = adjustname(fname); + if (adjf == NULL) + return (FALSE); if ((bp = findbuffer(adjf)) == NULL) return FALSE; curbp = bp; @@ -83,6 +87,8 @@ poptofile(int f, int n) EFNEW | EFCR | EFFILE)) != TRUE) return s; adjf = adjustname(fname); + if (adjf == NULL) + return (FALSE); if ((bp = findbuffer(adjf)) == NULL) return FALSE; if ((wp = popbuf(bp)) == NULL) @@ -362,6 +368,8 @@ filewrite(int f, int n) EFNEW | EFCR | EFFILE)) != TRUE) return (s); adjfname = adjustname(fname); + if (adjfname == NULL) + return (FALSE); /* old attributes are no longer current */ bzero(&curbp->b_fi, sizeof(curbp->b_fi)); if ((s = writeout(curbp, adjfname)) == TRUE) { diff --git a/usr.bin/mg/fileio.c b/usr.bin/mg/fileio.c index b780bb61a3f..5a9beb4c3db 100644 --- a/usr.bin/mg/fileio.c +++ b/usr.bin/mg/fileio.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fileio.c,v 1.32 2002/07/01 17:20:04 vincent Exp $ */ +/* $OpenBSD: fileio.c,v 1.33 2002/07/25 16:37:54 vincent Exp $ */ /* * POSIX fileio.c @@ -8,6 +8,7 @@ static FILE *ffp; #include <sys/types.h> +#include <limits.h> #include <sys/stat.h> #include <sys/dir.h> #include <string.h> @@ -230,127 +231,46 @@ extern char *wdir; char * adjustname(const char *fn) { - char *cp; - static char fnb[NFILEN]; - struct passwd *pwent; -#ifdef SYMBLINK - struct stat statbuf; - int i, j; - char linkbuf[NFILEN]; -#endif - int n; - - switch (*fn) { - case '/': - cp = fnb; - *cp++ = *fn++; - break; - case '~': - fn++; - cp = getenv("HOME"); - if (cp != NULL && *cp != '\0' && - (*fn == '/' || *fn == '\0')) { - n = strlcpy(fnb, cp, sizeof fnb); - if (n >= sizeof fnb) - n = sizeof fnb - 1; - cp = fnb + n; - if (*fn) - fn++; - break; - } else { - cp = fnb; - while (*fn && *fn != '/') - *cp++ = *fn++; - *cp = '\0'; - if ((pwent = getpwnam(fnb)) != NULL) { - n = strlcpy(fnb, pwent->pw_dir, sizeof fnb); - if (n >= sizeof fnb) - n = sizeof fnb - 1; - cp = fnb + n; - break; - } else { - fn -= strlen(fnb) + 1; - /* can't find ~user, continue to default case */ - } + static char fnb[MAXPATHLEN]; + const char *cp; + char user[LOGIN_NAME_MAX + 1], path[MAXPATHLEN]; + int len; + + path[0] = '\0'; + /* first handle tilde expansion */ + if (fn[0] == '~') { + struct passwd *pw; + + cp = strchr(fn, '/'); + if (cp == NULL) + cp = fn + strlen(fn); /* point to the NUL byte */ + + if ((cp - &fn[1]) > LOGIN_NAME_MAX) { + ewprintf("login name too long"); + return (NULL); } - default: -#ifndef NODIR - n = strlcpy(fnb, wdir, sizeof fnb); - if (n >= sizeof fnb) - n = sizeof fnb - 1; - cp = fnb + n; - break; -#else - return fn; /* punt */ -#endif - } - if (cp != fnb && cp[-1] != '/') - *cp++ = '/'; - while (*fn) { - switch (*fn) { - case '.': - switch (fn[1]) { - case '\0': - *--cp = '\0'; - return fnb; - case '/': - fn += 2; - continue; - case '.': - if (fn[2] != '/' && fn[2] != '\0') - break; -#ifdef SYMBLINK - cp[-1] = '\0'; - for (j = MAXLINK; j-- && - lstat(fnb, &statbuf) != -1 && - (statbuf.st_mode & S_IFMT) == S_IFLNK && - (i = readlink(fnb, linkbuf, sizeof linkbuf)) - != -1;) { - if (linkbuf[0] != '/') { - --cp; - while (cp > fnb && *--cp != '/') - ; - ++cp; - (void) strncpy(cp, linkbuf, i); - cp += i; - } else { - (void) strncpy(fnb, linkbuf, i); - cp = fnb + i; - } - if (cp[-1] != '/') - *cp++ = '\0'; - else - cp[-1] = '\0'; - } - cp[-1] = '/'; -#endif - --cp; - while (cp > fnb && *--cp != '/') - ; - ++cp; - if (fn[2] == '\0') { - *--cp = '\0'; - return fnb; - } - fn += 3; - continue; - default: - break; - } - break; - case '/': - fn++; - continue; - default: - break; + if (cp == &fn[1]) /* ~/ */ + strlcpy(user, getlogin(), sizeof user); + else + strlcpy(user, &fn[1], cp - &fn[1] + 1); + pw = getpwnam(user); + if (pw == NULL) { + ewprintf("unknown user %s", user); + return (NULL); + } + strlcpy(path, pw->pw_dir, sizeof path - 1); + len = strlen(path); + if (path[len] != '/') { + path[len] = '/'; + path[len + 1] = '\0'; } - while (*fn && (*cp++ = *fn++) != '/') - ; + fn = cp; + if (*fn == '/') + fn++; } - if (cp[-1] == '/') - --cp; - *cp = '\0'; - return fnb; + strlcat(path, fn, sizeof path); + + return (realpath(path, fnb)); } #ifndef NO_STARTUP @@ -574,6 +494,8 @@ make_file_list(char *buf) buf[len - 1] = '.'; } else dir = adjustname(buf); + if (dir == NULL) + return (FALSE); /* * If the user typed a trailing / or the empty string * he wants us to use his file spec as a directory name. diff --git a/usr.bin/mg/grep.c b/usr.bin/mg/grep.c index a3418dd2cd6..37b4bbd5d32 100644 --- a/usr.bin/mg/grep.c +++ b/usr.bin/mg/grep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: grep.c,v 1.6 2002/07/01 14:33:44 vincent Exp $ */ +/* $OpenBSD: grep.c,v 1.7 2002/07/25 16:37:54 vincent Exp $ */ /* * Copyright (c) 2001 Artur Grabowski <art@openbsd.org>. All rights reserved. * @@ -213,6 +213,8 @@ retry: free(line); adjf = adjustname(fname); + if (adjf == NULL) + return (FALSE); if ((bp = findbuffer(adjf)) == NULL) return FALSE; if ((wp = popbuf(bp)) == NULL) diff --git a/usr.bin/mg/main.c b/usr.bin/mg/main.c index 8a5fa78ed05..4aa35a298fa 100644 --- a/usr.bin/mg/main.c +++ b/usr.bin/mg/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.21 2002/07/03 03:47:59 vincent Exp $ */ +/* $OpenBSD: main.c,v 1.22 2002/07/25 16:37:54 vincent Exp $ */ /* * Mainline. @@ -63,9 +63,11 @@ main(int argc, char **argv) #endif /* !NO_STARTUP */ while (--argc > 0) { cp = adjustname(*++argv); - curbp = findbuffer(cp); - (void)showbuffer(curbp, curwp, 0); - (void)readin(cp); + if (cp != NULL) { + curbp = findbuffer(cp); + (void)showbuffer(curbp, curwp, 0); + (void)readin(cp); + } } /* fake last flags */ |