From 6a32ada5270c5a948f939b7f14be17c3fba646f0 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Mon, 28 Mar 2022 10:43:10 -0400 Subject: remove more global variables --- menudriver.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/menudriver.c b/menudriver.c index 17e2272..af47f54 100644 --- a/menudriver.c +++ b/menudriver.c @@ -11,13 +11,13 @@ #include #include +#include #include #include +#include #include -#include #include -#include -#include +#include #ifndef PATH_MAX #ifdef _XOPEN_PATH_MAX @@ -46,8 +46,7 @@ void poplast(void); void settings(const char *, const char *, int); int subdir(void); -char next[40] = "", cmd[80], last[40] = "."; -int val; +char next[40] = "", last[40] = "."; static void wait_for_return(void) { @@ -58,12 +57,12 @@ static void wait_for_return(void) } } -static void view_menu(const char *command, const char *filename) +static void view_menu(const char *viewer, const char *filename) { - if (command) { - char buf[PATH_MAX]; - snprintf(buf, sizeof(buf), "%s %s", command, filename); - system(buf); + if (viewer) { + char cmd[PATH_MAX]; + snprintf(cmd, sizeof(cmd), "%s %s", viewer, filename); + system(cmd); return; } @@ -99,7 +98,7 @@ static void view_file(const char *viewer, const char *filename) if (st.st_mode & S_IXUSR) { #endif snprintf(cmd, sizeof(cmd), "./%s", filename); - fastback = 0; + //fastback = 0; } else { snprintf(cmd, sizeof(cmd), "%s %s", viewer, filename); } @@ -116,10 +115,13 @@ int main(int argc, char *argv[]) char *viewer = getenv("VIEWER"); char *fb = getenv("RETURNTOMENU"); char *menuviewer = getenv("MENUVIEWER"); + char *dir = "."; if (argc > 2) { fprintf(stderr, "menudriver: wrong number of args\n"); exit(1); + } else if (argc == 2) { + dir = argv[1]; } if (!viewer) { @@ -130,13 +132,14 @@ int main(int argc, char *argv[]) fastback = !(*fb == 'N' || *fb == 'n'); } - menudrive(menuviewer, viewer, argc == 1 ? "." : argv[1]); + menudrive(menuviewer, viewer, dir); } /* checks if last is a number (and if so leaves value in val) */ int lastval(void) { /* special case, have just entered subdir */ + int val; if (strcmp(last, ".") == 0 && subdir()) { poplast(); @@ -216,11 +219,11 @@ void menudrive(const char *menuviewer, const char *viewer, char *dir) } if (strcmp(next, "+") == 0 && lastval()) { - (void)sprintf(next, "%d", val + 1); + (void)sprintf(next, "%d", atoi(last) + 1); } if (strcmp(next, "-") == 0 && lastval()) { - (void)sprintf(next, "%d", val - 1); + (void)sprintf(next, "%d", atoi(last) - 1); } if (stat(next, &st) == 0) { @@ -232,7 +235,8 @@ void menudrive(const char *menuviewer, const char *viewer, char *dir) if (S_ISDIR(st.st_mode)) { /* directory */ char hold[PATH_MAX]; if (!getcwd(hold, sizeof(hold))) { - fprintf(stderr, "panic: cwd too long\n"), exit(1); + fprintf(stderr, "panic: cwd too long\n"); + exit(1); } if (chdir(next) == -1 || stat("contents", &st)) { @@ -408,7 +412,6 @@ void pushlast(void) return; } - /* suppressed 'cos interferes with special case in lastval() */ strcpy(lastp, last); lastp += n + 1; strcpy(last, "."); -- cgit v1.2.1