aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2019-04-26 10:11:47 -0700
committerRob Landley <rob@landley.net>2019-04-26 15:17:23 -0500
commitbd6e9d030cbf709d40fb480449de200d49449dc8 (patch)
treedfefc6803839011b3b1f92c35b0131f51d25b1ae
parent81220b8090cc02158510fc6557b9cfd72e21f56a (diff)
downloadtoybox-bd6e9d030cbf709d40fb480449de200d49449dc8.tar.gz
more: better behavior with directories.
Also fix the non-tty output. Also tweak our output so the tests pass with TEST_HOST=1 too.
-rw-r--r--tests/more.test5
-rw-r--r--toys/pending/more.c41
2 files changed, 34 insertions, 12 deletions
diff --git a/tests/more.test b/tests/more.test
index 4dcf6d85..ac701f77 100644
--- a/tests/more.test
+++ b/tests/more.test
@@ -9,6 +9,9 @@ line1
line2
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
diff --git a/toys/pending/more.c b/toys/pending/more.c
index 79bb7bf9..dbd429b4 100644
--- a/toys/pending/more.c
+++ b/toys/pending/more.c
@@ -39,7 +39,7 @@ static void signal_handler(int sig)
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, ...)
@@ -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)
{
- if (toys.optc > 1) show_file_header(name);
- xsendfile(fd, 1);
+ struct stat st;
+
+ if (!more_directory(name, &st)) {
+ show_file_header(name);
+ fflush(stdout);
+ xsendfile(fd, 1);
+ }
}
void more_main()
{
int ch, input_key = 0, show_prompt;
unsigned rows = 24, cols = 80, row = 0, col = 0;
- struct stat st;
+ struct stat st;
struct termios newf;
FILE *fp, *cin;
@@ -97,18 +111,23 @@ void more_main()
sigatexit(signal_handler);
do {
- fp = stdin;
- if (*toys.optargs && !(fp = fopen(*toys.optargs, "r"))) {
- perror_msg("%s", *toys.optargs);
+ char *filename = *toys.optargs;
+
+ st.st_size = show_prompt = col = row = 0;
+ 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;
+ }
}
- st.st_size = show_prompt = col = row = 0;
- fstat(fileno(fp), &st);
+
terminal_size(&cols, &rows);
rows--;
if (toys.optc > 1) {
- show_file_header(*toys.optargs);
+ show_file_header(filename);
row += 3;
}
@@ -120,7 +139,7 @@ void more_main()
(long long)st.st_size);
else
input_key = prompt(cin, "--More--");
- if (input_key == 'q') goto stop;
+ if (input_key == 'q') goto stop;
col = row = show_prompt = 0;
terminal_size(&cols, &rows);