Fix some compiler warnings emitted by gcc-4.8.0
[oweals/busybox.git] / miscutils / last_fancy.c
index 03df8dcdde0f27edc848ad8383da99166505f87d..f687d7e16c7c201c4f784624810c2b95c814e67b 100644 (file)
@@ -4,9 +4,18 @@
  *
  * Copyright (C) 2008 by Patricia Muscalu <patricia.muscalu@axis.com>
  *
- * Licensed under the GPLv2 or later, see the file LICENSE in this tarball.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 
+#include "libbb.h"
+
+/* NB: ut_name and ut_user are the same field, use only one name (ut_user)
+ * to reduce confusion */
+
+#ifndef SHUTDOWN_TIME
+#  define SHUTDOWN_TIME 254
+#endif
+
 #define HEADER_FORMAT     "%-8.8s %-12.12s %-*.*s %-16.16s %-7.7s %s\n"
 #define HEADER_LINE       "USER", "TTY", \
        INET_ADDRSTRLEN, INET_ADDRSTRLEN, "HOST", "LOGIN", "  TIME", ""
@@ -38,8 +47,12 @@ static void show_entry(struct utmp *ut, int state, time_t dur_secs)
        char logout_time[8];
        const char *logout_str;
        const char *duration_str;
+       time_t tmp;
 
-       safe_strncpy(login_time, ctime(&(ut->ut_time)), 17);
+       /* manpages say ut_tv.tv_sec *is* time_t,
+        * but some systems have it wrong */
+       tmp = ut->ut_tv.tv_sec;
+       safe_strncpy(login_time, ctime(&tmp), 17);
        snprintf(logout_time, 8, "- %s", ctime(&dur_secs) + 11);
 
        dur_secs = MAX(dur_secs - (time_t)ut->ut_tv.tv_sec, (time_t)0);
@@ -50,11 +63,11 @@ static void show_entry(struct utmp *ut, int state, time_t dur_secs)
        hours = mins / 60;
        mins = mins % 60;
 
-       if (days) {
+//     if (days) {
                sprintf(duration, "(%u+%02u:%02u)", days, hours, mins);
-       } else {
-               sprintf(duration, " (%02u:%02u)", hours, mins);
-       }
+//     } else {
+//             sprintf(duration, " (%02u:%02u)", hours, mins);
+//     }
 
        logout_str = logout_time;
        duration_str = duration;
