bug-coreutils
[Top][All Lists]
Advanced

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

Re: who(1) makes trailing whitespace


From: Jim Meyering
Subject: Re: who(1) makes trailing whitespace
Date: Thu, 10 Jul 2003 13:30:18 +0200

Dan Jacobson <address@hidden> wrote:
> If it matters to you, who(1) makes wasteful trailing whitespace,
> $ who|cat -e
> jidanni  :0           Jun 28 01:23         $
> jidanni  pts/0        Jun 28 01:23 (:0)    $
> jidanni  pts/1        Jun 28 01:23 (:0)    $
> root     pts/6        Jun 28 13:36 (:0.0)  $

Thank you for reporting that.  I've fixed it.
The fix relies on using asprintf, and as such pulled in lots of
new files in both lib/ and m4/.  Here's the change to src/who.c:

        * src/who.c (print_line): Rewrite to use asprintf, in order to be
        able to avoid emitting trailing spaces.  Reported by Dan Jacobson.

Index: src/who.c
===================================================================
RCS file: /fetish/cu/src/who.c,v
retrieving revision 1.80
retrieving revision 1.81
diff -u -p -u -r1.80 -r1.81
--- src/who.c   28 Jun 2003 10:30:04 -0000      1.80
+++ src/who.c   4 Jul 2003 12:25:29 -0000       1.81
@@ -249,22 +249,68 @@ print_line (const char *user, const char
            const char *time_str, const char *idle, const char *pid,
            const char *comment, const char *exitstr)
 {
-  printf ("%-8.8s", user ? user : "   .");
-  if (include_mesg)
-    printf (" %c", state);
-  printf (" %-12s", line);
-  printf (" %-12s", time_str);
+  static char mesg[3] = { ' ', 'x', '\0' };
+  char *buf;
+  char x_idle[1 + IDLESTR_LEN + 1];
+  char x_pid[1 + INT_STRLEN_BOUND (pid_t) + 1];
+  char *x_exitstr;
+  int err;
+
+  mesg[1] = state;
+
   if (include_idle && !short_output)
-    printf (" %-6s", idle);
+    sprintf (x_idle, " %-6s", idle);
+  else
+    *x_idle = '\0';
+
   if (!short_output)
-    printf (" %10s", pid);
-  /* FIXME: it's not really clear whether the following should be in
-     short_output.  A strict reading of SUSv2 would suggest not, but
-     I haven't seen any implementations that actually work that way... */
-  printf (" %-8s", comment);
-  if (include_exit && exitstr && *exitstr)
-    printf (" %-12s", exitstr);
-  putchar ('\n');
+    sprintf (x_pid, " %10s", pid);
+  else
+    *x_pid = '\0';
+
+  x_exitstr = xmalloc (include_exit ? 1 + MAX (12, strlen (exitstr)) + 1 : 1);
+  if (include_exit)
+    sprintf (x_exitstr, " %-12s", exitstr);
+  else
+    *x_exitstr = '\0';
+
+  err = asprintf (&buf,
+                 "%-8.8s"
+                 "%s"
+                 " %-12s"
+                 " %-12s"
+                 "%s"
+                 "%s"
+                 " %-8s"
+                 "%s"
+                 ,
+                 user ? user : "   .",
+                 include_mesg ? mesg : "",
+                 line,
+                 time_str,
+                 x_idle,
+                 x_pid,
+                 /* FIXME: it's not really clear whether the following
+                    field should be in the short_output.  A strict reading
+                    of SUSv2 would suggest not, but I haven't seen any
+                    implementations that actually work that way... */
+                 comment,
+                 x_exitstr
+                 );
+  if (err == -1)
+    xalloc_die ();
+
+  {
+    /* Remove any trailing spaces.  */
+    char *p = buf + strlen (buf);
+    while (*--p == ' ')
+      /* empty */;
+    *(p + 1) = '\0';
+  }
+
+  puts (buf);
+  free (buf);
+  free (x_exitstr);
 }
 
 /* Send properly parsed USER_PROCESS info to print_line */




reply via email to

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