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

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/w/w.c
          +++ new/usr/src/cmd/w/w.c
↓ open down ↓ 71 lines elided ↑ open up ↑
  72   72  
  73   73  /*
  74   74   * Use the full lengths from utmpx for user and line.
  75   75   */
  76   76  static struct utmpx dummy;
  77   77  #define NMAX            (sizeof (dummy.ut_user))
  78   78  #define LMAX            (sizeof (dummy.ut_line))
  79   79  
  80   80  /* Print minimum field widths. */
  81   81  #define LOGIN_WIDTH     8
  82      -#define LINE_WIDTH      12
       82 +#define LINE_WIDTH      8
  83   83  
  84   84  #define DIV60(t)        ((t+30)/60)     /* x/60 rounded */
  85   85  
  86   86  #ifdef ERR
  87   87  #undef ERR
  88   88  #endif
  89   89  #define ERR             (-1)
  90   90  
  91   91  #define HSIZE           256             /* size of process hash table   */
  92   92  #define PROCDIR         "/proc"
↓ open down ↓ 27 lines elided ↑ open up ↑
 120  120   *      and the size of the hash table(HSIZE)
 121  121   *      to determine process index into the table.
 122  122   */
 123  123  static struct uproc     pr_htbl[HSIZE];
 124  124  
 125  125  static struct   uproc   *findhash(pid_t);
 126  126  static time_t   findidle(char *);
 127  127  static void     clnarglist(char *);
 128  128  static void     showtotals(struct uproc *);
 129  129  static void     calctotals(struct uproc *);
 130      -static void     prttime(time_t, char *);
      130 +static void     prttime(time_t, int);
 131  131  static void     prtat(time_t *time);
 132      -static void     checkampm(char *str);
 133  132  
 134  133  static char     *prog;          /* pointer to invocation name */
 135  134  static int      header = 1;     /* true if -h flag: don't print heading */
 136  135  static int      lflag = 1;      /* set if -l flag; 0 for -s flag: short form */
 137  136  static char     *sel_user;      /* login of particular user selected */
 138  137  static char     firstchar;      /* first char of name of prog invoked as */
 139  138  static int      login;          /* true if invoked as login shell */
 140  139  static time_t   now;            /* current time of day */
 141  140  static time_t   uptime;         /* time of last reboot & elapsed time since */
 142  141  static int      nusers;         /* number of users logged in now */
