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/last/last.c
          +++ new/usr/src/cmd/last/last.c
↓ open down ↓ 12 lines elided ↑ open up ↑
  13   13   *
  14   14   * When distributing Covered Code, include this CDDL HEADER in each
  15   15   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16   16   * If applicable, add the following below this CDDL HEADER, with the
  17   17   * fields enclosed by brackets "[]" replaced with your own identifying
  18   18   * information: Portions Copyright [yyyy] [name of copyright owner]
  19   19   *
  20   20   * CDDL HEADER END
  21   21   */
  22   22  /*
       23 + * Copyright (c) 2013 Gary Mills
       24 + *
  23   25   * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
  24   26   * Use is subject to license terms.
  25   27   */
  26   28  
  27   29  /*
  28   30   *      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
  29   31   *        All Rights Reserved
  30   32   */
  31   33  
  32   34  /*
  33   35   * University Copyright- Copyright (c) 1982, 1986, 1988
  34   36   * The Regents of the University of California
  35   37   * All Rights Reserved
  36   38   *
  37   39   * University Acknowledgment- Portions of this document are derived from
  38   40   * software developed by the University of California, Berkeley, and its
  39   41   * contributors.
  40   42   */
  41   43  
  42      -#pragma ident   "%Z%%M% %I%     %E% SMI"
  43      -
  44   44  /*
  45   45   * last
  46   46   */
  47   47  #include <sys/types.h>
  48   48  #include <stdio.h>
  49   49  #include <stdlib.h>
  50   50  #include <unistd.h>
  51   51  #include <strings.h>
  52   52  #include <signal.h>
  53   53  #include <sys/stat.h>
  54   54  #include <pwd.h>
  55   55  #include <fcntl.h>
  56   56  #include <utmpx.h>
  57   57  #include <locale.h>
  58   58  #include <ctype.h>
  59   59  
  60   60  /*
  61      - * NMAX, LMAX and HMAX are set to these values for now. They
  62      - * should be much higher because of the max allowed limit in
  63      - * utmpx.h
       61 + * Use the full lengths from utmpx for NMAX, LMAX and HMAX .
  64   62   */
  65      -#define NMAX    8
  66      -#define LMAX    12
       63 +#define NMAX    (sizeof (((struct utmpx *)0)->ut_user))
       64 +#define LMAX    (sizeof (((struct utmpx *)0)->ut_line))
  67   65  #define HMAX    (sizeof (((struct utmpx *)0)->ut_host))
       66 +
       67 +/* Print minimum field widths. */
       68 +#define LOGIN_WIDTH     8
       69 +#define LINE_WIDTH      12
       70 +
  68   71  #define SECDAY  (24*60*60)
  69   72  #define CHUNK_SIZE 256
  70   73  
  71   74  #define lineq(a, b)     (strncmp(a, b, LMAX) == 0)
  72   75  #define nameq(a, b)     (strncmp(a, b, NMAX) == 0)
  73   76  #define hosteq(a, b)    (strncmp(a, b, HMAX) == 0)
  74   77  #define linehostnameq(a, b, c, d) \
  75   78              (lineq(a, b)&&hosteq(a+LMAX+1, c)&&nameq(a+LMAX+HMAX+2, d))
  76   79  
  77   80  #define USAGE   "usage: last [-n number] [-f filename] [-a ] [name | tty] ...\n"
↓ open down ↓ 169 lines elided ↑ open up ↑
 247  250                  (void) fprintf(stderr, gettext("Out of memory \n "));
 248  251                  exit(2);
 249  252          }
 250  253                  for (bl--; bl >= 0; bl--) {
 251  254                  (void) lseek(wtmp, (off_t)(bl * sizeof (buf)), 0);
 252  255                  bp = &buf[read(wtmp, buf, sizeof (buf)) / sizeof (buf[0]) - 1];
 253  256                  for (; bp >= buf; bp--) {
 254  257                          if (want(bp, &ut_host, &ut_user)) {
 255  258                                  for (i = 0; i <= lines; i++) {
 256  259                                  if (i == lines)
 257      -                                    reallocate_buffer();
      260 +                                        reallocate_buffer();
 258  261                                  if (ttnames[i] == NULL) {
 259      -                                    memory_alloc(i);
      262 +                                        memory_alloc(i);
 260  263                                          /*
 261  264                                           * LMAX+HMAX+NMAX+3 bytes have been
 262  265                                           * allocated for ttnames[i].
 263  266                                           * If bp->ut_line is longer than LMAX,
 264  267                                           * ut_host is longer than HMAX,
 265  268                                           * and ut_user is longer than NMAX,
 266  269                                           * truncate it to fit ttnames[i].
 267  270                                           */
 268  271                                          (void) strlcpy(ttnames[i], bp->ut_line,
 269      -                                                LMAX+1);
      272 +                                            LMAX+1);
 270  273                                          (void) strlcpy(ttnames[i]+LMAX+1,
 271      -                                                ut_host, HMAX+1);
      274 +                                            ut_host, HMAX+1);
 272  275                                          (void) strlcpy(ttnames[i]+LMAX+HMAX+2,
 273      -                                                ut_user, NMAX+1);
      276 +                                            ut_user, NMAX+1);
 274  277                                                  record_time(&otime, &print,
 275      -                                                        i, bp);
      278 +                                                    i, bp);
 276  279                                                  break;
 277  280                                          } else if (linehostnameq(ttnames[i],
 278  281                                              bp->ut_line, ut_host, ut_user)) {
 279  282                                                  record_time(&otime,
 280  283                                                      &print, i, bp);
 281  284                                                  break;
 282  285                                          }
 283  286                                  }
 284  287                          }
 285  288                          if (print) {
 286  289                                  if (strncmp(bp->ut_line, "ftp", 3) == 0)
 287  290                                          bp->ut_line[3] = '\0';
 288  291                                  if (strncmp(bp->ut_line, "uucp", 4) == 0)
 289  292                                          bp->ut_line[4] = '\0';
 290  293  
 291  294                                  ct = ctime(&bp->ut_xtime);
 292  295                                  (void) printf(gettext("%-*.*s  %-*.*s "),
 293      -                                    NMAX, NMAX, bp->ut_name,
 294      -                                    LMAX, LMAX, bp->ut_line);
      296 +                                    LOGIN_WIDTH, NMAX, bp->ut_name,
      297 +                                    LINE_WIDTH, LMAX, bp->ut_line);
 295  298                                  hostf_len = strlen(bp->ut_host);
 296  299                                  (void) snprintf(hostf, sizeof (hostf),
 297  300                                      "%-*.*s", hostf_len, hostf_len,
 298  301                                      bp->ut_host);
 299  302                                  fpos = snprintf(timef, sizeof (timef),
 300      -                                        "%10.10s %5.5s ",
      303 +                                    "%10.10s %5.5s ",
 301  304                                      ct, 11 + ct);
 302  305                                  if (!lineq(bp->ut_line, "system boot") &&
 303  306                                      !lineq(bp->ut_line, "system down")) {
 304  307                                          if (otime == 0 &&
 305  308                                              bp->ut_type == USER_PROCESS) {
 306  309  
 307  310          if (fpos < sizeof (timef)) {
 308  311                  /* timef still has room */
 309  312                  (void) snprintf(timef + fpos, sizeof (timef) - fpos,
 310      -                        gettext("  still logged in"));
      313 +                    gettext("  still logged in"));
 311  314          }
 312  315  
 313  316                                          } else {
 314  317                                          time_t delta;
 315  318                                          if (otime < 0) {
 316  319                                                  otime = -otime;
 317  320                                                  /*
 318  321                                                   * TRANSLATION_NOTE
 319  322                                                   * See other notes on "down"
 320  323                                                   * and "- %5.5s".
↓ open down ↓ 3 lines elided ↑ open up ↑
 324  327                                                   *      16:20 - down
 325  328                                                   * You probably don't want to
 326  329                                                   * translate this.  Should you
 327  330                                                   * decide to translate this,
 328  331                                                   * translate "- %5.5s" too.
 329  332                                                   */
 330  333  
 331  334          if (fpos < sizeof (timef)) {
 332  335                  /* timef still has room */
 333  336                  chrcnt = snprintf(timef + fpos, sizeof (timef) - fpos,
 334      -                        gettext("- %s"), crmsg);
      337 +                    gettext("- %s"), crmsg);
 335  338                  fpos += chrcnt;
 336  339          }
 337  340  
 338  341                                          } else {
 339  342  
 340  343          if (fpos < sizeof (timef)) {
 341  344                  /* timef still has room */
 342  345                  chrcnt = snprintf(timef + fpos, sizeof (timef) - fpos,
 343      -                        gettext("- %5.5s"), ctime(&otime) + 11);
      346 +                    gettext("- %5.5s"), ctime(&otime) + 11);
 344  347                  fpos += chrcnt;
 345  348          }
 346  349  
 347  350                                          }
 348  351                                          delta = otime - bp->ut_xtime;
 349  352                                          if (delta < SECDAY) {
 350  353  
 351  354          if (fpos < sizeof (timef)) {
 352  355                  /* timef still has room */
 353  356                  (void) snprintf(timef + fpos, sizeof (timef) - fpos,
 354      -                        gettext("  (%5.5s)"), asctime(gmtime(&delta)) + 11);
      357 +                    gettext("  (%5.5s)"), asctime(gmtime(&delta)) + 11);
 355  358          }
 356  359  
 357  360                                          } else {
 358  361  
 359  362          if (fpos < sizeof (timef)) {
 360  363                  /* timef still has room */
 361  364                  (void) snprintf(timef + fpos, sizeof (timef) - fpos,
 362      -                        gettext(" (%ld+%5.5s)"), delta / SECDAY,
      365 +                    gettext(" (%ld+%5.5s)"), delta / SECDAY,
 363  366                      asctime(gmtime(&delta)) + 11);
 364  367          }
 365  368  
 366  369                                          }
 367  370                                  }
 368  371                                  }
 369  372                                  if (aflag)
 370  373                                          (void) printf("%-35.35s %-.*s\n",
 371  374                                              timef, strlen(hostf), hostf);
 372  375                                  else
↓ open down ↓ 38 lines elided ↑ open up ↑
 411  414          static char     **tmpttnames;
 412  415          static time_t   *tmplogouts;
 413  416  
 414  417          lines += CHUNK_SIZE;
 415  418          tmpttnames = realloc(ttnames, sizeof (char *)*lines);
 416  419          tmplogouts = realloc(logouts, sizeof (time_t)*lines);
 417  420          if (tmpttnames == NULL || tmplogouts == NULL) {
 418  421                  (void) fprintf(stderr, gettext("Out of memory \n"));
 419  422                  exit(2);
 420  423          } else {
 421      -            ttnames = tmpttnames;
 422      -            logouts = tmplogouts;
      424 +                ttnames = tmpttnames;
      425 +                logouts = tmplogouts;
 423  426          }
 424  427          for (j = lines-CHUNK_SIZE; j < lines; j++) {
 425  428                  ttnames[j] = NULL;
 426  429                  logouts[j] = bootxtime;
 427  430          }
 428  431  }
 429  432  
 430  433  static void
 431  434  memory_alloc(int i)
 432  435  {
↓ open down ↓ 99 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX