Date: Mon, 14 Jul 2014 10:15:44 GMT From: zkorchev@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r270816 - soc2014/zkorchev/freebsd_head/usr.bin/last Message-ID: <201407141015.s6EAFiKE077025@socsvn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: zkorchev Date: Mon Jul 14 10:15:44 2014 New Revision: 270816 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=270816 Log: libsol support for last Modified: soc2014/zkorchev/freebsd_head/usr.bin/last/Makefile soc2014/zkorchev/freebsd_head/usr.bin/last/last.c Modified: soc2014/zkorchev/freebsd_head/usr.bin/last/Makefile ============================================================================== --- soc2014/zkorchev/freebsd_head/usr.bin/last/Makefile Mon Jul 14 09:52:33 2014 (r270815) +++ soc2014/zkorchev/freebsd_head/usr.bin/last/Makefile Mon Jul 14 10:15:44 2014 (r270816) @@ -3,6 +3,9 @@ PROG= last +LDADD+= -lsol +CFLAGS+=-DSOL_ON -I/usr/local/include + NO_WFORMAT= .include <bsd.prog.mk> Modified: soc2014/zkorchev/freebsd_head/usr.bin/last/last.c ============================================================================== --- soc2014/zkorchev/freebsd_head/usr.bin/last/last.c Mon Jul 14 09:52:33 2014 (r270815) +++ soc2014/zkorchev/freebsd_head/usr.bin/last/last.c Mon Jul 14 10:15:44 2014 (r270816) @@ -57,6 +57,9 @@ #include <unistd.h> #include <utmpx.h> #include <sys/queue.h> +#if defined(SOL_ON) +# include <sol.h> +#endif #define NO 0 /* false/no */ #define YES 1 /* true/yes */ @@ -94,6 +97,11 @@ * at this snapshot time */ +#if defined(SOL_ON) +static struct sol_stream sol_stream; +#endif +static int sol_format; + static void addarg(int, char *); static time_t dateconv(char *); static void doentry(struct utmpx *); @@ -188,7 +196,17 @@ addarg(USER_TYPE, *argv); } } - wtmp(); +#if defined(SOL_ON) + sol_format = sol_init(&sol_stream); + if (sol_format) { + sol_array_start(&sol_stream); + wtmp(); + sol_array_end(&sol_stream); + sol_term(&sol_stream); + } + else +#endif + wtmp(); exit(0); } @@ -228,9 +246,11 @@ while (amount > 0) doentry(&buf[--amount]); - tm = localtime(&t); - (void) strftime(ct, sizeof(ct), "%+", tm); - printf("\n%s begins %s\n", ((file == NULL) ? "utx.log" : file), ct); + if (!sol_format) { + tm = localtime(&t); + (void) strftime(ct, sizeof(ct), "%+", tm); + printf("\n%s begins %s\n", ((file == NULL) ? "utx.log" : file), ct); + } } /* @@ -318,58 +338,143 @@ struct tm *tm; time_t delta; /* time difference */ time_t t; + size_t ct_len; if (maxrec != -1 && !maxrec--) exit(0); t = bp->ut_tv.tv_sec; tm = localtime(&t); - (void) strftime(ct, sizeof(ct), d_first ? + ct_len = strftime(ct, sizeof(ct), d_first ? (yflag ? "%a %e %b %Y %R" : "%a %e %b %R") : (yflag ? "%a %b %e %Y %R" : "%a %b %e %R"), tm); - switch (bp->ut_type) { - case BOOT_TIME: - printf("%-42s", "boot time"); - break; - case SHUTDOWN_TIME: - printf("%-42s", "shutdown time"); - break; - case OLD_TIME: - printf("%-42s", "old time"); - break; - case NEW_TIME: - printf("%-42s", "new time"); - break; - case USER_PROCESS: - printf("%-10s %-8s %-22.22s", - bp->ut_user, bp->ut_line, bp->ut_host); - break; + +#if defined(SOL_ON) + if (sol_format) + { + sol_map_start(&sol_stream); + SOL_MAP_KEYL(&sol_stream, "type"); + switch (bp->ut_type) { + case BOOT_TIME: + sol_string(&sol_stream, "boot", 4); + break; + case SHUTDOWN_TIME: + sol_string(&sol_stream, "shutdown", 8); + break; + case OLD_TIME: + sol_string(&sol_stream, "old", 3); + break; + case NEW_TIME: + sol_string(&sol_stream, "new", 3); + break; + case USER_PROCESS: + sol_string(&sol_stream, "login", 5); + SOL_MAP_KEYL(&sol_stream, "user"); + sol_string(&sol_stream, bp->ut_user, strlen(bp->ut_user)); + SOL_MAP_KEYL(&sol_stream, "line"); + sol_string(&sol_stream, bp->ut_line, strlen(bp->ut_line)); + SOL_MAP_KEYL(&sol_stream, "host"); + sol_string(&sol_stream, bp->ut_host, strlen(bp->ut_host)); + break; + } + SOL_MAP_KEYL(&sol_stream, "time"); + sol_string(&sol_stream, ct, ct_len); + } + else +#endif + { + switch (bp->ut_type) { + case BOOT_TIME: + printf("%-42s", "boot time"); + break; + case SHUTDOWN_TIME: + printf("%-42s", "shutdown time"); + break; + case OLD_TIME: + printf("%-42s", "old time"); + break; + case NEW_TIME: + printf("%-42s", "new time"); + break; + case USER_PROCESS: + printf("%-10s %-8s %-22.22s", + bp->ut_user, bp->ut_line, bp->ut_host); + break; + } + printf(" %s%c", ct, tt == NULL ? '\n' : ' '); } - printf(" %s%c", ct, tt == NULL ? '\n' : ' '); - if (tt == NULL) + if (tt == NULL) { +#if defined(SOL_ON) + if (sol_format) sol_map_end(&sol_stream); +#endif return; + } if (!tt->logout) { - puts(" still logged in"); +#if defined(SOL_ON) + if (sol_format) + sol_map_end(&sol_stream); + else +#endif + puts(" still logged in"); return; } +#if defined(SOL_ON) + if (sol_format) SOL_MAP_KEYL(&sol_stream, "logout"); +#endif if (tt->logout < 0) { tt->logout = -tt->logout; - printf("- %s", crmsg); +#if defined(SOL_ON) + if (sol_format) + sol_string(&sol_stream, crmsg, strlen(crmsg)); + else +#endif + printf("- %s", crmsg); } else { tm = localtime(&tt->logout); - (void) strftime(ct, sizeof(ct), "%R", tm); - printf("- %s", ct); + ct_len = strftime(ct, sizeof(ct), "%R", tm); +#if defined(SOL_ON) + if (sol_format) + sol_string(&sol_stream, ct, ct_len); + else +#endif + printf("- %s", ct); } delta = tt->logout - bp->ut_tv.tv_sec; +#if defined(SOL_ON) + if (sol_format) SOL_MAP_KEYL(&sol_stream, "duration"); +#endif if (sflag) { - printf(" (%8ld)\n", (long)delta); +#if defined(SOL_ON) + if (sol_format) + sol_integer(&sol_stream, delta); + else +#endif + printf(" (%8ld)\n", (long)delta); } else { tm = gmtime(&delta); - (void) strftime(ct, sizeof(ct), width >= 8 ? "%T" : "%R", tm); - if (delta < 86400) - printf(" (%s)\n", ct); + ct_len = strftime(ct, sizeof(ct), width >= 8 ? "%T" : "%R", tm); +#if defined(SOL_ON) + if (sol_format) + { + sol_map_start(&sol_stream); + SOL_MAP_KEYL(&sol_stream, "days"); + sol_integer(&sol_stream, delta / 86400); + SOL_MAP_KEYL(&sol_stream, "hours"); + sol_string(&sol_stream, ct, ct_len); + sol_map_end(&sol_stream); + } else - printf(" (%ld+%s)\n", (long)delta / 86400, ct); +#endif + { + if (delta < 86400) + printf(" (%s)\n", ct); + else + printf(" (%ld+%s)\n", (long)delta / 86400, ct); + } } + +#if defined(SOL_ON) + if (sol_format) sol_map_end(&sol_stream); +#endif } /*
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201407141015.s6EAFiKE077025>