[Toybox] [PATCH] more: better behavior with directories.

enh enh at google.com
Fri Apr 26 10:13:31 PDT 2019


Also fix the non-tty output.

Also tweak our output so the tests pass with TEST_HOST=1 too.
---
 tests/more.test     |  5 ++++-
 toys/pending/more.c | 41 ++++++++++++++++++++++++++++++-----------
 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);
-- 
2.21.0.593.g511ec345e18-goog
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-more-better-behavior-with-directories.patch
Type: text/x-patch
Size: 3198 bytes
Desc: not available
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20190426/48e47fa4/attachment.bin>


More information about the Toybox mailing list