Date: Mon, 21 Jul 2014 09:48:34 GMT From: zkorchev@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r271188 - soc2014/zkorchev/freebsd_head/usr.bin/finger Message-ID: <201407210948.s6L9mYL9065245@socsvn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: zkorchev Date: Mon Jul 21 09:48:34 2014 New Revision: 271188 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=271188 Log: libsol support for finger Modified: soc2014/zkorchev/freebsd_head/usr.bin/finger/Makefile soc2014/zkorchev/freebsd_head/usr.bin/finger/finger.c soc2014/zkorchev/freebsd_head/usr.bin/finger/finger.h soc2014/zkorchev/freebsd_head/usr.bin/finger/sprint.c Modified: soc2014/zkorchev/freebsd_head/usr.bin/finger/Makefile ============================================================================== --- soc2014/zkorchev/freebsd_head/usr.bin/finger/Makefile Mon Jul 21 09:48:13 2014 (r271187) +++ soc2014/zkorchev/freebsd_head/usr.bin/finger/Makefile Mon Jul 21 09:48:34 2014 (r271188) @@ -5,4 +5,7 @@ SRCS= finger.c lprint.c net.c sprint.c util.c MAN= finger.1 finger.conf.5 +LDADD+= -lsol +CFLAGS+=-DSOL_ON -I/usr/local/include + .include <bsd.prog.mk> Modified: soc2014/zkorchev/freebsd_head/usr.bin/finger/finger.c ============================================================================== --- soc2014/zkorchev/freebsd_head/usr.bin/finger/finger.c Mon Jul 21 09:48:13 2014 (r271187) +++ soc2014/zkorchev/freebsd_head/usr.bin/finger/finger.c Mon Jul 21 09:48:34 2014 (r271188) @@ -81,6 +81,9 @@ #include <unistd.h> #include <utmpx.h> #include <locale.h> +#if defined(SOL_ON) +# include <sol.h> +#endif #include "finger.h" #include "pathnames.h" @@ -94,6 +97,11 @@ char tbuf[1024]; int invoker_root = 0; +#if defined(SOL_ON) +struct sol_stream sol_stream; +#endif +int sol_format; + static void loginlist(void); static int option(int, char **); static void usage(void); @@ -196,6 +204,13 @@ argc -= argcnt; argv += argcnt; +#if defined(SOL_ON) + // libsol is not useful for "large" format so don't use it with it + if (!lflag && (!*argv || sflag)) { + sol_format = sol_init(&sol_stream); + if (sol_format) sol_array_start(&sol_stream); + } +#endif (void)time(&now); setpassent(1); if (!*argv) { @@ -207,8 +222,11 @@ if (!lflag) sflag = 1; /* if -l not explicit, force -s */ loginlist(); - if (entries == 0) - (void)printf("No one logged on.\n"); +#if defined(SOL_ON) + if (sol_format) +#endif + if (entries == 0) + (void)printf("No one logged on.\n"); } else { userlist(argc, argv); /* @@ -225,6 +243,12 @@ else sflag_print(); } +#if defined(SOL_ON) + if (sol_format) { + sol_array_end(&sol_stream); + sol_term(&sol_stream); + } +#endif return (0); } Modified: soc2014/zkorchev/freebsd_head/usr.bin/finger/finger.h ============================================================================== --- soc2014/zkorchev/freebsd_head/usr.bin/finger/finger.h Mon Jul 21 09:48:13 2014 (r271187) +++ soc2014/zkorchev/freebsd_head/usr.bin/finger/finger.h Mon Jul 21 09:48:34 2014 (r271188) @@ -69,4 +69,9 @@ #include "extern.h" +#if defined(SOL_ON) +extern struct sol_stream sol_stream; +#endif +extern int sol_format; + #endif /* !_FINGER_H_ */ Modified: soc2014/zkorchev/freebsd_head/usr.bin/finger/sprint.c ============================================================================== --- soc2014/zkorchev/freebsd_head/usr.bin/finger/sprint.c Mon Jul 21 09:48:13 2014 (r271187) +++ soc2014/zkorchev/freebsd_head/usr.bin/finger/sprint.c Mon Jul 21 09:48:34 2014 (r271188) @@ -50,6 +50,9 @@ #include <string.h> #include <time.h> #include <utmpx.h> +#if defined(SOL_ON) +# include <sol.h> +#endif #include "finger.h" static void stimeprint(WHERE *); @@ -61,6 +64,7 @@ WHERE *w; int sflag, r, namelen; char p[80]; + size_t len; PERSON *tmp; DBT data, key; struct tm *lc; @@ -85,9 +89,12 @@ */ #define MAXREALNAME 16 #define MAXHOSTNAME 17 /* in reality, hosts are never longer than 16 */ - (void)printf("%-*s %-*s%s %s\n", MAXLOGNAME, "Login", MAXREALNAME, - "Name", " TTY Idle Login Time ", (gflag) ? "" : - oflag ? "Office Phone" : "Where"); +#if defined(SOL_ON) + if (!sol_format) +#endif + (void)printf("%-*s %-*s%s %s\n", MAXLOGNAME, "Login", MAXREALNAME, + "Name", " TTY Idle Login Time ", (gflag) ? "" : + oflag ? "Office Phone" : "Where"); for (sflag = R_FIRST;; sflag = R_NEXT) { r = (*db->seq)(db, &key, &data, sflag); @@ -99,62 +106,136 @@ pn = tmp; for (w = pn->whead; w != NULL; w = w->next) { - namelen = MAXREALNAME; - if (w->info == LOGGEDIN && !w->writable) - --namelen; /* leave space before `*' */ - (void)printf("%-*.*s %-*.*s", MAXLOGNAME, MAXLOGNAME, - pn->name, MAXREALNAME, namelen, - pn->realname ? pn->realname : ""); - if (!w->loginat) { - (void)printf(" * * No logins "); - goto office; - } - (void)putchar(w->info == LOGGEDIN && !w->writable ? - '*' : ' '); - if (*w->tty) - (void)printf("%-7.7s ", - (strncmp(w->tty, "tty", 3) - && strncmp(w->tty, "cua", 3)) - ? w->tty : w->tty + 3); +#if defined(SOL_ON) + if (sol_format) + { + char buf[1 + sizeof(((struct utmpx *)0)->ut_line)]; + + sol_map_start(&sol_stream); + SOL_MAP_KEYL(&sol_stream, "login"); + sol_string(&sol_stream, pn->name, strlen(pn->name)); + if (pn->realname) { + SOL_MAP_KEYL(&sol_stream, "name"); + sol_string(&sol_stream, pn->realname, strlen(pn->realname)); + } + if (!w->loginat) goto office; + *buf = (w->info == LOGGEDIN && !w->writable ? '*' : ' '); + if (*w->tty) + strcpy(buf + 1, w->tty + ((strncmp(w->tty, "tty", 3) + && strncmp(w->tty, "cua", 3)) ? 0 : 3)); + else + buf[1] = 0; + SOL_MAP_KEYL(&sol_stream, "tty"); + sol_string(&sol_stream, buf, strlen(buf)); + } else - (void)printf(" "); +#endif + { + namelen = MAXREALNAME; + if (w->info == LOGGEDIN && !w->writable) + --namelen; /* leave space before `*' */ + (void)printf("%-*.*s %-*.*s", MAXLOGNAME, MAXLOGNAME, + pn->name, MAXREALNAME, namelen, + pn->realname ? pn->realname : ""); + if (!w->loginat) { + (void)printf(" * * No logins "); + goto office; + } + (void)putchar(w->info == LOGGEDIN && !w->writable ? + '*' : ' '); + if (*w->tty) + (void)printf("%-7.7s ", + (strncmp(w->tty, "tty", 3) + && strncmp(w->tty, "cua", 3)) + ? w->tty : w->tty + 3); + else + (void)printf(" "); + } if (w->info == LOGGEDIN) { stimeprint(w); - (void)printf(" "); +#if defined(SOL_ON) + if (!sol_format) +#endif + (void)printf(" "); } else - (void)printf(" * "); +#if defined(SOL_ON) + if (!sol_format) +#endif + (void)printf(" * "); lc = localtime(&w->loginat); #define SECSPERDAY 86400 #define DAYSPERWEEK 7 #define DAYSPERNYEAR 365 if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1)) { - (void)strftime(p, sizeof(p), "%a", lc); + len = strftime(p, sizeof(p), "%a", lc); } else { - (void)strftime(p, sizeof(p), + len = strftime(p, sizeof(p), d_first ? "%e %b" : "%b %e", lc); } - (void)printf("%-6.6s", p); +#if defined(SOL_ON) + if (sol_format) { + SOL_MAP_KEYL(&sol_stream, "login"); + sol_string(&sol_stream, p, len); + } + else +#endif + (void)printf("%-6.6s", p); if (now - w->loginat >= SECSPERDAY * DAYSPERNYEAR / 2) { - (void)strftime(p, sizeof(p), "%Y", lc); + len = strftime(p, sizeof(p), "%Y", lc); } else { - (void)strftime(p, sizeof(p), "%R", lc); + len = strftime(p, sizeof(p), "%R", lc); + } +#if defined(SOL_ON) + if (sol_format) { + SOL_MAP_KEYL(&sol_stream, "time"); + sol_string(&sol_stream, p, len); } - (void)printf(" %-5.5s", p); + else +#endif + (void)printf(" %-5.5s", p); office: if (gflag) goto no_gecos; - if (oflag) { - if (pn->office) - (void)printf(" %-7.7s", pn->office); - else if (pn->officephone) - (void)printf(" %-7.7s", " "); - if (pn->officephone) - (void)printf(" %-.9s", - prphone(pn->officephone)); - } else - (void)printf(" %.*s", MAXHOSTNAME, w->host); +#if defined(SOL_ON) + if (sol_format) + { + if (oflag) { + if (pn->office) { + SOL_MAP_KEYL(&sol_stream, "office"); + sol_string(&sol_stream, pn->office, strlen(pn->office)); + } + if (pn->officephone) { + const char *phone = prphone(pn->officephone); + SOL_MAP_KEYL(&sol_stream, "phone"); + sol_string(&sol_stream, phone, strlen(phone)); + } + } + else { + SOL_MAP_KEYL(&sol_stream, "?host"); + sol_string(&sol_stream, w->host, strlen(w->host)); + } + } + else +#endif + { + if (oflag) { + if (pn->office) + (void)printf(" %-7.7s", pn->office); + else if (pn->officephone) + (void)printf(" %-7.7s", " "); + if (pn->officephone) + (void)printf(" %-.9s", + prphone(pn->officephone)); + } else + (void)printf(" %.*s", MAXHOSTNAME, w->host); + } no_gecos: - putchar('\n'); +#if defined(SOL_ON) + if (sol_format) + sol_map_end(&sol_stream); + else +#endif + putchar('\n'); } } } @@ -164,6 +245,28 @@ { struct tm *delta; +#if defined(SOL_ON) + if (sol_format) { + if (w->idletime == -1) return; + delta = gmtime(&w->idletime); + if (delta->tm_yday) { + SOL_MAP_KEYL(&sol_stream, "yday"); + sol_integer(&sol_stream, delta->tm_yday); + } + else { + if (delta->tm_hour) { + SOL_MAP_KEYL(&sol_stream, "hour"); + sol_integer(&sol_stream, delta->tm_hour); + } + if (delta->tm_min) { + SOL_MAP_KEYL(&sol_stream, "minute"); + sol_integer(&sol_stream, delta->tm_min); + } + } + return; + } +#endif + if (w->idletime == -1) { (void)printf(" "); return;
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201407210948.s6L9mYL9065245>