Like the GNU ls first print a line with the directory path before printing
files in the directory, which will not have a directory component, but only
if there is more than one argument. Also, for arguments that are paths to
files, print the full path of the file.

Signed-off-by: Glenn Washburn <developm...@efficientek.com>
---
 grub-core/commands/ls.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/grub-core/commands/ls.c b/grub-core/commands/ls.c
index 6a45b998e30d..10939eefeab5 100644
--- a/grub-core/commands/ls.c
+++ b/grub-core/commands/ls.c
@@ -91,6 +91,7 @@ struct grub_ls_list_files_ctx
   int all;
   int human;
   int longlist;
+  int print_dirhdr;
 };
 
 /* Helper for grub_ls_list_files.  */
@@ -98,11 +99,18 @@ static int
 print_files (const char *filename, const struct grub_dirhook_info *info,
             void *data)
 {
+  char *pathname = NULL;
   struct grub_ls_list_files_ctx *ctx = data;
 
   if ((! ctx->all) && (filename[0] == '.'))
     return 0;
 
+  if (ctx->print_dirhdr)
+    {
+      grub_printf ("%s:\n", ctx->dirname);
+      ctx->print_dirhdr = 0;
+    }
+
   if (! ctx->longlist)
     {
       if (ctx->filename != NULL)
@@ -114,7 +122,6 @@ print_files (const char *filename, const struct 
grub_dirhook_info *info,
   if (! info->dir)
     {
       grub_file_t file;
-      char *pathname;
 
       if (ctx->dirname[grub_strlen (ctx->dirname) - 1] == '/')
        pathname = grub_xasprintf ("%s%s", ctx->dirname, filename);
@@ -140,7 +147,6 @@ print_files (const char *filename, const struct 
grub_dirhook_info *info,
       else
        grub_xputs ("????????????");
 
-      grub_free (pathname);
       grub_errno = GRUB_ERR_NONE;
     }
   else
@@ -162,7 +168,10 @@ print_files (const char *filename, const struct 
grub_dirhook_info *info,
                     datetime.day, datetime.hour,
                     datetime.minute, datetime.second);
     }
-  grub_printf ("%s%s\n", filename, info->dir ? "/" : "");
+  grub_printf ("%s%s\n", (ctx->filename) ? pathname : filename,
+                        info->dir ? "/" : "");
+
+  grub_free (pathname);
 
   return 0;
 }
@@ -181,7 +190,7 @@ print_file (const char *filename, const struct 
grub_dirhook_info *info,
 }
 
 static grub_err_t
-grub_ls_list_files (char *dirname, int longlist, int all, int human)
+grub_ls_list_files (char *dirname, int longlist, int all, int human, int 
dirhdr)
 {
   char *device_name;
   grub_fs_t fs;
@@ -229,7 +238,8 @@ grub_ls_list_files (char *dirname, int longlist, int all, 
int human)
        .filename = NULL,
        .all = all,
        .human = human,
-       .longlist = longlist
+       .longlist = longlist,
+       .print_dirhdr = dirhdr
       };
 
       (fs->fs_dir) (dev, path, print_files, &ctx);
@@ -244,6 +254,7 @@ grub_ls_list_files (char *dirname, int longlist, int all, 
int human)
          grub_errno = GRUB_ERR_NONE;
 
          /* PATH might be a regular file.  */
+         ctx.print_dirhdr = 0;
          ctx.filename = grub_strrchr (dirname, '/') + 1;
          ctx.dirname = grub_strndup (dirname, ctx.filename - dirname);
          if (ctx.dirname == NULL)
@@ -279,8 +290,8 @@ grub_cmd_ls (grub_extcmd_context_t ctxt, int argc, char 
**args)
     grub_ls_list_devices (state[0].set);
   else
     for (i = 0; i < argc; i++)
-      grub_ls_list_files (args[i], state[0].set, state[2].set,
-                         state[1].set);
+      grub_ls_list_files (args[i], state[0].set, state[2].set, state[1].set,
+                         argc > 1);
 
   return 0;
 }
-- 
2.34.1


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

Reply via email to