hust testsuite: fix a false positive
[oweals/busybox.git] / miscutils / last_fancy.c
index 7e69fc2811ccb0b3b3d9d920a128503ace3f096b..e56e0ba85995772ecf5f28e3ea33598e0ee4d223 100644 (file)
@@ -8,7 +8,6 @@
  */
 
 #include "libbb.h"
-#include <utmp.h>
 
 /* NB: ut_name and ut_user are the same field, use only one name (ut_user)
  * to reduce confusion */
 #define HEADER_LINE_WIDE  "USER", "TTY", \
        INET6_ADDRSTRLEN, INET6_ADDRSTRLEN, "HOST", "LOGIN", "  TIME", ""
 
+#if !defined __UT_LINESIZE && defined UT_LINESIZE
+# define __UT_LINESIZE UT_LINESIZE
+#endif
+
 enum {
        NORMAL,
        LOGGED,
@@ -40,10 +43,10 @@ enum {
 
 #define show_wide (option_mask32 & LAST_OPT_W)
 
-static void show_entry(struct utmp *ut, int state, time_t dur_secs)
+static void show_entry(struct utmpx *ut, int state, time_t dur_secs)
 {
        unsigned days, hours, mins;
-       char duration[32];
+       char duration[sizeof("(%u+02:02)") + sizeof(int)*3];
        char login_time[17];
        char logout_time[8];
        const char *logout_str;
@@ -54,7 +57,8 @@ static void show_entry(struct utmp *ut, int state, time_t dur_secs)
         * 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);
+       tmp = dur_secs;
+       snprintf(logout_time, 8, "- %s", ctime(&tmp) + 11);
 
        dur_secs = MAX(dur_secs - (time_t)ut->ut_tv.tv_sec, (time_t)0);
        /* unsigned int is easier to divide than time_t (which may be signed long) */
@@ -94,17 +98,17 @@ static void show_entry(struct utmp *ut, int state, time_t dur_secs)
        }
 
        printf(HEADER_FORMAT,
-                  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);
+               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)
+static int get_ut_type(struct utmpx *ut)
 {
        if (ut->ut_line[0] == '~') {
                if (strcmp(ut->ut_user, "shutdown") == 0) {
@@ -142,7 +146,7 @@ static int get_ut_type(struct utmp *ut)
        return ut->ut_type;
 }
 
-static int is_runlevel_shutdown(struct utmp *ut)
+static int is_runlevel_shutdown(struct utmpx *ut)
 {
        if (((ut->ut_pid & 255) == '0') || ((ut->ut_pid & 255) == '6')) {
                return 1;
@@ -154,7 +158,7 @@ static int is_runlevel_shutdown(struct utmp *ut)
 int last_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int last_main(int argc UNUSED_PARAM, char **argv)
 {
-       struct utmp ut;
+       struct utmpx ut;
        const char *filename = _PATH_WTMP;
        llist_t *zlist;
        off_t pos;
@@ -162,11 +166,10 @@ int last_main(int argc UNUSED_PARAM, 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 */
@@ -230,7 +233,7 @@ int last_main(int argc UNUSED_PARAM, char **argv)
                                break;
                        }
                        /* add_entry */
-                       llist_add_to(&zlist, memcpy(xmalloc(sizeof(ut)), &ut, sizeof(ut)));
+                       llist_add_to(&zlist, xmemdup(&ut, sizeof(ut)));
                        break;
                case USER_PROCESS: {
                        int show;
@@ -243,9 +246,9 @@ int last_main(int argc UNUSED_PARAM, char **argv)
                        {
                                llist_t *el, *next;
                                for (el = zlist; el; el = next) {
-                                       struct utmp *up = (struct utmp *)el->data;
+                                       struct utmpx *up = (struct utmpx *)el->data;
                                        next = el->link;
-                                       if (strncmp(up->ut_line, ut.ut_line, UT_LINESIZE) == 0) {
+                                       if (strncmp(up->ut_line, ut.ut_line, __UT_LINESIZE) == 0) {
                                                if (show) {
                                                        show_entry(&ut, NORMAL, up->ut_tv.tv_sec);
                                                        show = 0;
@@ -272,7 +275,7 @@ int last_main(int argc UNUSED_PARAM, char **argv)
                                show_entry(&ut, state, boot_time);
                        }
                        /* add_entry */
-                       llist_add_to(&zlist, memcpy(xmalloc(sizeof(ut)), &ut, sizeof(ut)));
+                       llist_add_to(&zlist, xmemdup(&ut, sizeof(ut)));
                        break;
                }
                }