From 7370aa2b4eea3b6a82b857d67a2bb03956a1e87d Mon Sep 17 00:00:00 2001 From: anon Date: Wed, 13 Nov 2024 13:57:10 +0100 Subject: [PATCH] rename `filepath` to `prepend_path`; require a prefix to be explicitly passed; prepare `prependpath`, the legacy global for refactoring --- source/edit.c | 27 ++++++++++++++------------- source/egrep.y | 2 +- source/find.c | 2 +- source/global.h | 6 +++--- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/source/edit.c b/source/edit.c index 7928eff..fff2a53 100644 --- a/source/edit.c +++ b/source/edit.c @@ -85,13 +85,13 @@ void editall(void) { } /* call the editor */ -void edit(char *file, const char *const linenum) { +void edit(const char *filename, const char *const linenum) { const char *const editor_basename = basename(editor); char msg[MSGLEN + 1]; /* message */ char plusnum[NUMLEN + 20]; /* line number option: allow space for wordy line# flag */ - file = filepath(file); - snprintf(msg, sizeof(msg), "%s +%s %s", basename(editor), linenum, file); + filename = prepend_path(prependpath, filename); + snprintf(msg, sizeof(msg), "%s +%s %s", basename(editor), linenum, filename); postmsg(msg); snprintf(plusnum, sizeof(plusnum), lineflag, linenum); @@ -103,15 +103,15 @@ void edit(char *file, const char *const linenum) { const char *const shit_pagers[] = {"page", "more", NULL}; for(const char *const *sp = shit_pagers; *sp != NULL; sp++) { if(!strcmp(editor_basename, *sp)) { - execute(editor, editor, plusnum, file, "/dev/null", NULL); + execute(editor, editor, plusnum, filename, "/dev/null", NULL); goto end; } } if(lineflagafterfile) { - execute(editor, editor, file, plusnum, NULL); + execute(editor, editor, filename, plusnum, NULL); } else { - execute(editor, editor, plusnum, file, NULL); + execute(editor, editor, plusnum, filename, NULL); } end: @@ -119,13 +119,14 @@ end: } /* if requested, prepend a path to a relative file name */ +const char * prepend_path(const char * prepand_with, const char * file) { + static char path[PATHLEN + 1]; // XXX -char *filepath(char *file) { - static char path[PATHLEN + 1]; + if (!prepand_with + || *file == '/') { + return file; + } - if(prependpath != NULL && *file != '/') { - (void)snprintf(path, sizeof(path), "%s/%s", prependpath, file); - file = path; - } - return (file); + snprintf(path, sizeof(path), "%s/%s", prependpath, file); + return path; } diff --git a/source/egrep.y b/source/egrep.y index 24b05e7..d5d7926 100644 --- a/source/egrep.y +++ b/source/egrep.y @@ -554,7 +554,7 @@ size_t read_next_chunk(char **p, FILE *fptr) return fread(*p, sizeof(**p), buf_end - *p, fptr); } -int egrep(char * file, FILE *output, char *format) { +int egrep(const char * file, FILE *output, char *format) { char *p; unsigned int cstat; int ccount; diff --git a/source/find.c b/source/find.c index 3078a2f..78d1c68 100644 --- a/source/find.c +++ b/source/find.c @@ -578,7 +578,7 @@ char *findregexp(const char *egreppat) { /* search the files */ for(i = 0; i < nsrcfiles; ++i) { - char *file = filepath(srcfiles[i]); + const char * file = prepend_path(prependpath, srcfiles[i]); progress("Search", searchcount, nsrcfiles); if(egrep(file, refsfound, "%s %ld ") < 0) { diff --git a/source/global.h b/source/global.h index e9c3f51..de62c26 100644 --- a/source/global.h +++ b/source/global.h @@ -193,7 +193,7 @@ extern int rl_point; /* cscope functions called from more than one function or between files */ -char *filepath(char *file); +const char * prepend_path(const char * prepand_with, const char * file); char *findsymbol(const char *pattern); char *finddef(const char *pattern); char *findcalledby(const char *pattern); @@ -253,7 +253,7 @@ void dispinit(void); void display(void); void redisplay(void); void drawscrollbar(int top, int bot); -void edit(char *file, const char *const linenum); +void edit(const char * filename, const char *const linenum); void editall(void); void editref(int); void entercurses(void); @@ -293,7 +293,7 @@ bool writerefsfound(void); int findinit(const char *pattern_); -int egrep(char *file, FILE *output, char *format); +int egrep(const char * file, FILE *output, char *format); int hash(const char * ss); int execute(char *a, ...); long dbseek(long offset);