diff options
author | 2001-05-03 12:57:22 +0000 | |
---|---|---|
committer | 2001-05-03 12:57:22 +0000 | |
commit | 89ab1f0ff6467cf6ccba9ab572eaae705ce7bd7d (patch) | |
tree | cc5ebaade64007504bfc20797b87eafd2fcae1ab | |
parent | Add an MIIF_AUTOTSLEEP flag, needed by new aue(4) code. From NetBSD. (diff) | |
download | wireguard-openbsd-89ab1f0ff6467cf6ccba9ab572eaae705ce7bd7d.tar.xz wireguard-openbsd-89ab1f0ff6467cf6ccba9ab572eaae705ce7bd7d.zip |
* Let make_file_list always append a '/' at the end of directories.
This makes filename TAB-completion more useful, now you don't need to
write that '/' manually.
* random cleanups including using snprintf and memcmp where open-coded
equivalents were used, waitpid instead of 'while (wait() != pid);', etc.
-rw-r--r-- | usr.bin/mg/def.h | 4 | ||||
-rw-r--r-- | usr.bin/mg/echo.c | 37 | ||||
-rw-r--r-- | usr.bin/mg/fileio.c | 101 |
3 files changed, 66 insertions, 76 deletions
diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h index bff2b66ea81..8d3a36e85ac 100644 --- a/usr.bin/mg/def.h +++ b/usr.bin/mg/def.h @@ -1,4 +1,4 @@ -/* $OpenBSD: def.h,v 1.7 2001/05/01 13:22:00 art Exp $ */ +/* $OpenBSD: def.h,v 1.8 2001/05/03 12:57:22 art Exp $ */ /* * This file is the general header file for all parts @@ -404,7 +404,7 @@ char *startupfile __P((char *)); int copy __P((char *, char *)); BUFFER *dired_ __P((char *)); int d_makename __P((LINE *, char *)); -LIST *make_file_list __P((char *, int)); +LIST *make_file_list __P((char *)); /* keymap.c X */ int complete_function __P((char *, int)); diff --git a/usr.bin/mg/echo.c b/usr.bin/mg/echo.c index c1576ab30e6..dad76f4b134 100644 --- a/usr.bin/mg/echo.c +++ b/usr.bin/mg/echo.c @@ -1,4 +1,4 @@ -/* $OpenBSD: echo.c,v 1.4 2001/01/29 01:58:07 niklas Exp $ */ +/* $OpenBSD: echo.c,v 1.5 2001/05/03 12:57:22 art Exp $ */ /* * Echo line reading and writing. @@ -382,7 +382,7 @@ complt(flags, c, buf, cpos) lh = &(bheadp->b_list); else if ((flags & EFFILE) != 0) { buf[cpos] = '\0'; - wholelist = lh = make_file_list(buf, 0); + wholelist = lh = make_file_list(buf); } else panic("broken complt call: flags"); @@ -394,25 +394,20 @@ complt(flags, c, buf, cpos) nhits = 0; nxtra = HUGE; - while (lh != NULL) { - for (i = 0; i < cpos; ++i) { - if (buf[i] != lh->l_name[i]) - break; - } - if (i == cpos) { - if (nhits == 0) - lh2 = lh; - ++nhits; - if (lh->l_name[i] == '\0') - nxtra = -1; - else { - bxtra = getxtra(lh, lh2, cpos, wflag); - if (bxtra < nxtra) - nxtra = bxtra; - lh2 = lh; - } + for (; lh != NULL; lh = lh->l_next) { + if (memcmp(buf, lh->l_name, cpos) != 0) + continue; + if (nhits == 0) + lh2 = lh; + ++nhits; + if (lh->l_name[cpos] == '\0') + nxtra = -1; + else { + bxtra = getxtra(lh, lh2, cpos, wflag); + if (bxtra < nxtra) + nxtra = bxtra; + lh2 = lh; } - lh = lh->l_next; } if (nhits == 0) msg = " [No match]"; @@ -511,7 +506,7 @@ complt_list(flags, c, buf, cpos) wholelist = lh = complete_function_list(buf, c); } else if ((flags & EFFILE) != 0) { buf[cpos] = '\0'; - wholelist = lh = make_file_list(buf, 1); + wholelist = lh = make_file_list(buf); /* * We don't want to display stuff up to the / for file * names preflen is the list of a prefix of what the diff --git a/usr.bin/mg/fileio.c b/usr.bin/mg/fileio.c index 36110dbf2a2..232af9f66e1 100644 --- a/usr.bin/mg/fileio.c +++ b/usr.bin/mg/fileio.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fileio.c,v 1.11 2001/05/01 13:26:59 art Exp $ */ +/* $OpenBSD: fileio.c,v 1.12 2001/05/03 12:57:22 art Exp $ */ /* * POSIX fileio.c @@ -343,8 +343,6 @@ adjustname(fn) } #ifndef NO_STARTUP -#include <sys/file.h> - /* * Find a startup file for the user and return its name. As a service * to other pieces of code that may want to find a startup file (like @@ -355,35 +353,39 @@ char * startupfile(suffix) char *suffix; { - char *file; - static char home[NFILEN]; - - if ((file = getenv("HOME")) == NULL || *file == '\0') - goto notfound; - if (strlen(file) + 7 >= NFILEN - 1) - goto notfound; - (VOID) strcpy(home, file); - (VOID) strcat(home, "/.mg"); - if (suffix != NULL) { - (VOID) strcat(home, "-"); - (VOID) strcat(home, suffix); + static char file[NFILEN]; + char *home; + + if ((home = getenv("HOME")) == NULL || *home == '\0') + goto nohome; + + if (suffix == NULL) { + if (snprintf(file, sizeof(file), "%s/.mg", home) + >= sizeof(file)) + return NULL; + } else { + if (snprintf(file, sizeof(file), "%s/.mg-%s", home, suffix) + >= sizeof(file)) + return NULL; } - if (access(home, F_OK) == 0) - return home; - -notfound: -#ifdef STARTUPFILE - file = STARTUPFILE; - if (suffix != NULL) { - (VOID) strcpy(home, file); - (VOID) strcat(home, "-"); - (VOID) strcat(home, suffix); - file = home; + + if (access(file, R_OK) == 0) + return file; +nohome: +#ifdef STARTUPFILE + if (suffix == NULL) + if (snprintf(file, sizeof(file), "%s", STARTUPFILE) + >= sizeof(file)) + return NULL; + } else { + if (snprintf(file, sizeof(file), "%s%s", STARTUPFILE, suffix) + >= sizeof(file)) + return NULL; } - if (access(file, F_OK) == 0) + + if (access(file, R_OK) == 0) return file; #endif - return NULL; } #endif @@ -406,8 +408,8 @@ copy(frname, toname) execl("/bin/cp", "cp", frname, toname, (char *) NULL); _exit(1); /* shouldn't happen */ } - while (wait(&status) != pid); - return status == 0; + waitpid(pid, &status, 0); + return (WIFEXITED(status) && WEXITSTATUS(status) == 0); } BUFFER * @@ -480,16 +482,11 @@ struct filelist { /* * return list of file names that match the name in buf. - * System V version. listing is a flag indicating whether the - * list is being used for printing a listing rather than - * completion. In that case, trailing * and / are put on - * for executables and directories. The list is not sorted. */ LIST * -make_file_list(buf, listing) +make_file_list(buf) char *buf; - int listing; { char *dir, *file, *cp; int len, preflen; @@ -558,8 +555,6 @@ make_file_list(buf, listing) */ if ((preflen + MAXNAMLEN) > NFILEN) return (NULL); - if ((strlen(dir) + MAXNAMLEN) > NFILEN) - listing = 0; /* loop over the specified directory, making up the list of files */ @@ -580,6 +575,8 @@ make_file_list(buf, listing) continue; current = (struct filelist *) malloc(sizeof(struct filelist)); + if (current == NULL) + break; if (snprintf(current->fl_name, sizeof(current->fl_name), "%s%s", prefixx, dent->d_name) > sizeof(current->fl_name)) { free(current); @@ -588,23 +585,21 @@ make_file_list(buf, listing) current->fl_l.l_next = last; current->fl_l.l_name = current->fl_name; last = (LIST *) current; - if (listing) { - if (dent->d_type == DT_DIR) { - strcat(current->fl_name, "/"); - continue; - } else if (dent->d_type != DT_UNKNOWN) - continue; + if (dent->d_type == DT_DIR) { + strcat(current->fl_name, "/"); + continue; + } else if (dent->d_type != DT_UNKNOWN) + continue; - statbuf.st_mode = 0; - if (snprintf(statname, sizeof(statname), "%s/%s", - dir, dent->d_name) > sizeof(statname) - 1) { - continue; - } - if (stat(statname, &statbuf) < 0) - continue; - if (statbuf.st_mode & 040000) - strcat(current->fl_name, "/"); + statbuf.st_mode = 0; + if (snprintf(statname, sizeof(statname), "%s/%s", + dir, dent->d_name) > sizeof(statname) - 1) { + continue; } + if (stat(statname, &statbuf) < 0) + continue; + if (statbuf.st_mode & 040000) + strcat(current->fl_name, "/"); } closedir(dirp); |