changeset 1278:324306321d82 draft

Initial cleanup of last: mostly whitespace, move no record test to start of loop, don't bother to stat an empty file to report when an empty log was created (just report current time).
author Rob Landley <rob@landley.net>
date Sat, 10 May 2014 13:20:03 -0500
parents 23817e1675f2
children 29603c5a3317
files toys/pending/last.c
diffstat 1 files changed, 23 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/toys/pending/last.c	Sat May 10 13:06:31 2014 -0500
+++ b/toys/pending/last.c	Sat May 10 13:20:03 2014 -0500
@@ -18,8 +18,8 @@
     -W      Display the information without host-column truncation.
     -f FILE Read from file FILE instead of /var/log/wtmp.
 */
+
 #define FOR_last
-
 #include "toys.h"
 #include <utmp.h>
 
@@ -29,6 +29,7 @@
 
 GLOBALS(
   char *file;
+
   struct arg_list *list;
 )
 
@@ -94,7 +95,6 @@
 void last_main(void)
 {
   struct utmp ut;
-  struct stat sb;
   time_t tm[3] = {0,}; //array for time avlues, previous, current
   char *file = "/var/log/wtmp";
   int fd, pwidth, curlog_type = EMPTY;
@@ -102,31 +102,26 @@
 
   if (toys.optflags & FLAG_f) file = TT.file;
 
-  TT.list = NULL;
   pwidth = (toys.optflags & FLAG_W) ? 46 : 16;
-  time(&tm[1]);
+  *tm = time(tm+1);
   fd = xopen(file, O_RDONLY);
   loc = xlseek(fd, 0, SEEK_END);
-  // in case of empty file or 'filesize < sizeof(ut)'
-  fstat(fd, &sb);
-  if (sizeof(ut) > sb.st_size) {
-    xclose(fd);
-    printf("\n%s begins %-24.24s\n", basename(file), ctime(&sb.st_ctime));
-    return;
-  }
-  loc = xlseek(fd, loc - sizeof(ut), SEEK_SET);
 
-  while (1) {
+  // Loop through file structures in reverse order.
+  for (;;) {
+    loc -= sizeof(ut);
+    if(loc < 0) break;
+    xlseek(fd, loc, SEEK_SET);
+
     xreadall(fd, &ut, sizeof(ut));
-    tm[0] = (time_t)ut.ut_tv.tv_sec;
-    if (ut.ut_line[0] == '~') {
+    *tm = ut.ut_tv.tv_sec;
+    if (*ut.ut_line == '~') {
       if (!strcmp(ut.ut_user, "runlevel")) ut.ut_type = RUN_LVL;
       else if (!strcmp(ut.ut_user, "reboot")) ut.ut_type = BOOT_TIME;
       else if (!strcmp(ut.ut_user, "shutdown")) ut.ut_type = SHUTDOWN_TIME;
-    } 
-    else if (ut.ut_user[0] == '\0') ut.ut_type = DEAD_PROCESS;
-    else if (ut.ut_user[0] && ut.ut_line[0] && (ut.ut_type != DEAD_PROCESS)
-        && (strcmp(ut.ut_user, "LOGIN")) ) ut.ut_type = USER_PROCESS;
+    } else if (!*ut.ut_user) ut.ut_type = DEAD_PROCESS;
+    else if (*ut.ut_user && *ut.ut_line && ut.ut_type != DEAD_PROCESS
+        && strcmp(ut.ut_user, "LOGIN")) ut.ut_type = USER_PROCESS;
     /* The pair of terminal names '|' / '}' logs the
      * old/new system time when date changes it.
      */ 
@@ -134,8 +129,10 @@
       if (ut.ut_line[0] == '|') ut.ut_type = OLD_TIME;
       if (ut.ut_line[0] == '{') ut.ut_type = NEW_TIME;
     }
-    if ( (ut.ut_type == SHUTDOWN_TIME) || ((ut.ut_type == RUN_LVL) && 
-        (((ut.ut_pid & 255) == '0') || ((ut.ut_pid & 255) == '6')))) {
+
+    if ((ut.ut_type == SHUTDOWN_TIME) || ((ut.ut_type == RUN_LVL) && 
+        (((ut.ut_pid & 255) == '0') || ((ut.ut_pid & 255) == '6'))))
+    {
       tm[1] = tm[2] = (time_t)ut.ut_tv.tv_sec;
       free_list();
       curlog_type = RUN_LVL;
@@ -150,6 +147,7 @@
       tm[2] = (time_t)ut.ut_tv.tv_sec;
     } else if (ut.ut_type == USER_PROCESS && *ut.ut_line) {
       struct arg_list *l = find_and_dlink(&TT.list, ut.ut_line);
+
       if (l) {
         struct utmp *u = (struct utmp *)l->arg;
         seize_duration(tm[0], u->ut_tv.tv_sec);
@@ -194,10 +192,11 @@
     loc -= sizeof(ut);
     if(loc < 0) break;
     xlseek(fd, loc, SEEK_SET);
-  } // End of while.
+  }
 
-  fflush(stdout);
   xclose(fd);
+
   if (CFG_TOYBOX_FREE) free_list();
-  printf("\n%s begins %-24.24s\n", basename(file), ctime(&tm[0]));
+
+  xprintf("\n%s begins %-24.24s\n", basename(file), ctime(tm));
 }