Print this page
2989 Eliminate use of LOGNAME_MAX in ON
1166 useradd have warning with name more 8 chars

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/who/who.c
          +++ new/usr/src/cmd/who/who.c
↓ open down ↓ 15 lines elided ↑ open up ↑
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
  22   22  /*        All Rights Reserved   */
  23   23  
  24   24  
  25   25  /*
       26 + * Copyright (c) 2013 Gary Mills
       27 + *
  26   28   * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  27   29   * Use is subject to license terms.
  28   30   */
  29   31  
  30      -#pragma ident   "%Z%%M% %I%     %E% SMI"
  31      -
  32   32  /*
  33   33   *      This program analyzes information found in /var/adm/utmpx
  34   34   *
  35   35   *      Additionally information is gathered from /etc/inittab
  36   36   *      if requested.
  37   37   *
  38   38   *
  39   39   *      Syntax:
  40   40   *
  41   41   *              who am i        Displays info on yourself
↓ open down ↓ 51 lines elided ↑ open up ↑
  93   93  #include        <pwd.h>
  94   94  #include        <limits.h>
  95   95  
  96   96  static void process(void);
  97   97  static void ck_file(char *);
  98   98  static void dump(void);
  99   99  
 100  100  static struct   utmpx *utmpp;   /* pointer for getutxent()      */
 101  101  
 102  102  /*
 103      - * utmpx defines wider fields for user and line.  For compatibility of output,
 104      - * we are limiting these to the old maximums in utmp. Define UTMPX_NAMELEN
 105      - * to use the full lengths.
      103 + * Use the full lengths from utmpx for user and line.
 106  104   */
 107      -#ifndef UTMPX_NAMELEN
 108      -/* XXX - utmp - fix name length */
 109      -#define NMAX    (_POSIX_LOGIN_NAME_MAX - 1)
 110      -#define LMAX    12
 111      -#else /* UTMPX_NAMELEN */
 112  105  #define NMAX    (sizeof (utmpp->ut_user))
 113  106  #define LMAX    (sizeof (utmpp->ut_line))
 114      -#endif
 115  107  
      108 +/* Print minimum field widths. */
      109 +#define LOGIN_WIDTH     8
      110 +#define LINE_WIDTH      12
      111 +
 116  112  static char     comment[BUFSIZ]; /* holds inittab comment       */
 117  113  static char     errmsg[BUFSIZ]; /* used in snprintf for errors  */
 118  114  static int      fildes;         /* file descriptor for inittab  */
 119  115  static int      Hopt = 0;       /* 1 = who -H                   */
 120  116  static char     *inittab;       /* ptr to inittab contents      */
 121  117  static char     *iinit;         /* index into inittab           */
 122  118  static int      justme = 0;     /* 1 = who am i                 */
 123  119  static struct   tm *lptr;       /* holds user login time        */
 124  120  static char     *myname;        /* pointer to invoker's name    */
 125  121  static char     *mytty;         /* holds device user is on      */
