added depend label
[unix-history] / usr / src / usr.bin / lastcomm / lastcomm.c
index cf016c5..64335da 100644 (file)
@@ -1,6 +1,18 @@
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)lastcomm.c  4.9 (Berkeley) %G%";
-#endif
+char copyright[] =
+"@(#) Copyright (c) 1980 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
+
+#ifndef lint
+static char sccsid[] = "@(#)lastcomm.c 5.3 (Berkeley) %G%";
+#endif not lint
 
 /*
  * last command
 
 /*
  * last command
@@ -30,6 +42,8 @@ main(argc, argv)
        register struct acct *acp;
        int fd;
        struct stat sb;
        register struct acct *acp;
        int fd;
        struct stat sb;
+       long lseek();
+       char *strcpy(), *ctime();
 
        fd = open("/usr/adm/acct", O_RDONLY);
        if (fd < 0) {
 
        fd = open("/usr/adm/acct", O_RDONLY);
        if (fd < 0) {
@@ -37,8 +51,8 @@ main(argc, argv)
                exit(1);
        }
        fstat(fd, &sb);
                exit(1);
        }
        fstat(fd, &sb);
-       for (bn = btodb(sb.st_size) - 1; bn >= 0; bn--) {
-               lseek(fd, bn * DEV_BSIZE, L_SET);
+       for (bn = btodb(sb.st_size); bn >= 0; bn--) {
+               (void)lseek(fd, (off_t)dbtob(bn), L_SET);
                cc = read(fd, buf, DEV_BSIZE);
                if (cc < 0) {
                        perror("read");
                cc = read(fd, buf, DEV_BSIZE);
                if (cc < 0) {
                        perror("read");
@@ -47,24 +61,26 @@ main(argc, argv)
                acp = buf + (cc / sizeof (buf[0])) - 1;
                for (; acp >= buf; acp--) {
                        register char *cp;
                acp = buf + (cc / sizeof (buf[0])) - 1;
                for (; acp >= buf; acp--) {
                        register char *cp;
-                       time_t x =
-                           expand(acp->ac_utime) + expand(acp->ac_stime);
+                       time_t x;
 
 
-                       acp->ac_comm[10] = '\0';
-                       if (*acp->ac_comm == '\0')
+                       if (acp->ac_comm[0] == '\0')
                                strcpy(acp->ac_comm, "?");
                                strcpy(acp->ac_comm, "?");
-                       for (cp = acp->ac_comm; *cp; cp++)
-                               if (iscntrl(*cp))
+                       for (cp = &acp->ac_comm[0];
+                            cp < &acp->ac_comm[fldsiz(acct, ac_comm)] && *cp;
+                            cp++)
+                               if (!isascii(*cp) || iscntrl(*cp))
                                        *cp = '?';
                                        *cp = '?';
-                       if (!ok(argc, argv, acp) && argc != 1)
+                       if (argc > 1 && !ok(argc, argv, acp))
                                continue;
                                continue;
-                       printf("%-*s %s %-*s %-*s %4d sec%s %.16s\n",
-                               fldsiz(acct, ac_comm), acp->ac_comm,
-                               flagbits(acp->ac_flag),
-                               fldsiz(utmp, ut_name), getname(acp->ac_uid),
+                       x = expand(acp->ac_utime) + expand(acp->ac_stime);
+                       printf("%-*.*s %s %-*s %-*s %6.2f secs %.16s\n",
+                               fldsiz(acct, ac_comm),
+                               fldsiz(acct, ac_comm),
+                               acp->ac_comm, flagbits(acp->ac_flag),
+                               fldsiz(utmp, ut_name),
+                               (cp = getname(acp->ac_uid)) ? cp : "",
                                fldsiz(utmp, ut_line), getdev(acp->ac_tty),
                                fldsiz(utmp, ut_line), getdev(acp->ac_tty),
-                               x, x > 1 || x == 0 ? "s" : " ",
-                               ctime(&acp->ac_btime));
+                               x / (double)AHZ, ctime(&acp->ac_btime));
                }
        }
 }
                }
        }
 }
@@ -107,11 +123,12 @@ ok(argc, argv, acp)
        register struct acct *acp;
 {
        register int j;
        register struct acct *acp;
 {
        register int j;
+       register char *cp;
 
        for (j = 1; j < argc; j++)
 
        for (j = 1; j < argc; j++)
-               if (strcmp(getname(acp->ac_uid), argv[j]) &&
-                   strcmp(getdev(acp->ac_tty), argv[j]) &&
-                   strcmp(acp->ac_comm, argv[j]))
+               if ((!(cp = getname(acp->ac_uid)) || strcmp(cp, argv[j])) &&
+                   (!(cp = getdev(acp->ac_tty)) || strcmp(cp, argv[j])) &&
+                   strncmp(acp->ac_comm, argv[j], fldsiz(acct, ac_comm)))
                        break;
        return (j == argc);
 }
                        break;
        return (j == argc);
 }
@@ -129,14 +146,15 @@ int       outrangeuid = -1;
 
 char *
 getname(uid)
 
 char *
 getname(uid)
+       uid_t uid;
 {
        register struct passwd *pw;
        static init;
        struct passwd *getpwent();
 
 {
        register struct passwd *pw;
        static init;
        struct passwd *getpwent();
 
-       if (uid >= 0 && uid < NUID && names[uid][0])
+       if (uid < NUID && names[uid][0])
                return (&names[uid][0]);
                return (&names[uid][0]);
-       if (uid >= 0 && uid == outrangeuid)
+       if (uid == outrangeuid)
                return (outrangename);
        if (init == 2) {
                if (uid < NUID)
                return (outrangename);
        if (init == 2) {
                if (uid < NUID)
@@ -195,15 +213,16 @@ setupdevs()
        register struct devhash * hashtab;
        register ndevs = NDEVS;
        struct direct * dp;
        register struct devhash * hashtab;
        register ndevs = NDEVS;
        struct direct * dp;
+       char *malloc();
 
 
-       if ((fd = opendir("/dev")) == NULL) {
-               perror("/dev");
-               return;
-       }
+       /*NOSTRICT*/
        hashtab = (struct devhash *)malloc(NDEVS * sizeof(struct devhash));
        if (hashtab == (struct devhash *)0) {
        hashtab = (struct devhash *)malloc(NDEVS * sizeof(struct devhash));
        if (hashtab == (struct devhash *)0) {
-               fprintf(stderr, "No mem for dev table\n");
-               closedir(fd);
+               fputs("No mem for dev table\n", stderr);
+               return;
+       }
+       if ((fd = opendir("/dev")) == NULL) {
+               perror("/dev");
                return;
        }
        while (dp = readdir(fd)) {
                return;
        }
        while (dp = readdir(fd)) {
@@ -232,6 +251,7 @@ getdev(dev)
        static dev_t lastdev = (dev_t) -1;
        static char *lastname;
        static int init = 0;
        static dev_t lastdev = (dev_t) -1;
        static char *lastname;
        static int init = 0;
+       char *strcpy(), *strcat();
 
        if (dev == NODEV)
                return ("__");
 
        if (dev == NODEV)
                return ("__");