--- /Users/debkantamondal/coreutils-original/src/ls.c	2023-03-13 23:38:10
+++ /Users/debkantamondal/coreutils-9.3/src/ls.c	2025-10-28 23:45:27
@@ -282,6 +282,8 @@
 static void indent (size_t from, size_t to);
 static size_t calculate_columns (bool by_columns);
 static void print_current_files (void);
+static void count_elements_in_container();
+static void containers_list();
 static void print_dir (char const *name, char const *realname,
                        bool command_line_arg);
 static size_t print_file_name_and_frills (const struct fileinfo *f,
@@ -522,6 +524,10 @@
 /* Human-readable options for output, when printing block counts.  */
 static int human_output_opts;
 
+/* --count and --coun-list options control */
+static bool count_mode = false;
+static bool count_mode_with_list = false;
+
 /* The units to use when printing block counts.  */
 static uintmax_t output_block_size;
 
@@ -829,6 +835,8 @@
 enum
 {
   AUTHOR_OPTION = CHAR_MAX + 1,
+  COUNT_OPTION,
+  COUNT_OPTION_WITH_LIST,
   BLOCK_SIZE_OPTION,
   COLOR_OPTION,
   DEREFERENCE_COMMAND_LINE_SYMLINK_TO_DIR_OPTION,
@@ -894,6 +902,8 @@
   {"block-size", required_argument, NULL, BLOCK_SIZE_OPTION},
   {"context", no_argument, 0, 'Z'},
   {"author", no_argument, NULL, AUTHOR_OPTION},
+  {"count", no_argument, NULL, COUNT_OPTION},
+  {"count-list", no_argument, NULL, COUNT_OPTION_WITH_LIST},
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
   {NULL, 0, NULL, 0}
@@ -1543,9 +1553,111 @@
 
       /* If execution reaches here, then the program has been
          continued (after being suspended).  */
+    }
+}
+
+static void count_elements_in_container()
+{
+  size_t file_count = 0, dir_count = 0, link_count = 0;
+
+    for (size_t i = 0; i < cwd_n_used; i++)
+    {
+      struct fileinfo *f = sorted_file[i];
+
+      // Ensure we have stat info
+      if (!f->stat_ok)
+      {
+        if (lstat(f->name, &f->stat) == 0)
+          f->stat_ok = true;
+      }
+
+      if (!f->stat_ok)
+        continue;
+
+      
+
+      mode_t mode = f->stat.st_mode;
+
+      if (S_ISDIR(mode))
+        dir_count++;
+      else if (S_ISLNK(mode))
+        link_count++;
+      else if (S_ISREG(mode))
+        file_count++;
     }
+
+    printf("Directories: %zu | Files: %zu | Links: %zu",
+           dir_count, file_count, link_count);
+    if (count_mode_with_list)
+      printf("\n-------------------------------------------------------------------------------\n");
+    else
+      printf("\n");
 }
 
+static void containers_list()
+{
+  // for (size_t i = 0; i < cwd_n_used; i++)
+      printf("CONTAINING ELEMENTS ARE\n-------------------------------------------------------------------------------\n");
+      // print_current_files();
+      printf("%-30s %-6s %-12s %-10s %-20s\n",
+            "Name", "Type", "Permissions", "Size", "Modified");
+      printf("-------------------------------------------------------------------------------\n");
+
+      for (size_t i = 0; i < cwd_n_used; i++)
+      {
+        struct fileinfo *f = sorted_file[i];
+
+        // Ensure stat info
+        if (!f->stat_ok)
+        {
+          if (lstat(f->name, &f->stat) == 0)
+            f->stat_ok = true;
+        }
+
+        if (!f->stat_ok)
+          continue;
+
+        // File type
+        const char *type = "?";
+        if (S_ISDIR(f->stat.st_mode))
+          type = "DIR";
+        else if (S_ISREG(f->stat.st_mode))
+          type = "FILE";
+        else if (S_ISLNK(f->stat.st_mode))
+          type = "LINK";
+
+        // Permissions string
+        char perms[11];
+        mode_t m = f->stat.st_mode;
+        perms[0] = S_ISDIR(m) ? 'd' : (S_ISLNK(m) ? 'l' : '-');
+        perms[1] = (m & S_IRUSR) ? 'r' : '-';
+        perms[2] = (m & S_IWUSR) ? 'w' : '-';
+        perms[3] = (m & S_IXUSR) ? 'x' : '-';
+        perms[4] = (m & S_IRGRP) ? 'r' : '-';
+        perms[5] = (m & S_IWGRP) ? 'w' : '-';
+        perms[6] = (m & S_IXGRP) ? 'x' : '-';
+        perms[7] = (m & S_IROTH) ? 'r' : '-';
+        perms[8] = (m & S_IWOTH) ? 'w' : '-';
+        perms[9] = (m & S_IXOTH) ? 'x' : '-';
+        perms[10] = '\0';
+
+        // Size
+        long size = (long)f->stat.st_size;
+
+        // Modified time
+        char mod_time[20];
+        struct tm *tm_info = localtime(&f->stat.st_mtime);
+        strftime(mod_time, sizeof(mod_time), "%Y-%m-%d %H:%M", tm_info);
+
+        // Print row (with optional color)
+        set_normal_color();
+        printf("%-30s %-6s %-12s %-10ld %-20s\n",
+               f->name, type, perms, size, mod_time);
+      }
+      printf("\n");
+}
+
+
 /* Setup signal handlers if INIT is true,
    otherwise restore to the default.  */
 
@@ -1844,7 +1956,19 @@
       j = interrupt_signal;
       if (j)
         raise (j);
+    }
+    /* --count: show number of files, directories, and symlinks */
+  if (count_mode)
+  {
+    
+    count_elements_in_container();
+
+    if (count_mode_with_list)
+    {
+       containers_list();
     }
+  }
+
 
   if (dired)
     {
@@ -2124,8 +2248,17 @@
 
         case AUTHOR_OPTION:
           print_author = true;
+          break;
+        
+        case COUNT_OPTION:
+          count_mode = true;
           break;
 
+        case COUNT_OPTION_WITH_LIST:
+          count_mode = true;
+          count_mode_with_list = true;
+          break;
+
         case HIDE_OPTION:
           {
             struct ignore_pattern *hide = xmalloc (sizeof *hide);
@@ -3097,7 +3230,7 @@
                      "sort_type == sort_none" for its initialization
                      of the sorted_file vector.  */
                   sort_files ();
-                  print_current_files ();
+                  if (!count_mode) print_current_files ();
                   clear_files ();
                 }
             }
@@ -3146,7 +3279,7 @@
     }
 
   if (cwd_n_used)
-    print_current_files ();
+    if (!count_mode) print_current_files ();
 }
 
 /* Add 'pattern' to the list of patterns for which files that match are
@@ -5588,7 +5721,19 @@
 \n\
 \n\
 "), stdout);
+fputs (_("\
+  --count                    will print total counts of files, dirs, and links before listing.\
+  \n\
+"),
+       stdout);
       fputs (_("\
+  --count-list               will print counts of files, dirs and links and \n\
+                             also list down all the containing files, dirs and links for current directory.\
+      \n\
+      \n\
+"), 
+       stdout);
+      fputs (_("\
   -Q, --quote-name           enclose entry names in double quotes\n\
 "), stdout);
       fputs (_("\