↓ open down ↓ 35 lines elided ↑ open up ↑
 161  157  #endif
 162  158          (void) textdomain(TEXT_DOMAIN);
 163  159  
 164  160          validtype[USER_PROCESS] = 1;
 165  161          validtype[EMPTY] = 0;
 166  162          stbufp = &stbuf;
 167  163  
 168  164          /*
 169  165           *      Strip off path name of this command
 170  166           */
 171      -        for (i = strlen(argv[0]); i >= 0 && argv[0][i] != '/'; --i);
      167 +        for (i = strlen(argv[0]); i >= 0 && argv[0][i] != '/'; --i)
      168 +                ;
 172  169          if (i >= 0)
 173  170                  argv[0] += i+1;
 174  171          program = argv[0];
 175  172  
 176  173          /*
 177  174           *      Buffer stdout for speed
 178  175           */
 179  176          setbuf(stdout, outbuf);
 180  177  
 181  178          /*
↓ open down ↓ 54 lines elided ↑ open up ↑
 236  233                          number = strtol(optarg, &end, 10);
 237  234                          if (errno != 0 || *end != '\0') {
 238  235                                  (void) fprintf(stderr, gettext(
 239  236                                      "%s: Invalid numeric argument\n"),
 240  237                                      program);
 241  238                                  exit(1);
 242  239                          }
 243  240                          if (number < 1) {
 244  241                                  (void) fprintf(stderr, gettext(
 245  242                                      "%s: Number of users per line must "
 246      -                                        "be at least 1\n"), program);
      243 +                                    "be at least 1\n"), program);
 247  244                                  exit(1);
 248  245                          }
 249  246                          break;
 250  247  
 251  248                  case 'p':
 252  249                          validtype[INIT_PROCESS] = 1;
 253  250                          if (!uopt) validtype[USER_PROCESS] = 0;
 254  251                          break;
 255  252  
 256  253                  case 'q':
↓ open down ↓ 84 lines elided ↑ open up ↑
 341  338                  (void) fprintf(stderr,
 342  339                      gettext("p\tprocesses other than getty or users\n"));
 343  340                  (void) fprintf(stderr, gettext("q\tquick %s\n"), program);
 344  341                  (void) fprintf(stderr, gettext("r\trun level\n"));
 345  342                  (void) fprintf(stderr, gettext(
 346  343                  "s\tshort form of %s (no time since last output or pid)\n"),
 347  344                      program);
 348  345                  (void) fprintf(stderr, gettext("t\ttime changes\n"));
 349  346                  (void) fprintf(stderr, gettext(
 350  347                      "T\tstatus of tty (+ writable, - not writable, "
 351      -                        "? hung)\n"));
      348 +                    "? hung)\n"));
 352  349                  (void) fprintf(stderr, gettext("u\tuseful information\n"));
 353  350                  (void) fprintf(stderr,
 354  351                      gettext("m\tinformation only about current terminal\n"));
 355  352                  (void) fprintf(stderr, gettext(
 356  353                      "am i\tinformation about current terminal "
 357      -                        "(same as -m)\n"));
      354 +                    "(same as -m)\n"));
 358  355                  (void) fprintf(stderr, gettext(
 359  356                      "am I\tinformation about current terminal "
 360      -                        "(same as -m)\n"));
      357 +                    "(same as -m)\n"));
 361  358                  exit(1);
 362  359          }
 363  360  
 364  361          /*
 365  362           * XCU4: If -q option ignore all other options
 366  363           */
 367  364          if (qopt == 1) {
 368  365                  Hopt = 0;
 369  366                  sopt = 0;
 370  367                  Topt = 0;
↓ open down ↓ 15 lines elided ↑ open up ↑
 386  383                  ck_file(argv[optind]);
 387  384                  (void) utmpxname(argv[optind]);
 388  385          }
 389  386  
 390  387          /*
 391  388           *      Test for 'who am i' or 'who am I'
 392  389           *      XCU4 - check if justme was already set by -m option
 393  390           */
 394  391          if (justme == 1 || (argc == 3 && strcmp(argv[1], "am") == 0 &&
 395  392              ((argv[2][0] == 'i' || argv[2][0] == 'I') &&
 396      -                argv[2][1] == '\0'))) {
      393 +            argv[2][1] == '\0'))) {
 397  394                  justme = 1;
 398  395                  myname = nameval;
 399  396                  (void) cuserid(myname);
 400  397                  if ((mytty = ttyname(fileno(stdin))) == NULL &&
 401  398                      (mytty = ttyname(fileno(stdout))) == NULL &&
 402  399                      (mytty = ttyname(fileno(stderr))) == NULL) {
 403  400                          (void) fprintf(stderr, gettext(
 404  401                          "Must be attached to terminal for 'am I' option\n"));
 405  402                          (void) fflush(stderr);
 406  403                          exit(1);
↓ open down ↓ 95 lines elided ↑ open up ↑
 502  499  
 503  500          /*
 504  501           * Do print in 'who -q' format
 505  502           */
 506  503          if (qopt) {
 507  504                  /*
 508  505                   * XCU4 - Use non user macro for correct user count
 509  506                   */
 510  507                  if (((totlusrs - 1) % number) == 0 && totlusrs > 1)
 511  508                          (void) printf("\n");
 512      -                (void) printf("%-*s ", NMAX, user);
      509 +                (void) printf("%-*.*s ", LOGIN_WIDTH, NMAX, user);
 513  510                  return;
 514  511          }
 515  512  
 516  513  
 517  514          pexit = (int)' ';
 518  515          pterm = (int)' ';
 519  516  
 520  517          /*
 521  518           *      Get exit info if applicable
 522  519           */
↓ open down ↓ 35 lines elided ↑ open up ↑
 558  555                  else if ((stbufp->st_mode & S_IWOTH) ||
 559  556                      (stbufp->st_mode & S_IWGRP))  /* Check group & other */
 560  557                          w = '+';
 561  558  
 562  559          } else
 563  560                  w = ' ';
 564  561  
 565  562          /*
 566  563           *      Print the TERSE portion of the output
 567  564           */
 568      -        (void) printf("%-*s %c %-12s %s", NMAX, user, w, device, time_buf);
      565 +        (void) printf("%-*.*s %c %-12s %s", LOGIN_WIDTH, NMAX, user,
      566 +            w, device, time_buf);
 569  567  
 570  568          if (!terse) {
 571  569                  /*
 572  570                   *      Stat device for idle time
 573  571                   *      (Don't complain if you can't)
 574  572                   */
 575  573                  rc = -1;
 576  574                  if (utmpp->ut_type == USER_PROCESS) {
 577  575                          (void) strcpy(path, "/dev/");
 578  576                          (void) strncpy(path + 5, utmpp->ut_line,
↓ open down ↓ 40 lines elided ↑ open up ↑
 619  617                           *      minimize the work. Wrap once if needed
 620  618                           *      for each entry.
 621  619                           */
 622  620                          wrap = 0;
 623  621                          /*
 624  622                           *      Look for a line beginning with
 625  623                           *      utmpp->ut_id
 626  624                           */
 627  625                          while ((rc = strncmp(utmpp->ut_id, iinit,
 628  626                              strcspn(iinit, ":"))) != 0) {
 629      -                                for (; *iinit != '\n'; iinit++);
      627 +                                for (; *iinit != '\n'; iinit++)
      628 +                                        ;
 630  629                                  iinit++;
 631  630  
 632  631                                  /*
 633  632                                   *      Wrap once if necessary to
 634  633                                   *      find entry in inittab
 635  634                                   */
 636  635                                  if (*iinit == '\0') {
 637  636                                          if (!wrap) {
 638  637                                                  iinit = inittab;
 639  638                                                  wrap = 1;
 640  639                                          }
 641  640                                  }
 642  641                          }
 643  642  
 644  643                          if (*iinit != '\0') {
 645  644                                  /*
 646  645                                   *      We found our entry
 647  646                                   */
 648  647                                  for (iinit++; *iinit != '#' &&
 649      -                                         *iinit != '\n'; iinit++);
      648 +                                    *iinit != '\n'; iinit++)
      649 +                                        ;
 650  650                                  if (*iinit == '#') {
 651  651                                          for (iinit++; *iinit == ' ' ||
 652      -                                                 *iinit == '\t'; iinit++);
      652 +                                            *iinit == '\t'; iinit++)
      653 +                                                ;
 653  654                                          for (rc = 0; *iinit != '\n'; iinit++)
 654  655                                                  comment[rc++] = *iinit;
 655  656                                          comment[rc] = '\0';
 656  657                                  } else
 657  658                                          (void) strcpy(comment, " ");
 658  659  
 659  660                                  (void) printf("  %s", comment);
 660  661                          } else
 661  662                                  iinit = inittab;        /* Reset pointer */
 662  663                  }
↓ open down ↓ 63 lines elided ↑ open up ↑
 726  727              utmpp->ut_user, utmpp->ut_id, utmpp->ut_line, utmpp->ut_type);
 727  728  #endif
 728  729                  if (utmpp->ut_type <= UTMAXTYPE) {
 729  730                          /*
 730  731                           *      Handle "am i"
 731  732                           */
 732  733                          if (justme) {
 733  734                                  if (strncmp(myname, utmpp->ut_user,
 734  735                                      sizeof (utmpp->ut_user)) == 0 &&
 735  736                                      strncmp(mytty, utmpp->ut_line,
 736      -                                        sizeof (utmpp->ut_line)) == 0 &&
      737 +                                    sizeof (utmpp->ut_line)) == 0 &&
 737  738                                      utmpp->ut_type == USER_PROCESS) {
 738  739                                          /*
 739  740                                           * we have have found ourselves
 740  741                                           * in the utmp file and the entry
 741  742                                           * is a user process, this is not
 742  743                                           * meaningful otherwise
 743  744                                           *
 744  745                                           */
 745  746  
 746  747                                          dump();
↓ open down ↓ 2 lines elided ↑ open up ↑
 749  750                                  continue;
 750  751                          }
 751  752  
 752  753                          /*
 753  754                           *      Print the line if we want it
 754  755                           */
 755  756                          if (validtype[utmpp->ut_type]) {
 756  757  #ifdef  XPG4
 757  758                                  if (utmpp->ut_type == LOGIN_PROCESS) {
 758  759                                          if ((utmpp->ut_line[0] == '\0') ||
 759      -                                        (strcmp(utmpp->ut_user, "LOGIN") != 0))
      760 +                                            (strcmp(utmpp->ut_user,
      761 +                                            "LOGIN") != 0))
 760  762                                                  continue;
 761  763                                  }
 762  764  #endif  /* XPG4 */
 763  765                                  dump();
 764  766                          }
 765  767                  } else {
 766  768                          (void) fprintf(stderr,
 767  769                              gettext("%s: Error --- entry has ut_type "
 768      -                                "of %d\n"), program, utmpp->ut_type);
      770 +                            "of %d\n"), program, utmpp->ut_type);
 769  771                          (void) fprintf(stderr,
 770  772                              gettext(" when maximum is %d\n"), UTMAXTYPE);
 771  773                  }
 772  774          }
 773  775  
 774  776          /*
 775  777           * If justme is set at this point than the utmp entry
 776  778           * was not found.
 777  779           */
 778  780          if (justme) {
↓ open down ↓ 89 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX