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

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/whodo/whodo.c
          +++ new/usr/src/cmd/whodo/whodo.c
↓ open down ↓ 69 lines elided ↑ open up ↑
  70   70  #include <priv_utils.h>
  71   71  
  72   72  /*
  73   73   * Use the full lengths from utmpx for user and line.
  74   74   */
  75   75  #define NMAX    (sizeof (((struct utmpx *)0)->ut_user))
  76   76  #define LMAX    (sizeof (((struct utmpx *)0)->ut_line))
  77   77  
  78   78  /* Print minimum field widths. */
  79   79  #define LOGIN_WIDTH     8
  80      -#define LINE_WIDTH      12
       80 +#define LINE_WIDTH      8
  81   81  
  82   82  #define DIV60(t)        ((t+30)/60)    /* x/60 rounded */
  83   83  
  84   84  #ifdef ERR
  85   85  #undef ERR
  86   86  #endif
  87   87  #define ERR             (-1)
  88   88  
  89   89  #define DEVNAMELEN      14
  90   90  #define HSIZE           256             /* size of process hash table */
↓ open down ↓ 35 lines elided ↑ open up ↑
 126  126   */
 127  127  static struct uproc     pr_htbl[HSIZE];
 128  128  
 129  129  static struct   uproc   *findhash(pid_t);
 130  130  static time_t   findidle(char *);
 131  131  static void     clnarglist(char *);
 132  132  static void     showproc(struct uproc *);
 133  133  static void     showtotals(struct uproc *);
 134  134  static void     calctotals(struct uproc *);
 135  135  static char     *getty(dev_t);
 136      -static void     prttime(time_t, char *);
      136 +static void     prttime(time_t, int);
 137  137  static void     prtat(time_t *);
 138      -static void     checkampm(char *);
 139  138  
 140  139  static char     *prog;
 141  140  static int      header = 1;     /* true if -h flag: don't print heading */
 142  141  static int      lflag = 0;      /* true if -l flag: w command format */
 143  142  static char     *sel_user;      /* login of particular user selected */
 144  143  static time_t   now;            /* current time of day */
 145  144  static time_t   uptime;         /* time of last reboot & elapsed time since */
 146  145  static int      nusers;         /* number of users logged in now */
 147  146  static time_t   idle;           /* number of minutes user is idle */
 148  147  static time_t   jobtime;        /* total cpu time visible */
↓ open down ↓ 117 lines elided ↑ open up ↑
 266  265                                  } else if (ut->ut_type == BOOT_TIME) {
 267  266                                          uptime = now - ut->ut_xtime;
 268  267                                          uptime += 30;
 269  268                                          days = uptime / (60*60*24);
 270  269                                          uptime %= (60*60*24);
 271  270                                          hrs = uptime / (60*60);
 272  271                                          uptime %= (60*60);
 273  272                                          mins = uptime / 60;
 274  273  
 275  274                                          (void) printf(dcgettext(NULL,
 276      -                                            "  up %d day(s), %d hr(s), "
      275 +                                            "up %d day(s), %d hr(s), "
 277  276                                              "%d min(s)", LC_TIME),
 278  277                                              days, hrs, mins);
 279  278                                  }
 280  279                          }
 281  280  
 282  281                          ut = utmpbegin; /* rewind utmp data */
 283  282                          (void) printf(dcgettext(NULL,
 284  283                              "  %d user(s)\n", LC_TIME), nusers);
 285      -                        (void) printf(dcgettext(NULL, "User     tty           "
 286      -                            "login@  idle   JCPU   PCPU  what\n", LC_TIME));
      284 +                        (void) printf(dcgettext(NULL, "User     tty      "
      285 +                            "login@         idle    JCPU    PCPU what\n",
      286 +                            LC_TIME));
 287  287                  } else {        /* standard whodo header */
 288  288                          char date_buf[100];
 289  289  
 290  290                          /*
 291  291                           * print current time and date
 292  292                           */
 293  293                          (void) strftime(date_buf, sizeof (date_buf),
 294      -                            dcgettext(NULL, "%C", LC_TIME), localtime(&now));
      294 +                            "%c", localtime(&now));
 295  295                          (void) printf("%s\n", date_buf);
 296  296  
 297  297                          /*
 298  298                           * print system name
 299  299                           */
 300  300                          (void) uname(&uts);
 301  301                          (void) printf("%s\n", uts.nodename);
 302  302                  }
 303  303          }
 304  304  