@@ -80,44 +93,44 @@ static void show_entry(struct utmp *ut, int state, time_t dur_secs)
        }
 
        printf(HEADER_FORMAT,
-                  ut->ut_name,
-                  ut->ut_line,
-                  show_wide ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN,
-                  show_wide ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN,
-                  ut->ut_host,
-                  login_time,
-                  logout_str,
-                  duration_str);
+               ut->ut_user,
+               ut->ut_line,
+               show_wide ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN,
+               show_wide ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN,
+               ut->ut_host,
+               login_time,
+               logout_str,
+               duration_str);
 }
 
 static int get_ut_type(struct utmp *ut)
 {
        if (ut->ut_line[0] == '~') {
-               if (strncmp(ut->ut_user, "shutdown", sizeof("shutdown")-1) == 0) {
+               if (strcmp(ut->ut_user, "shutdown") == 0) {
                        return SHUTDOWN_TIME;
                }
-               if (strncmp(ut->ut_user, "reboot", sizeof("reboot")-1) == 0) {
+               if (strcmp(ut->ut_user, "reboot") == 0) {
                        return BOOT_TIME;
                }
-               if (strncmp(ut->ut_user, "runlevel", sizeof("runlevel")-1) == 0) {
+               if (strcmp(ut->ut_user, "runlevel") == 0) {
                        return RUN_LVL;
                }
                return ut->ut_type;
        }
 
-       if (ut->ut_name[0] == 0) {
+       if (ut->ut_user[0] == 0) {
                return DEAD_PROCESS;
        }
 
        if ((ut->ut_type != DEAD_PROCESS)
-        && (strcmp(ut->ut_name, "LOGIN") != 0)
-        && ut->ut_name[0]
+        && (strcmp(ut->ut_user, "LOGIN") != 0)
+        && ut->ut_user[0]
         && ut->ut_line[0]
        ) {
                ut->ut_type = USER_PROCESS;
        }
 
-       if (strcmp(ut->ut_name, "date") == 0) {
+       if (strcmp(ut->ut_user, "date") == 0) {
                if (ut->ut_line[0] == '|') {
                        return OLD_TIME;
                }
@@ -138,7 +151,7 @@ static int is_runlevel_shutdown(struct utmp *ut)
 }
 
 int last_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int last_main(int argc ATTRIBUTE_UNUSED, char **argv)
+int last_main(int argc UNUSED_PARAM, char **argv)
 {
        struct utmp ut;
        const char *filename = _PATH_WTMP;
@@ -148,11 +161,10 @@ int last_main(int argc ATTRIBUTE_UNUSED, char **argv)
        time_t boot_time;
        time_t down_time;
        int file;
-       unsigned opt;
        smallint going_down;
        smallint boot_down;
 
-       opt = getopt32(argv, "Wf:" /* "H" */, &filename);
+       /*opt =*/ getopt32(argv, "Wf:" /* "H" */, &filename);
 #ifdef BUT_UTIL_LINUX_LAST_HAS_NO_SUCH_OPT
        if (opt & LAST_OPT_H) {
                /* Print header line */
@@ -165,41 +177,42 @@ int last_main(int argc ATTRIBUTE_UNUSED, char **argv)
 #endif
 
        file = xopen(filename, O_RDONLY);
-       if (full_read(file, &ut, sizeof(ut)) != sizeof(ut)) {
+       {
+               /* in case the file is empty... */
                struct stat st;
                fstat(file, &st);
                start_time = st.st_ctime;
-               goto exit;
        }
-       start_time = ut.ut_time;
 
        time(&down_time);
        going_down = 0;
        boot_down = NORMAL; /* 0 */
        zlist = NULL;
        boot_time = 0;
-       pos = 0;
+       /* get file size, rounding down to last full record */
+       pos = xlseek(file, 0, SEEK_END) / sizeof(ut) * sizeof(ut);
        for (;;) {
                pos -= (off_t)sizeof(ut);
-               /* Bug? What if file changes size?
-                * What if size is not a multiple of sizeof(ut)? */
-               if (lseek(file, pos, SEEK_END) < 0) {
+               if (pos < 0) {
                        /* Beyond the beginning of the file boundary =>
                         * the whole file has been read. */
                        break;
                }
-               if (full_read(file, &ut, sizeof(ut)) != sizeof(ut))
-                       break;
+               xlseek(file, pos, SEEK_SET);
+               xread(file, &ut, sizeof(ut));
+               /* rewritten by each record, eventially will have
+                * first record's ut_tv.tv_sec: */
+               start_time = ut.ut_tv.tv_sec;
 
                switch (get_ut_type(&ut)) {
                case SHUTDOWN_TIME:
-                       down_time = ut.ut_time;
+                       down_time = ut.ut_tv.tv_sec;
                        boot_down = DOWN;
                        going_down = 1;
                        break;
                case RUN_LVL:
                        if (is_runlevel_shutdown(&ut)) {
-                               down_time = ut.ut_time;
+                               down_time = ut.ut_tv.tv_sec;
                                going_down = 1;
                                boot_down = DOWN;
                        }
@@ -232,7 +245,7 @@ int last_main(int argc ATTRIBUTE_UNUSED, char **argv)
                                        next = el->link;
                                        if (strncmp(up->ut_line, ut.ut_line, UT_LINESIZE) == 0) {
                                                if (show) {
-                                                       show_entry(&ut, NORMAL, up->ut_time);
+                                                       show_entry(&ut, NORMAL, up->ut_tv.tv_sec);
                                                        show = 0;
                                                }
                                                llist_unlink(&zlist, el);
@@ -263,7 +276,7 @@ int last_main(int argc ATTRIBUTE_UNUSED, char **argv)
                }
 
                if (going_down) {
-                       boot_time = ut.ut_time;
+                       boot_time = ut.ut_tv.tv_sec;
                        llist_free(zlist, free);
                        zlist = NULL;
                        going_down = 0;
@@ -274,7 +287,6 @@ int last_main(int argc ATTRIBUTE_UNUSED, char **argv)
                llist_free(zlist, free);
        }
 
- exit:
        printf("\nwtmp begins %s", ctime(&start_time));
 
        if (ENABLE_FEATURE_CLEAN_UP)