more: better behavior with directories.

Also fix the non-tty output.

Also tweak our output so the tests pass with TEST_HOST=1 too.
This commit is contained in:
Elliott Hughes 2019-04-26 10:11:47 -07:00 committed by Rob Landley
parent 81220b8090
commit bd6e9d030c
2 changed files with 35 additions and 13 deletions

View File

@ -9,6 +9,9 @@ line1
line2 line2
EOF EOF
testing "non-tty" "more file1 | cat -" "line1\nline2\n" "" "" # For non-tty output, headers are shown even if there's only one file.
testing "non-tty" "more file1 | cat -" "::::::::::::::\nfile1\n::::::::::::::\nline1\nline2\n" "" ""
testing "directory" "more ." "\n*** .: directory ***\n\n" "" ""
rm file1 rm file1

View File

@ -39,7 +39,7 @@ static void signal_handler(int sig)
static void show_file_header(const char *name) static void show_file_header(const char *name)
{ {
printf(":::::::::::::::::::::::\n%s\n:::::::::::::::::::::::\n", name); printf("::::::::::::::\n%s\n::::::::::::::\n", name);
} }
static int prompt(FILE *cin, const char* fmt, ...) static int prompt(FILE *cin, const char* fmt, ...)
@ -65,17 +65,31 @@ static int prompt(FILE *cin, const char* fmt, ...)
} }
} }
static int more_directory(char *path, struct stat *st)
{
if (!stat(path, st) && S_ISDIR(st->st_mode)) {
printf("\n*** %s: directory ***\n\n", path);
return 1;
}
return 0;
}
static void do_cat_operation(int fd, char *name) static void do_cat_operation(int fd, char *name)
{ {
if (toys.optc > 1) show_file_header(name); struct stat st;
xsendfile(fd, 1);
if (!more_directory(name, &st)) {
show_file_header(name);
fflush(stdout);
xsendfile(fd, 1);
}
} }
void more_main() void more_main()
{ {
int ch, input_key = 0, show_prompt; int ch, input_key = 0, show_prompt;
unsigned rows = 24, cols = 80, row = 0, col = 0; unsigned rows = 24, cols = 80, row = 0, col = 0;
struct stat st; struct stat st;
struct termios newf; struct termios newf;
FILE *fp, *cin; FILE *fp, *cin;
@ -97,18 +111,23 @@ void more_main()
sigatexit(signal_handler); sigatexit(signal_handler);
do { do {
fp = stdin; char *filename = *toys.optargs;
if (*toys.optargs && !(fp = fopen(*toys.optargs, "r"))) {
perror_msg("%s", *toys.optargs);
goto next_file;
}
st.st_size = show_prompt = col = row = 0; st.st_size = show_prompt = col = row = 0;
fstat(fileno(fp), &st); if (!filename) fp = stdin;
else {
if (more_directory(filename, &st)) goto next_file;
if (!(fp = fopen(filename, "r"))) {
perror_msg("%s", filename);
goto next_file;
}
}
terminal_size(&cols, &rows); terminal_size(&cols, &rows);
rows--; rows--;
if (toys.optc > 1) { if (toys.optc > 1) {
show_file_header(*toys.optargs); show_file_header(filename);
row += 3; row += 3;
} }
@ -120,7 +139,7 @@ void more_main()
(long long)st.st_size); (long long)st.st_size);
else else
input_key = prompt(cin, "--More--"); input_key = prompt(cin, "--More--");
if (input_key == 'q') goto stop; if (input_key == 'q') goto stop;
col = row = show_prompt = 0; col = row = show_prompt = 0;
terminal_size(&cols, &rows); terminal_size(&cols, &rows);