Print this page
2849 uptime should use locale settings for current time

@@ -77,11 +77,11 @@
 #define NMAX            (sizeof (dummy.ut_user))
 #define LMAX            (sizeof (dummy.ut_line))
 
 /* Print minimum field widths. */
 #define LOGIN_WIDTH     8
-#define LINE_WIDTH      12
+#define LINE_WIDTH      8
 
 #define DIV60(t)        ((t+30)/60)     /* x/60 rounded */
 
 #ifdef ERR
 #undef ERR

@@ -125,13 +125,12 @@
 static struct   uproc   *findhash(pid_t);
 static time_t   findidle(char *);
 static void     clnarglist(char *);
 static void     showtotals(struct uproc *);
 static void     calctotals(struct uproc *);
-static void     prttime(time_t, char *);
+static void     prttime(time_t, int);
 static void     prtat(time_t *time);
-static void     checkampm(char *str);
 
 static char     *prog;          /* pointer to invocation name */
 static int      header = 1;     /* true if -h flag: don't print heading */
 static int      lflag = 1;      /* set if -l flag; 0 for -s flag: short form */
 static char     *sel_user;      /* login of particular user selected */

@@ -274,11 +273,11 @@
                                 uptime %= (60*60*24);
                                 hrs = uptime / (60*60);
                                 uptime %= (60*60);
                                 mins = uptime / 60;
 
