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 */

@@ -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,70 @@
 #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 a time in days, hours, minutes, or seconds.
+ * The second argument is the 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[12];
+        char *unit;
+
+        if (tim >= 36 * HR) {
+                (void) snprintf(value, sizeof (value), "%d",
+                    (tim + (DAY / 2)) / (DAY));
+                unit = dcgettext(NULL, "days", LC_TIME);
+        } else if (tim >= 36 * 60) {
+                (void) snprintf(value, sizeof (value), "%d",
+                    (tim + (HR / 2)) / (HR));
+                unit = dcgettext(NULL, "hours", LC_TIME);
+        } else if (tim >= 60) {
+                (void) snprintf(value, sizeof (value), "%d",
+                    (tim + 30) / 60);
+                unit = dcgettext(NULL, "mins", LC_TIME);
         } else if (tim > 0) {
-                PRINTF((dcgettext(NULL, "    %2d", LC_TIME), (int)tim));
+                (void) snprintf(value, sizeof (value), "%d", (int)tim);
+                unit = dcgettext(NULL, "secs", LC_TIME);
         } else {
-                PRINTF(("      "));
+                (void) strcpy(value, "0");
+                unit = " ";
         }
-        PRINTF(("%s", tail));
+        width -= 2 + strlen(value);
+        width = (width > 1) ? width : 1;
+        PRINTF(("%s %-*s ", value, width, unit));
 }
 
 /*
- * prints a 12 hour time given a pointer to a time of day
+ * prints a locale-specific time given a pointer to a time of day
  */
 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));
+                    "%X", 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));
+                    "%d %b", p);
+                PRINTF(("%-11s ", weekdaytime));
         } else {
                 char monthtime[20];
 
                 (void) strftime(monthtime, sizeof (monthtime),
-                    dcgettext(NULL, "%e%b%y", LC_TIME), p);
-                PRINTF((" %s", monthtime));
+                    "%b %Y", p);
+                PRINTF(("%-11s ", monthtime));
         }
 }
 
 /*
  * find & return number of minutes current tty has been idle

@@ -736,17 +748,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));
-        }
-}