↓ open down ↓ 162 lines elided ↑ open up ↑
 467  467                  if (ut->ut_type != USER_PROCESS)
 468  468                          continue;
 469  469                  if (sel_user && strncmp(ut->ut_name, sel_user, NMAX) != 0)
 470  470                          continue;       /* we're looking for somebody else */
 471  471                  if (lflag) {    /* -l flag format (w command) */
 472  472                          /* print login name of the user */
 473  473                          (void) printf("%-*.*s ", LOGIN_WIDTH, (int)NMAX,
 474  474                              ut->ut_name);
 475  475  
 476  476                          /* print tty user is on */
 477      -                        (void) printf("%-*.*s", LINE_WIDTH, (int)LMAX,
      477 +                        (void) printf("%-*.*s ", LINE_WIDTH, (int)LMAX,
 478  478                              ut->ut_line);
 479  479  
 480  480                          /* print when the user logged in */
 481  481                          tim = ut->ut_xtime;
 482  482                          (void) prtat(&tim);
 483  483  
 484  484                          /* print idle time */
 485  485                          idle = findidle(ut->ut_line);
 486      -                        if (idle >= 36 * 60)
 487      -                                (void) printf(dcgettext(NULL, "%2ddays ",
 488      -                                    LC_TIME), (idle + 12 * 60) / (24 * 60));
 489      -                        else
 490      -                                prttime(idle, " ");
      486 +                        prttime(idle, 8);
 491  487                          showtotals(findhash((pid_t)ut->ut_pid));
 492  488                  } else {        /* standard whodo format */
 493  489                          tim = ut->ut_xtime;
 494  490                          tm = localtime(&tim);
 495  491                          (void) printf("\n%-*.*s %-*.*s %2.1d:%2.2d\n",
 496  492                              LINE_WIDTH, (int)LMAX, ut->ut_line,
 497  493                              LOGIN_WIDTH, (int)NMAX, ut->ut_name, tm->tm_hour,
 498  494                              tm->tm_min);
 499  495                          showproc(findhash((pid_t)ut->ut_pid));
 500  496                  }
↓ open down ↓ 54 lines elided ↑ open up ↑
 555  551  {
 556  552          jobtime = 0;
 557  553          proctime = 0;
 558  554          empty = 1;
 559  555          curpid = -1;
 560  556          (void) strcpy(doing, "-"); /* default act: normally never prints */
 561  557          calctotals(up);
 562  558  
 563  559          /* print CPU time for all processes & children */
 564  560          /* and need to convert clock ticks to seconds first */
 565      -        prttime((time_t)jobtime, " ");
      561 +        prttime((time_t)jobtime, 8);
 566  562  
 567  563          /* print cpu time for interesting process */
 568  564          /* and need to convert clock ticks to seconds first */
 569      -        prttime((time_t)proctime, " ");
      565 +        prttime((time_t)proctime, 8);
 570  566  
 571  567          /* what user is doing, current process */
 572      -        (void) printf(" %-.32s\n", doing);
      568 +        (void) printf("%-.32s\n", doing);
 573  569  }
 574  570  
 575  571  /*
 576  572   *  Used for -l flag (w command) format.
 577  573   *  This recursive routine descends the process
 578  574   *  tree starting from the given process pointer(up).
 579  575   *  It used depth-first search strategy and also marked
 580  576   *  each node as visited as it traversed down the tree.
 581  577   *  It calculates the process time for all processes &
 582  578   *  children.  It also finds the "interesting" process
↓ open down ↓ 146 lines elided ↑ open up ↑
 729  725          tp->p_state = NONE;
 730  726          tp->p_child = tp->p_sibling = tp->p_pgrplink = (pid_t)0;
 731  727          tp->p_link = up->p_link;                /* insert after head */
 732  728          up->p_link = tp;
 733  729          return (tp);
 734  730  }
 735  731  
 736  732  #define HR      (60 * 60)
 737  733  #define DAY     (24 * HR)
 738  734  #define MON     (30 * DAY)
      735 +#define PRINTF(a)       (void) printf a
 739  736  
 740  737  /*
 741      - * prints a time in hours and minutes or minutes and seconds.
 742      - * The character string 'tail' is printed at the end, obvious
 743      - * strings to pass are "", " ", or "am".
      738 + * Prttime prints an elapsed time in hours, minutes, or seconds,
      739 + * right-justified with the rightmost column always blank.
      740 + * The second argument is the minimum field width.
 744  741   */
 745  742  static void
 746      -prttime(time_t tim, char *tail)
      743 +prttime(time_t tim, int width)
 747  744  {
 748      -        if (tim >= 60)
 749      -                (void) printf(dcgettext(NULL, "%3d:%02d", LC_TIME),
 750      -                    (int)tim/60, (int)tim%60);
 751      -        else if (tim > 0)
 752      -                (void) printf(dcgettext(NULL, "    %2d", LC_TIME), (int)tim);
 753      -        else
 754      -                (void) printf("      ");
 755      -        (void) printf("%s", tail);
      745 +        char value[36];
      746 +
      747 +        if (tim >= 36 * 60) {
      748 +                (void) snprintf(value, sizeof (value), "%d:%02d:%02d",
      749 +                    (int)tim / HR, (int)(tim % HR) / 60, (int)tim % 60);
      750 +        } else if (tim >= 60) {
      751 +                (void) snprintf(value, sizeof (value), "%d:%02d",
      752 +                    (int)tim / 60, (int)tim % 60);
      753 +        } else if (tim > 0) {
      754 +                (void) snprintf(value, sizeof (value), "%d", (int)tim);
      755 +        } else {
      756 +                (void) strcpy(value, "0");
      757 +        }
      758 +        width = (width > 2) ? width - 1 : 1;
      759 +        PRINTF(("%*s ", width, value));
 756  760  }
 757  761  
 758      -
 759  762  /*
 760      - * prints a 12 hour time given a pointer to a time of day
      763 + * Prints the ISO date or time given a pointer to a time of day,
      764 + * left-justfied in a 12-character expanding field with the
      765 + * rightmost column always blank.
      766 + * Includes a dcgettext() override in case a message catalog is needed.
 761  767   */
 762  768  static void
 763  769  prtat(time_t *time)
 764  770  {
 765      -        struct tm *p;
      771 +        struct tm       *p;
 766  772  
 767  773          p = localtime(time);
 768  774          if (now - *time <= 18 * HR) {
 769  775                  char timestr[50];
      776 +
 770  777                  (void) strftime(timestr, sizeof (timestr),
 771      -                    dcgettext(NULL, " %l:%M""%p", LC_TIME), p);
 772      -                checkampm(timestr);
 773      -                (void) printf("%s", timestr);
      778 +                    dcgettext(NULL, "%T", LC_TIME), p);
      779 +                PRINTF(("%-11s ", timestr));
 774  780          } else if (now - *time <= 7 * DAY) {
 775  781                  char weekdaytime[20];
 776  782  
 777  783                  (void) strftime(weekdaytime, sizeof (weekdaytime),
 778      -                    dcgettext(NULL, "%a%l%p", LC_TIME), p);
 779      -                checkampm(weekdaytime);
 780      -                (void) printf(" %s", weekdaytime);
      784 +                    dcgettext(NULL, "%a %H:%M", LC_TIME), p);
      785 +                PRINTF(("%-11s ", weekdaytime));
 781  786          } else {
 782  787                  char monthtime[20];
 783  788  
 784  789                  (void) strftime(monthtime, sizeof (monthtime),
 785      -                    dcgettext(NULL, "%e%b%y", LC_TIME), p);
 786      -                (void) printf(" %s", monthtime);
      790 +                    dcgettext(NULL, "%F", LC_TIME), p);
      791 +                PRINTF(("%-11s ", monthtime));
 787  792          }
 788  793  }
 789  794  
 790  795  /*
 791  796   * find & return number of minutes current tty has been idle
 792  797   */
 793  798  static time_t
 794  799  findidle(char *devname)
 795  800  {
 796  801          struct stat stbuf;
↓ open down ↓ 26 lines elided ↑ open up ↑
 823  828          for (c = arglist; *c == NULL; c++) {
 824  829                  if ((*c < ' ') || (*c > 0176)) {
 825  830                          if (err++ > 5) {
 826  831                                  *arglist = NULL;
 827  832                                  break;
 828  833                          }
 829  834                          *c = '?';
 830  835                  }
 831  836          }
 832  837  }
 833      -
 834      -/* replaces all occurences of AM/PM with am/pm */
 835      -static void
 836      -checkampm(char *str)
 837      -{
 838      -        char *ampm;
 839      -        while ((ampm = strstr(str, "AM")) != NULL ||
 840      -            (ampm = strstr(str, "PM")) != NULL) {
 841      -                *ampm = tolower(*ampm);
 842      -                *(ampm+1) = tolower(*(ampm+1));
 843      -        }
 844      -}
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX