diff options
author | Jakob Kaivo <jkk@ung.org> | 2022-03-27 22:29:36 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2022-03-27 22:29:36 -0400 |
commit | 9755a239808a1ae095d9a94773456a1581eb792d (patch) | |
tree | 8ff59eaaaf60aef21d83ce1c9417886b5e02b9bb | |
parent | b521d989a5cb854b5c1e78d54a46106542237c11 (diff) |
clean up callshell(), add wait_for_return()
-rw-r--r-- | menudriver.c | 43 |
1 files changed, 24 insertions, 19 deletions
diff --git a/menudriver.c b/menudriver.c index 7d517ef..989a06a 100644 --- a/menudriver.c +++ b/menudriver.c @@ -9,6 +9,7 @@ * Revised to C11 standard and made 64bit compatible, January 2020 * *------------------------------------------------------------------------*/ +#include <errno.h> #include <limits.h> #include <stdio.h> #include <stdlib.h> @@ -55,6 +56,14 @@ int val, ok = 0; #include <string.h> #define index(s,c) strchr(s,c) +static void wait_for_return(void) { + printf("[Hit return to continue]"); + fflush(stdout); + while (getchar() != '\n') { + /* keep reading until new-line */ + } +} + int main(int argc, char *argv[]) { char *v = getenv("VIEWER"); @@ -203,8 +212,7 @@ void menudrive(char *dir) strcat(cmd, next); system(cmd); if (fastback) { - printf("[Hit return to continue]"); - while (getchar() != '\n') ; + wait_for_return(); } } else { @@ -249,11 +257,7 @@ void menudrive(char *dir) } else if (strcmp(next, "???") == 0) { /* ask to see menudriver settings */ settings(); - printf("[Hit return to continue]"); - while (getchar() != '\n') { - /* keep reading until new-line */ - } - + wait_for_return(); next[0] = '\0'; } else if (strcmp(next, "q") == 0 || strcmp(next, "/q") == 0) { @@ -279,8 +283,7 @@ void menudrive(char *dir) callshell(syscm); /* `system' always gets /bin/sh */ } - printf("[Hit return to continue]"); - while (getchar() != '\n') ; + wait_for_return(); next[0] = '\0'; } else { @@ -322,8 +325,7 @@ void singleton(char *fil) } if (!fastback) { - printf("[Hit return to continue]"); - while (getchar() != '\n') ; + wait_for_return(); } exit(0); @@ -336,7 +338,6 @@ void singleton(char *fil) void callshell(char v[]) { static char *shell = NULL; - int pid; if (!shell) { shell = getenv("SHELL"); @@ -346,16 +347,19 @@ void callshell(char v[]) } void (*oldsig)(int) = signal(SIGINT, SIG_IGN); - if ((pid = fork())) { /* parent */ - if (pid == -1) { - perror("UNIX error - cannot create process"); - } + switch (fork()) { + case -1: + perror("UNIX error - cannot create process"); + return; + case 0: + execl(shell, shell, "-c", v, (char *)0); + fprintf(stderr, "%s\n", strerror(errno)); + exit(1); + + default: wait(0); (void)signal(SIGINT, oldsig); - - } else { - execl(shell, shell, "-c", v, (char *)0); } } @@ -434,3 +438,4 @@ void clrscr(void) { system("tput clear"); } + |