diff options
author | toni <matzeton@googlemail.com> | 2016-11-30 18:39:07 +0100 |
---|---|---|
committer | toni <matzeton@googlemail.com> | 2016-11-30 18:39:07 +0100 |
commit | 8a04b833b6fa929b7e86fcdbef6304e8f4c55dcc (patch) | |
tree | 3462429c893381fc7f0f89809227cd651f1a98c5 /dummyshell.c | |
parent | 4cec8224c8e1c954b866abab604cc3842a2cd8a3 (diff) |
dummyshell
Diffstat (limited to 'dummyshell.c')
-rw-r--r-- | dummyshell.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/dummyshell.c b/dummyshell.c new file mode 100644 index 0000000..177423a --- /dev/null +++ b/dummyshell.c @@ -0,0 +1,66 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <termios.h> +#include <fcntl.h> +#include <time.h> + + +static const char txtheader[] = + "**************\n" + "* dummyshell *\n" + "**************\n"; + + +int main(int argc, char** argv) +{ + struct timeval tv; + tv.tv_sec = 1; + tv.tv_usec = 0; + + int flags = fcntl(STDIN_FILENO, F_GETFL, 0); + fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); + + static struct termios oldt, newt; + tcgetattr(STDIN_FILENO, &oldt); + newt = oldt; + newt.c_lflag &= ~(ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &newt); + + printf("%s\n", txtheader); + fd_set fds; + time_t start = time(NULL); + time_t cur; + while (1) { + cur = time(NULL); + double diff = difftime(cur, start); + printf("\r( %0.fs ) [PRESS ANY KEY TO QUIT] ", diff); + fflush(stdout); + fflush(stdin); + FD_ZERO(&fds); + FD_SET(STDIN_FILENO, &fds); + int ret = select(FD_SETSIZE, &fds, NULL, NULL, &tv); + if (ret == 0) { + tv.tv_sec = 1; + tv.tv_usec = 0; + } else { + printf("quit in 3 .. "); + fflush(stdout); + sleep(1); + printf("2 .. "); + fflush(stdout); + sleep(1); + printf("1 .. "); + fflush(stdout); + sleep(1); + printf("\n"); + break; + } + if (FD_ISSET(STDIN_FILENO,&fds)) break; + } + while (getchar() != EOF) {} + + tcsetattr( STDIN_FILENO, TCSANOW, &oldt); + + return 0; +} |