↓ open down ↓ 158 lines elided ↑ open up ↑
 301  300                   */
 302  301                  (void) getloadavg(loadavg, 3);
 303  302                  PRINTF((gettext(",  load average: %.2f, %.2f, %.2f\n"),
 304  303                      loadavg[LOADAVG_1MIN], loadavg[LOADAVG_5MIN],
 305  304                      loadavg[LOADAVG_15MIN]));
 306  305  
 307  306                  if (firstchar == 'u')   /* uptime command */
 308  307                          exit(0);
 309  308  
 310  309                  if (lflag) {
 311      -                        PRINTF((dcgettext(NULL, "User     tty           "
 312      -                            "login@  idle   JCPU   PCPU  what\n", LC_TIME)));
      310 +                        PRINTF((dcgettext(NULL, "User     tty      "
      311 +                            "login@      idle    JCPU    PCPU    what\n",
      312 +                            LC_TIME)));
 313  313                  } else {
 314  314                          PRINTF((dcgettext(NULL,
 315      -                            "User     tty           idle   what\n", LC_TIME)));
      315 +                            "User     tty      idle    what\n",
      316 +                            LC_TIME)));
 316  317                  }
 317  318  
 318  319                  if (fflush(stdout) == EOF) {
 319  320                          perror((gettext("%s: fflush failed\n"), prog));
 320  321                          exit(1);
 321  322                  }
 322  323          }
 323  324  
 324  325          /*
 325  326           * loop through /proc, reading info about each process
↓ open down ↓ 152 lines elided ↑ open up ↑
 478  479                  if (ut->ut_type != USER_PROCESS)
 479  480                          continue;
 480  481                  if (sel_user && strncmp(ut->ut_name, sel_user, NMAX) != 0)
 481  482                          continue;       /* we're looking for somebody else */
 482  483  
 483  484                  /* print login name of the user */
 484  485                  PRINTF(("%-*.*s ", LOGIN_WIDTH, NMAX, ut->ut_name));
 485  486  
 486  487                  /* print tty user is on */
 487  488                  if (lflag) {
 488      -                        PRINTF(("%-*.*s", LINE_WIDTH, LMAX, ut->ut_line));
      489 +                        PRINTF(("%-*.*s ", LINE_WIDTH, LMAX, ut->ut_line));
 489  490                  } else {
 490  491                          if (ut->ut_line[0] == 'p' && ut->ut_line[1] == 't' &&
 491  492                              ut->ut_line[2] == 's' && ut->ut_line[3] == '/') {
 492      -                                PRINTF(("%-*.3s", LMAX, &ut->ut_line[4]));
      493 +                                PRINTF(("%-*.*s ", LINE_WIDTH, LMAX,
      494 +                                    &ut->ut_line[4]));
 493  495                          } else {
 494      -                                PRINTF(("%-*.*s", LINE_WIDTH, LMAX,
      496 +                                PRINTF(("%-*.*s ", LINE_WIDTH, LMAX,
 495  497                                      ut->ut_line));
 496  498                          }
 497  499                  }
 498  500  
 499  501                  /* print when the user logged in */
 500  502                  if (lflag) {
 501  503                          time_t tim = ut->ut_xtime;
 502  504                          prtat(&tim);
 503  505                  }
 504  506  
 505  507                  /* print idle time */
 506  508                  idle = findidle(ut->ut_line);
 507      -                if (idle >= 36 * 60) {
 508      -                        PRINTF((dcgettext(NULL, "%2ddays ", LC_TIME),
 509      -                            (idle + 12 * 60) / (24 * 60)));
 510      -                } else
 511      -                        prttime(idle, " ");
      509 +                prttime(idle, 8);
 512  510                  showtotals(findhash(ut->ut_pid));
 513  511          }
 514  512          if (fclose(stdout) == EOF) {
 515  513                  perror((gettext("%s: fclose failed"), prog));
 516  514                  exit(1);
 517  515          }
 518  516          return (0);
 519  517  }
 520  518  
 521  519  /*
↓ open down ↓ 8 lines elided ↑ open up ↑
 530  528          proctime = 0;
 531  529          empty = 1;
 532  530          curpid = -1;
 533  531          add_times = 1;
 534  532  
 535  533          calctotals(up);
 536  534  
 537  535          if (lflag) {
 538  536                  /* print CPU time for all processes & children */
 539  537                  /* and need to convert clock ticks to seconds first */
 540      -                prttime((time_t)jobtime, " ");
      538 +                prttime((time_t)jobtime, 8);
 541  539  
 542  540                  /* print cpu time for interesting process */
 543  541                  /* and need to convert clock ticks to seconds first */
 544      -                prttime((time_t)proctime, " ");
      542 +                prttime((time_t)proctime, 8);
 545  543          }
 546  544          /* what user is doing, current process */
 547      -        PRINTF((" %-.32s\n", doing));
      545 +        PRINTF(("%-.32s\n", doing));
 548  546  }
 549  547  
 550  548  /*
 551  549   *  This recursive routine descends the process
 552  550   *  tree starting from the given process pointer(up).
 553  551   *  It used depth-first search strategy and also marked
 554  552   *  each node as visited as it traversed down the tree.
 555  553   *  It calculates the process time for all processes &
 556  554   *  children.  It also finds the interesting process
 557  555   *  and determines its cpu time and command.
↓ open down ↓ 81 lines elided ↑ open up ↑
 639  637          tp->p_link = up->p_link;                /* insert after head */
 640  638          up->p_link = tp;
 641  639          return (tp);
 642  640  }
 643  641  
 644  642  #define HR      (60 * 60)
 645  643  #define DAY     (24 * HR)
 646  644  #define MON     (30 * DAY)
 647  645  
 648  646  /*
 649      - * prttime prints a time in hours and minutes or minutes and seconds.
 650      - * The character string tail is printed at the end, obvious
 651      - * strings to pass are "", " ", or "am".
      647 + * prttime prints a time in days, hours, minutes, or seconds.
      648 + * The second argument is the field width.
 652  649   */
 653  650  static void
 654      -prttime(time_t tim, char *tail)
      651 +prttime(time_t tim, int width)
 655  652  {
 656      -        if (tim >= 60) {
 657      -                PRINTF((dcgettext(NULL, "%3d:%02d", LC_TIME),
 658      -                    (int)tim/60, (int)tim%60));
      653 +        char value[12];
      654 +        char *unit;
      655 +
      656 +        if (tim >= 36 * HR) {
      657 +                (void) snprintf(value, sizeof (value), "%d",
      658 +                    (tim + (DAY / 2)) / (DAY));
      659 +                unit = dcgettext(NULL, "days", LC_TIME);
      660 +        } else if (tim >= 36 * 60) {
      661 +                (void) snprintf(value, sizeof (value), "%d",
      662 +                    (tim + (HR / 2)) / (HR));
      663 +                unit = dcgettext(NULL, "hours", LC_TIME);
      664 +        } else if (tim >= 60) {
      665 +                (void) snprintf(value, sizeof (value), "%d",
      666 +                    (tim + 30) / 60);
      667 +                unit = dcgettext(NULL, "mins", LC_TIME);
 659  668          } else if (tim > 0) {
 660      -                PRINTF((dcgettext(NULL, "    %2d", LC_TIME), (int)tim));
      669 +                (void) snprintf(value, sizeof (value), "%d", (int)tim);
      670 +                unit = dcgettext(NULL, "secs", LC_TIME);
 661  671          } else {
 662      -                PRINTF(("      "));
      672 +                (void) strcpy(value, "0");
      673 +                unit = " ";
 663  674          }
 664      -        PRINTF(("%s", tail));
      675 +        width -= 2 + strlen(value);
      676 +        width = (width > 1) ? width : 1;
      677 +        PRINTF(("%s %-*s ", value, width, unit));
 665  678  }
 666  679  
 667  680  /*
 668      - * prints a 12 hour time given a pointer to a time of day
      681 + * prints a locale-specific time given a pointer to a time of day
 669  682   */
 670  683  static void
 671  684  prtat(time_t *time)
 672  685  {
 673  686          struct tm       *p;
 674  687  
 675  688          p = localtime(time);
 676  689          if (now - *time <= 18 * HR) {
 677  690                  char timestr[50];
      691 +
 678  692                  (void) strftime(timestr, sizeof (timestr),
 679      -                    dcgettext(NULL, "%l:%M""%p", LC_TIME), p);
 680      -                checkampm(timestr);
 681      -                PRINTF((" %s", timestr));
      693 +                    "%X", p);
      694 +                PRINTF(("%-11s ", timestr));
 682  695          } else if (now - *time <= 7 * DAY) {
 683  696                  char weekdaytime[20];
 684  697  
 685  698                  (void) strftime(weekdaytime, sizeof (weekdaytime),
 686      -                    dcgettext(NULL, "%a%l%p", LC_TIME), p);
 687      -                checkampm(weekdaytime);
 688      -                PRINTF((" %s", weekdaytime));
      699 +                    "%d %b", p);
      700 +                PRINTF(("%-11s ", weekdaytime));
 689  701          } else {
 690  702                  char monthtime[20];
 691  703  
 692  704                  (void) strftime(monthtime, sizeof (monthtime),
 693      -                    dcgettext(NULL, "%e%b%y", LC_TIME), p);
 694      -                PRINTF((" %s", monthtime));
      705 +                    "%b %Y", p);
      706 +                PRINTF(("%-11s ", monthtime));
 695  707          }
 696  708  }
 697  709  
 698  710  /*
 699  711   * find & return number of minutes current tty has been idle
 700  712   */
 701  713  static time_t
 702  714  findidle(char *devname)
 703  715  {
 704  716          struct stat stbuf;
↓ open down ↓ 26 lines elided ↑ open up ↑
 731  743          for (c = arglist; *c != NULL; c++) {
 732  744                  if ((*c < ' ') || (*c > 0176)) {
 733  745                          if (err++ > 5) {
 734  746                                  *arglist = NULL;
 735  747                                  break;
 736  748                          }
 737  749                          *c = '?';
 738  750                  }
 739  751          }
 740  752  }
 741      -
 742      -/* replaces all occurences of AM/PM with am/pm */
 743      -static void
 744      -checkampm(char *str)
 745      -{
 746      -        char *ampm;
 747      -        while ((ampm = strstr(str, "AM")) != NULL ||
 748      -            (ampm = strstr(str, "PM")) != NULL) {
 749      -                *ampm = tolower(*ampm);
 750      -                *(ampm+1) = tolower(*(ampm+1));
 751      -        }
 752      -}
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX