#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)sprint.c 8.3 (Berkeley) 4/28/95";
#else
__RCSID("$NetBSD: sprint.c,v 1.18 2015/11/21 15:22:17 christos Exp $");
#endif
#endif
#include <sys/param.h>
#include <sys/time.h>
#include <time.h>
#include <tzfile.h>
#include <db.h>
#include <err.h>
#include <pwd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utmpentry.h"
#include "finger.h"
#include "extern.h"
static void stimeprint(WHERE *);
void
sflag_print(void)
{
PERSON *pn;
WHERE *w;
int sflag, r;
char *p;
PERSON *tmp;
DBT data, key;
if (db == NULL)
return;
#define MAXREALNAME 18
(void)printf("%-*s %-*s %s %s\n", (int)maxname, "Login", MAXREALNAME,
"Name", " Tty Idle Login Time ", (gflag) ? "" :
(oflag) ? "Office Office Phone" : "Where");
for (sflag = R_FIRST;; sflag = R_NEXT) {
r = (*db->seq)(db, &key, &data, sflag);
if (r == -1)
err(1, "db seq");
if (r == 1)
break;
memmove(&tmp, data.data, sizeof tmp);
pn = tmp;
for (w = pn->whead; w != NULL; w = w->next) {
(void)printf("%-*.*s %-*.*s ", (int)maxname,
(int)maxname,
pn->name, MAXREALNAME, MAXREALNAME,
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 ", w->tty);
else
(void)printf(" ");
if (w->info == LOGGEDIN) {
stimeprint(w);
(void)printf(" ");
} else
(void)printf(" * ");
p = ctime(&w->loginat);
if (now - w->loginat < SECSPERDAY * (DAYSPERWEEK - 1))
(void)printf("%.3s %-8.5s", p, p + 11);
else if (now - w->loginat
< SECSPERDAY * DAYSPERNYEAR / 2)
(void)printf("%.6s %-5.5s", p + 4, p + 11);
else
(void)printf("%.6s %-5.4s", p + 4, p + 20);
office:
if (gflag)
goto no_gecos;
putchar(' ');
if (oflag) {
if (pn->office)
(void)printf("%-10.10s", pn->office);
else if (pn->officephone)
(void)printf("%-10.10s", " ");
if (pn->officephone)
(void)printf(" %-.15s",
prphone(pn->officephone));
} else
(void)printf("%.*s", MAXHOSTNAMELEN, w->host);
no_gecos:
putchar('\n');
}
}
}
static void
stimeprint(WHERE *w)
{
struct tm *delta;
delta = gmtime(&w->idletime);
if (!delta->tm_yday) {
if (!delta->tm_hour) {
if (!delta->tm_min)
(void)printf(" -");
else
(void)printf("%5d", delta->tm_min);
} else
(void)printf("%2d:%02d",
delta->tm_hour, delta->tm_min);
} else
(void)printf("%4dd", delta->tm_yday);
}