-                                PRINTF((gettext("  up")));
+                                PRINTF((gettext("up")));
                                 if (days > 0)
                                         PRINTF((gettext(
                                             " %d day(s),"), days));
                                 if (hrs > 0 && mins > 0) {
                                         PRINTF((" %2d:%02d,", hrs, mins));

@@ -307,14 +306,16 @@
                 if (firstchar == 'u')   /* uptime command */
                         exit(0);
 
                 if (lflag) {
                         PRINTF((dcgettext(NULL, "User     tty           "
-                            "login@  idle   JCPU   PCPU  what\n", LC_TIME)));
+                            "login@         idle    JCPU    PCPU what\n",
+                            LC_TIME)));
                 } else {
                         PRINTF((dcgettext(NULL,
-                            "User     tty           idle   what\n", LC_TIME)));
+                            "User     tty         idle what\n",
+                            LC_TIME)));
                 }
 
                 if (fflush(stdout) == EOF) {
                         perror((gettext("%s: fflush failed\n"), prog));
                         exit(1);

@@ -483,17 +484,18 @@
                 /* print login name of the user */
                 PRINTF(("%-*.*s ", LOGIN_WIDTH, NMAX, ut->ut_name));
 
                 /* print tty user is on */
                 if (lflag) {
-                        PRINTF(("%-*.*s", LINE_WIDTH, LMAX, ut->ut_line));
+                        PRINTF(("%-*.*s ", LINE_WIDTH, LMAX, ut->ut_line));
                 } else {
                         if (ut->ut_line[0] == 'p' && ut->ut_line[1] == 't' &&
                             ut->ut_line[2] == 's' && ut->ut_line[3] == '/') {
-                                PRINTF(("%-*.3s", LMAX, &ut->ut_line[4]));
+                                PRINTF(("%-*.*s ", LINE_WIDTH, LMAX,
+                                    &ut->ut_line[4]));
                         } else {
-                                PRINTF(("%-*.*s", LINE_WIDTH, LMAX,
+                                PRINTF(("%-*.*s ", LINE_WIDTH, LMAX,
                                     ut->ut_line));
                         }
                 }
 
                 /* print when the user logged in */

@@ -502,15 +504,11 @@
                         prtat(&tim);
                 }
 
                 /* print idle time */
                 idle = findidle(ut->ut_line);
-                if (idle >= 36 * 60) {
-                        PRINTF((dcgettext(NULL, "%2ddays ", LC_TIME),
-                            (idle + 12 * 60) / (24 * 60)));
-                } else
-                        prttime(idle, " ");
+                prttime(idle, 8);
                 showtotals(findhash(ut->ut_pid));
         }
         if (fclose(stdout) == EOF) {
                 perror((gettext("%s: fclose failed"), prog));
                 exit(1);

@@ -535,18 +533,18 @@
         calctotals(up);
 
         if (lflag) {
                 /* print CPU time for all processes & children */
                 /* and need to convert clock ticks to seconds first */
-                prttime((time_t)jobtime, " ");
+                prttime((time_t)jobtime, 8);
 
                 /* print cpu time for interesting process */
                 /* and need to convert clock ticks to seconds first */
-                prttime((time_t)proctime, " ");
+                prttime((time_t)proctime, 8);
         }
         /* what user is doing, current process */
-        PRINTF((" %-.32s\n", doing));
+        PRINTF(("%-.32s\n", doing));
 }
 
 /*
  *  This recursive routine descends the process
  *  tree starting from the given process pointer(up).

@@ -644,56 +642,64 @@
 #define HR      (60 * 60)
 #define DAY     (24 * HR)
 #define MON     (30 * DAY)
 
 /*
- * prttime prints a time in hours and minutes or minutes and seconds.
- * The character string tail is printed at the end, obvious
- * strings to pass are "", " ", or "am".
+ * Prttime prints an elapsed time in hours, minutes, or seconds,
+ * right-justified with the rightmost column always blank.
+ * The second argument is the minimum field width.
  */
 static void
-prttime(time_t tim, char *tail)
+prttime(time_t tim, int width)
 {
-        if (tim >= 60) {
-                PRINTF((dcgettext(NULL, "%3d:%02d", LC_TIME),
-                    (int)tim/60, (int)tim%60));
+        char value[36];
+
+        if (tim >= 36 * 60) {
+                (void) snprintf(value, sizeof (value), "%d:%02d:%02d",
+                    (int)tim / HR, (int)(tim % HR) / 60, (int)tim % 60);
+        } else if (tim >= 60) {
+                (void) snprintf(value, sizeof (value), "%d:%02d",
+                    (int)tim / 60, (int)tim % 60);
         } else if (tim > 0) {
-                PRINTF((dcgettext(NULL, "    %2d", LC_TIME), (int)tim));
+                (void) snprintf(value, sizeof (value), "%d", (int)tim);
         } else {
-                PRINTF(("      "));
+                (void) strcpy(value, "0");
         }
-        PRINTF(("%s", tail));
+        width = (width > 2) ? width - 1 : 1;
+        PRINTF(("%*s ", width, value));
 }
 
 /*
- * prints a 12 hour time given a pointer to a time of day
+ * Prints the ISO date or time given a pointer to a time of day,
+ * left-justfied in a 12-character expanding field with the
+ * rightmost column always blank.
+ * Includes a dcgettext() override in case a message catalog is needed.
  */
 static void
 prtat(time_t *time)
 {
         struct tm       *p;
 
         p = localtime(time);
         if (now - *time <= 18 * HR) {
                 char timestr[50];
+
                 (void) strftime(timestr, sizeof (timestr),
-                    dcgettext(NULL, "%l:%M""%p", LC_TIME), p);
-                checkampm(timestr);
-                PRINTF((" %s", timestr));
+                    dcgettext(NULL, "%T", LC_TIME), p);
+                PRINTF(("%-11s ", timestr));
         } else if (now - *time <= 7 * DAY) {
                 char weekdaytime[20];
 
                 (void) strftime(weekdaytime, sizeof (weekdaytime),
-                    dcgettext(NULL, "%a%l%p", LC_TIME), p);
-                checkampm(weekdaytime);
-                PRINTF((" %s", weekdaytime));
+                    dcgettext(NULL, "%a %H:%M", LC_TIME), p);
+                PRINTF(("%-11s ", weekdaytime));
         } else {
                 char monthtime[20];
 
                 (void) strftime(monthtime, sizeof (monthtime),
-                    dcgettext(NULL, "%e%b%y", LC_TIME), p);
-                PRINTF((" %s", monthtime));
+                    dcgettext(NULL, "%F", LC_TIME), p);
+                PRINTF(("%-11s ", monthtime));
         }
 }
 
 /*
  * find & return number of minutes current tty has been idle

@@ -736,17 +742,5 @@
                         }
                         *c = '?';
                 }
         }
 }
-
-/* replaces all occurences of AM/PM with am/pm */
-static void
-checkampm(char *str)
-{
-        char *ampm;
-        while ((ampm = strstr(str, "AM")) != NULL ||
-            (ampm = strstr(str, "PM")) != NULL) {
-                *ampm = tolower(*ampm);
-                *(ampm+1) = tolower(*(ampm+1));
-        }
-}