From 5307eca7de2caf1d4efb8ee3e802a69a1a076376 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Fri, 26 Jan 2001 01:52:43 +0000 Subject: [PATCH] Make ls understand termios. -Erik --- coreutils/ls.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++- ls.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 120 insertions(+), 2 deletions(-) diff --git a/coreutils/ls.c b/coreutils/ls.c index 64e5bf828..215c58bb6 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -60,6 +60,10 @@ static const int COLUMN_GAP = 2; /* includes the file type char, if present */ #endif #include +#include +#include +#include + #ifndef NAJOR #define MAJOR(dev) (((dev)>>8)&0xff) #define MINOR(dev) ((dev)&0xff) @@ -181,6 +185,31 @@ static int status = EXIT_SUCCESS; unsigned long ls_disp_hr = KILOBYTE; #endif +/* sparc termios is broken -- use old termio handling. */ +#ifdef BB_FEATURE_USE_TERMIOS +# if #cpu(sparc) +# include +# define termios termio +# define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp) +# define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp) +# else +# include +# define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp) +# define getTermSettings(fd,argp) tcgetattr(fd, argp); +# endif + +FILE *cin; + +static struct termios initial_settings, new_settings; + +static void gotsig(int sig) +{ + setTermSettings(fileno(cin), &initial_settings); + putchar('\n'); + exit(EXIT_FAILURE); +} +#endif /* BB_FEATURE_USE_TERMIOS */ + static int my_stat(struct dnode *cur) { #ifdef BB_FEATURE_LS_FOLLOWLINKS @@ -707,6 +736,9 @@ extern int ls_main(int argc, char **argv) int opt; int oi, ac; char **av; +#if defined BB_FEATURE_AUTOWIDTH && defined BB_FEATURE_USE_TERMIOS + struct winsize win = { 0, 0, 0, 0 }; +#endif disp_opts= DISP_NORMAL; style_fmt= STYLE_AUTO; @@ -719,10 +751,34 @@ extern int ls_main(int argc, char **argv) time_fmt= TIME_MOD; #endif #ifdef BB_FEATURE_AUTOWIDTH +#ifdef BB_FEATURE_USE_TERMIOS + cin = fopen("/dev/tty", "r"); + if (!cin) + cin = fopen("/dev/console", "r"); + getTermSettings(fileno(cin), &initial_settings); + new_settings = initial_settings; + new_settings.c_cc[VMIN] = 1; + new_settings.c_cc[VTIME] = 0; + new_settings.c_lflag &= ~ICANON; + new_settings.c_lflag &= ~ECHO; + setTermSettings(fileno(cin), &new_settings); + + ioctl(fileno(stdout), TIOCGWINSZ, &win); + if (win.ws_row > 4) + column_width = win.ws_row - 2; + if (win.ws_col > 0) + terminal_width = win.ws_col - 1; + + (void) signal(SIGINT, gotsig); + (void) signal(SIGQUIT, gotsig); + (void) signal(SIGTERM, gotsig); +#else + terminal_width = TERMINAL_WIDTH; column_width = COLUMN_WIDTH; - tabstops = 8; #endif +#endif + tabstops = 8; nfiles=0; /* process options */ @@ -908,6 +964,9 @@ extern int ls_main(int argc, char **argv) } } +#ifdef BB_FEATURE_USE_TERMIOS + gotsig(0); +#endif return(status); print_usage_message: diff --git a/ls.c b/ls.c index 64e5bf828..215c58bb6 100644 --- a/ls.c +++ b/ls.c @@ -60,6 +60,10 @@ static const int COLUMN_GAP = 2; /* includes the file type char, if present */ #endif #include +#include +#include +#include + #ifndef NAJOR #define MAJOR(dev) (((dev)>>8)&0xff) #define MINOR(dev) ((dev)&0xff) @@ -181,6 +185,31 @@ static int status = EXIT_SUCCESS; unsigned long ls_disp_hr = KILOBYTE; #endif +/* sparc termios is broken -- use old termio handling. */ +#ifdef BB_FEATURE_USE_TERMIOS +# if #cpu(sparc) +# include +# define termios termio +# define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp) +# define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp) +# else +# include +# define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp) +# define getTermSettings(fd,argp) tcgetattr(fd, argp); +# endif + +FILE *cin; + +static struct termios initial_settings, new_settings; + +static void gotsig(int sig) +{ + setTermSettings(fileno(cin), &initial_settings); + putchar('\n'); + exit(EXIT_FAILURE); +} +#endif /* BB_FEATURE_USE_TERMIOS */ + static int my_stat(struct dnode *cur) { #ifdef BB_FEATURE_LS_FOLLOWLINKS @@ -707,6 +736,9 @@ extern int ls_main(int argc, char **argv) int opt; int oi, ac; char **av; +#if defined BB_FEATURE_AUTOWIDTH && defined BB_FEATURE_USE_TERMIOS + struct winsize win = { 0, 0, 0, 0 }; +#endif disp_opts= DISP_NORMAL; style_fmt= STYLE_AUTO; @@ -719,10 +751,34 @@ extern int ls_main(int argc, char **argv) time_fmt= TIME_MOD; #endif #ifdef BB_FEATURE_AUTOWIDTH +#ifdef BB_FEATURE_USE_TERMIOS + cin = fopen("/dev/tty", "r"); + if (!cin) + cin = fopen("/dev/console", "r"); + getTermSettings(fileno(cin), &initial_settings); + new_settings = initial_settings; + new_settings.c_cc[VMIN] = 1; + new_settings.c_cc[VTIME] = 0; + new_settings.c_lflag &= ~ICANON; + new_settings.c_lflag &= ~ECHO; + setTermSettings(fileno(cin), &new_settings); + + ioctl(fileno(stdout), TIOCGWINSZ, &win); + if (win.ws_row > 4) + column_width = win.ws_row - 2; + if (win.ws_col > 0) + terminal_width = win.ws_col - 1; + + (void) signal(SIGINT, gotsig); + (void) signal(SIGQUIT, gotsig); + (void) signal(SIGTERM, gotsig); +#else + terminal_width = TERMINAL_WIDTH; column_width = COLUMN_WIDTH; - tabstops = 8; #endif +#endif + tabstops = 8; nfiles=0; /* process options */ @@ -908,6 +964,9 @@ extern int ls_main(int argc, char **argv) } } +#ifdef BB_FEATURE_USE_TERMIOS + gotsig(0); +#endif return(status); print_usage_message: