bug-fileutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

patch for ls to change owner length


From: John Newbigin
Subject: patch for ls to change owner length
Date: Sat, 08 Mar 2003 17:18:56 +1100

If you have usernames or group names longer than 8 characters it is
useful to be able to show the full name length  The attached patch
allows you to set the width of the user name and group name columns via
environment variables or the command line.

The patch needs a bit of polish but does work OK.

John.


--- ls.c.orig   Wed Mar  5 22:12:04 2003
+++ ls.c        Wed Mar  5 22:20:18 2003
@@ -619,6 +619,10 @@
    inhibits the use of TAB characters for separating columns.  -T */
 static int tabsize;
 
+/* The number of characters of user name or group name to show in a file 
listing */
+static int user_width = 8;
+static int group_width = 8;
+
 /* Nonzero means we are listing the working directory because no
    non-option arguments were given. */
 
@@ -665,7 +669,9 @@
   SHOW_CONTROL_CHARS_OPTION,
   SI_OPTION,
   SORT_OPTION,
-  TIME_OPTION
+  TIME_OPTION,
+  USER_WIDTH_OPTION,
+  GROUP_WIDTH_OPTION
 };
 
 static struct option const long_options[] =
@@ -703,6 +709,8 @@
   {"time", required_argument, 0, TIME_OPTION},
   {"color", optional_argument, 0, COLOR_OPTION},
   {"block-size", required_argument, 0, BLOCK_SIZE_OPTION},
+  {"user-width", required_argument, 0, USER_WIDTH_OPTION},
+  {"group-width", required_argument, 0, GROUP_WIDTH_OPTION},
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
   {NULL, 0, NULL, 0}
@@ -1070,6 +1078,24 @@
        }
     }
 
+  if ((p = getenv ("LS_USER_WIDTH")) && *p)
+    {
+      if (xstrtol (p, NULL, 0, &tmp_long, NULL) == LONGINT_OK
+         && 0 < tmp_long && tmp_long <= INT_MAX)
+       {
+         user_width = (int) tmp_long;
+       }
+    }
+
+  if ((p = getenv ("LS_GROUP_WIDTH")) && *p)
+    {
+      if (xstrtol (p, NULL, 0, &tmp_long, NULL) == LONGINT_OK
+         && 0 < tmp_long && tmp_long <= INT_MAX)
+       {
+         group_width = (int) tmp_long;
+       }
+    }
+
 #ifdef TIOCGWINSZ
   {
     struct winsize ws;
@@ -1280,6 +1306,22 @@
          tabsize = (int) tmp_long;
          break;
 
+       case USER_WIDTH_OPTION:
+         if (xstrtol (optarg, NULL, 0, &tmp_long, NULL) != LONGINT_OK
+             || tmp_long < 0 || tmp_long > INT_MAX)
+           error (EXIT_FAILURE, 0, _("invalid user_width size: %s"),
+                  quotearg (optarg));
+         user_width = (int) tmp_long;
+         break;
+
+       case GROUP_WIDTH_OPTION:
+         if (xstrtol (optarg, NULL, 0, &tmp_long, NULL) != LONGINT_OK
+             || tmp_long < 0 || tmp_long > INT_MAX)
+           error (EXIT_FAILURE, 0, _("invalid group_width size: %s"),
+                  quotearg (optarg));
+         group_width = (int) tmp_long;
+         break;
+
        case 'U':
          sort_type = sort_none;
          sort_type_specified = 1;
@@ -2467,9 +2509,11 @@
        or LONGEST_HUMAN_READABLE integer,
      9 spaces, one following each of these fields, and
      1 trailing NUL byte.  */
+  /* what about user names and group names?  are these part of the 7 
LONGEST_HUMAN_READABLE?
+   * I will add them just to be safe for now */
   char init_bigbuf[7 * LONGEST_HUMAN_READABLE + 10
                   + (LONGEST_HUMAN_READABLE < 24 ? 24 : LONGEST_HUMAN_READABLE)
-                  + 9 + 1];
+                  + 9 + 1 + user_width + group_width];
   char *buf = init_bigbuf;
   size_t bufsize = sizeof (init_bigbuf);
   size_t s;
@@ -2532,18 +2576,18 @@
 
   user_name = (numeric_ids ? NULL : getuser (f->stat.st_uid));
   if (user_name)
-    sprintf (p, "%-8.8s ", user_name);
+    sprintf (p, "%-*.*s ", user_width, user_width, user_name);
   else
-    sprintf (p, "%-8u ", (unsigned int) f->stat.st_uid);
+    sprintf (p, "%-*u ", user_width, (unsigned int) f->stat.st_uid);
   p += strlen (p);
 
   if (!inhibit_group)
     {
       char *group_name = (numeric_ids ? NULL : getgroup (f->stat.st_gid));
       if (group_name)
-       sprintf (p, "%-8.8s ", group_name);
+       sprintf (p, "%-*.*s ", group_width, group_width, group_name);
       else
-       sprintf (p, "%-8u ", (unsigned int) f->stat.st_gid);
+       sprintf (p, "%-*u ", group_width, (unsigned int) f->stat.st_gid);
       p += strlen (p);
     }
 
@@ -3301,6 +3345,7 @@
       printf (_("\
   -g                         (ignored)\n\
   -G, --no-group             inhibit display of group information\n\
+      --group-width=COLS     format group names to a maximum of COLS instead 
of 8\n\
   -h, --human-readable  print sizes in human readable format (e.g., 1K 234M 
2G)\n\
       --si                   likewise, but use powers of 1000 not 1024\n\
   -H                         same as `--si' for now; soon to change\n\
@@ -3342,6 +3387,7 @@
                                with -l: show access time and sort by name\n\
                                otherwise: sort by access time\n\
   -U                         do not sort; list entries in directory order\n\
+      --user-width=COLS      format user names to a maximum of COLS instead of 
8\n\
   -v                         sort by version\n\
   -w, --width=COLS           assume screen width instead of current value\n\
   -x                         list entries by lines instead of by columns\n\

reply via email to

[Prev in Thread] Current Thread [Next in Thread]