#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)lprint.c 8.3 (Berkeley) 4/28/95";
#else
__RCSID( "$NetBSD: lprint.c,v 1.23 2013/01/18 22:10:31 christos Exp $");
#endif
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <tzfile.h>
#include <db.h>
#include <err.h>
#include <pwd.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <paths.h>
#include <vis.h>
#include "utmpentry.h"
#include "finger.h"
#include "extern.h"
#define LINE_LEN 80
#define TAB_LEN 8
#define _PATH_FORWARD ".forward"
#define _PATH_PLAN ".plan"
#define _PATH_PROJECT ".project"
static int demi_print(const char *, int);
static void lprint(PERSON *);
static int show_text(const char *, const char *, const char *);
static void vputc(int);
#ifdef __SVR4
#define TIMEZONE(a) tzname[0]
#else
#define TIMEZONE(a) (a)->tm_zone
#endif
void
lflag_print(void)
{
PERSON *pn;
int sflag, r;
PERSON *tmp;
DBT data, key;
if (db == NULL)
return;
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;
if (sflag != R_FIRST)
putchar('\n');
lprint(pn);
if (!pplan) {
(void)show_text(pn->dir,
_PATH_FORWARD, "Mail forwarded to");
(void)show_text(pn->dir, _PATH_PROJECT, "Project");
if (!show_text(pn->dir, _PATH_PLAN, "Plan"))
(void)printf("No Plan.\n");
}
}
}
static size_t
visify(char *buf, size_t blen, const char *str)
{
int len = strnvisx(buf, blen, str, strlen(str), VIS_WHITE|VIS_CSTYLE);
if (len == -1) {
buf[0] = '\0';
return 0;
}
return len;
}
static void
fmt_time(char *buf, size_t blen, time_t ti, time_t n)
{
struct tm *tp = localtime(&ti);
if (tp != NULL) {
char *t = asctime(tp);
char *tzn = TIMEZONE(tp);
if (n == (time_t)-1 ||
n - ti > SECSPERDAY * DAYSPERNYEAR / 2)
snprintf(buf, blen, "%.16s %.4s (%s)", t, t + 20, tzn);
else
snprintf(buf, blen, "%.16s (%s)", t, tzn);
} else
snprintf(buf, blen, "[*bad time 0x%llx*]", (long long)ti);
}
static int
print_idle(time_t t, int maxlen, size_t hostlen, size_t ttylen) {
int cpr;
struct tm *delta = gmtime(&t);
if (delta == NULL)
return printf("Bad idle 0x%llx", (long long)t);
if (delta->tm_yday == 0 && delta->tm_hour == 0 && delta->tm_min == 0)
return 0;
cpr = printf("%-*s idle ", (int)(maxlen - ttylen + 1), ",");
if (delta->tm_yday > 0) {
cpr += printf("%d day%s ", delta->tm_yday,
delta->tm_yday == 1 ? "" : "s");
}
cpr += printf("%d:%02d", delta->tm_hour, delta->tm_min);
if (hostlen) {
putchar(',');
++cpr;
}
return cpr;
}
static void
lprint(PERSON *pn)
{
WHERE *w;
int cpr, len, maxlen;
int oddfield;
char timebuf[128], ttybuf[64], hostbuf[512];
size_t ttylen, hostlen;
cpr = 0;
(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
pn->name, pn->realname, pn->dir);
(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
if (gflag)
goto no_gecos;
#define OFFICE_TAG "Office"
#define OFFICE_PHONE_TAG "Office Phone"
oddfield = 0;
if (pn->office && pn->officephone &&
strlen(pn->office) + strlen(pn->officephone) +
sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
(void)snprintf(timebuf, sizeof(timebuf), "%s: %s, %s",
OFFICE_TAG, pn->office, prphone(pn->officephone));
oddfield = demi_print(timebuf, oddfield);
} else {
if (pn->office) {
(void)snprintf(timebuf, sizeof(timebuf), "%s: %s",
OFFICE_TAG, pn->office);
oddfield = demi_print(timebuf, oddfield);
}
if (pn->officephone) {
(void)snprintf(timebuf, sizeof(timebuf), "%s: %s",
OFFICE_PHONE_TAG, prphone(pn->officephone));
oddfield = demi_print(timebuf, oddfield);
}
}
if (pn->homephone) {
(void)snprintf(timebuf, sizeof(timebuf), "%s: %s", "Home Phone",
prphone(pn->homephone));
oddfield = demi_print(timebuf, oddfield);
}
if (oddfield)
putchar('\n');
no_gecos:
for (w = pn->whead, maxlen = -1; w != NULL; w = w->next) {
visify(ttybuf, sizeof(ttybuf), w->tty);
if ((len = strlen(ttybuf)) > maxlen)
maxlen = len;
}
for (w = pn->whead; w != NULL; w = w->next) {
ttylen = visify(ttybuf, sizeof(ttybuf), w->tty);
hostlen = visify(hostbuf, sizeof(hostbuf), w->host);
switch (w->info) {
case LOGGEDIN:
fmt_time(timebuf, sizeof(timebuf), w->loginat, -1);
cpr = printf("On since %s on %s", timebuf, ttybuf);
cpr += print_idle(w->idletime, maxlen, hostlen,
ttylen);
if (!w->writable)
cpr += printf(" (messages off)");
break;
case LASTLOG:
if (w->loginat == 0) {
(void)printf("Never logged in.");
break;
}
fmt_time(timebuf, sizeof(timebuf), w->loginat, now);
cpr = printf("Last login %s on %s", timebuf, ttybuf);
break;
}
if (hostlen) {
if (LINE_LEN < (cpr + 6 + hostlen))
(void)printf("\n ");
(void)printf(" from %s", hostbuf);
}
putchar('\n');
}
if (pn->mailrecv == -1)
printf("No Mail.\n");
else if (pn->mailrecv > pn->mailread) {
fmt_time(timebuf, sizeof(timebuf), pn->mailrecv, -1);
printf("New mail received %s\n", timebuf);
fmt_time(timebuf, sizeof(timebuf), pn->mailread, -1);
printf(" Unread since %s\n", timebuf);
} else {
fmt_time(timebuf, sizeof(timebuf), pn->mailread, -1);
printf("Mail last read %s\n", timebuf);
}
}
static int
demi_print(const char *str, int oddfield)
{
static int lenlast;
int lenthis, maxlen;
lenthis = strlen(str);
if (oddfield) {
maxlen = 5 * TAB_LEN;
if (maxlen < lenlast)
maxlen = lenlast;
if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
lenthis) <= LINE_LEN) {
while(lenlast < (4 * TAB_LEN)) {
putchar('\t');
lenlast += TAB_LEN;
}
(void)printf("\t%s\n", str);
} else {
(void)printf("\n%s", str);
oddfield = !oddfield;
}
} else
(void)printf("%s", str);
oddfield = !oddfield;
lenlast = lenthis;
return(oddfield);
}
static int
show_text(const char *directory, const char *file_name, const char *header)
{
struct stat sb;
FILE *fp;
int ch, cnt, lastc;
char *p;
int fd, nr;
lastc = 0;
(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
sb.st_size == 0)
return(0);
if (sb.st_size <= (off_t)(LINE_LEN - strlen(header) - 5)) {
nr = read(fd, tbuf, sizeof(tbuf));
if (nr <= 0) {
(void)close(fd);
return(0);
}
for (p = tbuf, cnt = nr; cnt--; ++p)
if (*p == '\n')
break;
if (cnt <= 1) {
(void)printf("%s: ", header);
for (p = tbuf, cnt = nr; cnt--; ++p)
vputc(lastc = (unsigned char)*p);
if (lastc != '\n')
(void)putchar('\n');
(void)close(fd);
return(1);
}
else
(void)lseek(fd, 0L, SEEK_SET);
}
if ((fp = fdopen(fd, "r")) == NULL)
return(0);
(void)printf("%s:\n", header);
while ((ch = getc(fp)) != EOF)
vputc(lastc = ch);
if (lastc != '\n')
(void)putchar('\n');
(void)fclose(fp);
return(1);
}
static void
vputc(int ch)
{
char visout[5], *s2;
if (eightflag || isprint(ch) || isspace(ch)) {
(void)putchar(ch);
return;
}
ch = toascii(ch);
vis(visout, ch, VIS_SAFE|VIS_NOSLASH, 0);
for (s2 = visout; *s2; s2++)
(void)putchar(*s2);
}