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 ↓ 126 lines elided ↑ open up ↑
 275  274                                          (void) printf(dcgettext(NULL,
 276  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 ↓ 148 lines elided ↑ open up ↑
 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)
 739  735  
 740  736  /*
 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".
      737 + * prttime prints a time in days, hours, minutes, or seconds.
      738 + * The second argument is the field width.
 744  739   */
 745  740  static void
 746      -prttime(time_t tim, char *tail)
      741 +prttime(time_t tim, int width)
 747  742  {
 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);
      743 +        char value[12];
      744 +        char *unit;
      745 +
      746 +        if (tim >= 36 * HR) {
      747 +                (void) snprintf(value, sizeof (value), "%d",
      748 +                    (tim + (DAY / 2)) / (DAY));
      749 +                unit = dcgettext(NULL, "days", LC_TIME);
      750 +        } else if (tim >= 36 * 60) {
      751 +                (void) snprintf(value, sizeof (value), "%d",
      752 +                    (tim + (HR / 2)) / (HR));
      753 +                unit = dcgettext(NULL, "hours", LC_TIME);
      754 +        } else if (tim >= 60) {
      755 +                (void) snprintf(value, sizeof (value), "%d",
      756 +                    (tim + 30) / 60);
      757 +                unit = dcgettext(NULL, "mins", LC_TIME);
      758 +        } else if (tim > 0) {
      759 +                (void) snprintf(value, sizeof (value), "%d", (int)tim);
      760 +                unit = dcgettext(NULL, "secs", LC_TIME);
      761 +        } else {
      762 +                (void) strcpy(value, "0");
      763 +                unit = " ";
      764 +        }
      765 +        width -= 2 + strlen(value);
      766 +        width = (width > 1) ? width : 1;
      767 +        printf("%s %-*s ", value, width, unit);
 756  768  }
 757  769  
 758  770  
 759  771  /*
 760      - * prints a 12 hour time given a pointer to a time of day
      772 + * prints a locale-specific time given a pointer to a time of day
 761  773   */
 762  774  static void
 763  775  prtat(time_t *time)
 764  776  {
 765      -        struct tm *p;
      777 +        struct tm       *p;
 766  778  
 767  779          p = localtime(time);
 768  780          if (now - *time <= 18 * HR) {
 769  781                  char timestr[50];
      782 +
 770  783                  (void) strftime(timestr, sizeof (timestr),
 771      -                    dcgettext(NULL, " %l:%M""%p", LC_TIME), p);
 772      -                checkampm(timestr);
 773      -                (void) printf("%s", timestr);
      784 +                    "%X", p);
      785 +                printf("%-11s ", timestr);
 774  786          } else if (now - *time <= 7 * DAY) {
 775  787                  char weekdaytime[20];
 776  788  
 777  789                  (void) strftime(weekdaytime, sizeof (weekdaytime),
 778      -                    dcgettext(NULL, "%a%l%p", LC_TIME), p);
 779      -                checkampm(weekdaytime);
 780      -                (void) printf(" %s", weekdaytime);
      790 +                    "%d %b", p);
      791 +                printf("%-11s ", weekdaytime);
 781  792          } else {
 782  793                  char monthtime[20];
 783  794  
 784  795                  (void) strftime(monthtime, sizeof (monthtime),
 785      -                    dcgettext(NULL, "%e%b%y", LC_TIME), p);
 786      -                (void) printf(" %s", monthtime);
      796 +                    "%b %Y", p);
      797 +                printf("%-11s ", monthtime);
 787  798          }
 788  799  }
 789  800  
 790  801  /*
 791  802   * find & return number of minutes current tty has been idle
 792  803   */
 793  804  static time_t
 794  805  findidle(char *devname)
 795  806  {
 796  807          struct stat stbuf;
↓ open down ↓ 26 lines elided ↑ open up ↑
 823  834          for (c = arglist; *c == NULL; c++) {
 824  835                  if ((*c < ' ') || (*c > 0176)) {
 825  836                          if (err++ > 5) {
 826  837                                  *arglist = NULL;
 827  838                                  break;
 828  839                          }
 829  840                          *c = '?';
 830  841                  }
 831  842          }
 832  843  }
 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