NetBSD/usr.bin/finger/lprint.c

372 lines
9.5 KiB
C
Raw Normal View History

/* $NetBSD: lprint.c,v 1.19 2003/08/07 11:13:45 agc Exp $ */
1997-01-09 23:18:21 +03:00
1993-03-21 12:45:37 +03:00
/*
1997-10-19 12:13:23 +04:00
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* This code is derived from software contributed to Berkeley by
* Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
1993-03-21 12:45:37 +03:00
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
1997-10-19 12:13:23 +04:00
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
1997-10-19 12:13:23 +04:00
#if 0
static char sccsid[] = "@(#)lprint.c 8.3 (Berkeley) 4/28/95";
#else
__RCSID( "$NetBSD: lprint.c,v 1.19 2003/08/07 11:13:45 agc Exp $");
1997-10-19 12:13:23 +04:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
1997-10-19 12:13:23 +04:00
#include <fcntl.h>
#include <time.h>
1993-03-21 12:45:37 +03:00
#include <tzfile.h>
1997-10-19 12:13:23 +04:00
#include <db.h>
#include <err.h>
#include <pwd.h>
#include <errno.h>
#include <unistd.h>
1993-03-21 12:45:37 +03:00
#include <stdio.h>
#include <string.h>
#include <time.h>
1993-03-21 12:45:37 +03:00
#include <ctype.h>
1997-10-19 12:13:23 +04:00
#include <string.h>
1993-03-21 12:45:37 +03:00
#include <paths.h>
#include <vis.h>
1997-10-19 12:13:23 +04:00
#include "utmpentry.h"
1993-03-21 12:45:37 +03:00
#include "finger.h"
#include "extern.h"
1993-03-21 12:45:37 +03:00
#define LINE_LEN 80
#define TAB_LEN 8 /* 8 spaces between tabs */
1997-10-19 12:13:23 +04:00
#define _PATH_FORWARD ".forward"
1993-03-21 12:45:37 +03:00
#define _PATH_PLAN ".plan"
#define _PATH_PROJECT ".project"
1997-10-19 12:13:23 +04:00
static int demi_print __P((char *, int));
static void lprint __P((PERSON *));
static int show_text __P((char *, char *, char *));
static void vputc __P((int));
1998-12-19 19:00:33 +03:00
#ifdef __SVR4
#define TIMEZONE(a) tzname[0]
#else
#define TIMEZONE(a) (a)->tm_zone
#endif
void
1993-03-21 12:45:37 +03:00
lflag_print()
{
PERSON *pn;
1997-10-19 12:13:23 +04:00
int sflag, r;
PERSON *tmp;
DBT data, key;
1993-03-21 12:45:37 +03:00
if (db == NULL)
return;
1997-10-19 12:13:23 +04:00
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');
1993-03-21 12:45:37 +03:00
lprint(pn);
if (!pplan) {
1997-10-19 12:13:23 +04:00
(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"))
1993-03-21 12:45:37 +03:00
(void)printf("No Plan.\n");
}
}
}
1997-10-19 12:13:23 +04:00
static void
1993-03-21 12:45:37 +03:00
lprint(pn)
PERSON *pn;
1993-03-21 12:45:37 +03:00
{
struct tm *delta;
WHERE *w;
int cpr, len, maxlen;
1993-03-21 12:45:37 +03:00
struct tm *tp;
int oddfield;
1998-07-26 19:13:14 +04:00
char *t;
const char *tzn;
1993-03-21 12:45:37 +03:00
cpr = 0;
1993-03-21 12:45:37 +03:00
/*
* long format --
* login name
* real name
* home directory
* shell
* office, office phone, home phone if available
* mail status
1993-03-21 12:45:37 +03:00
*/
(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;
1993-03-21 12:45:37 +03:00
/*
* try and print office, office phone, and home phone on one line;
* if that fails, do line filling so it looks nice.
*/
#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) {
1997-10-19 12:13:23 +04:00
(void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s",
OFFICE_TAG, pn->office, prphone(pn->officephone));
1993-03-21 12:45:37 +03:00
oddfield = demi_print(tbuf, oddfield);
} else {
if (pn->office) {
1997-10-19 12:13:23 +04:00
(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
OFFICE_TAG, pn->office);
1993-03-21 12:45:37 +03:00
oddfield = demi_print(tbuf, oddfield);
}
if (pn->officephone) {
1997-10-19 12:13:23 +04:00
(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
OFFICE_PHONE_TAG, prphone(pn->officephone));
1993-03-21 12:45:37 +03:00
oddfield = demi_print(tbuf, oddfield);
}
}
if (pn->homephone) {
(void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
1993-03-21 12:45:37 +03:00
prphone(pn->homephone));
oddfield = demi_print(tbuf, oddfield);
}
if (oddfield)
putchar('\n');
no_gecos:
1993-03-21 12:45:37 +03:00
/*
* long format con't:
* if logged in
1993-03-21 12:45:37 +03:00
* terminal
* idle time
* if messages allowed
* where logged in from
* if not logged in
* when last logged in
*/
/* find out longest device name for this user for formatting */
for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
if ((len = strlen(w->tty)) > maxlen)
maxlen = len;
/* find rest of entries for user */
for (w = pn->whead; w != NULL; w = w->next) {
switch (w->info) {
case LOGGEDIN:
tp = localtime(&w->loginat);
t = asctime(tp);
1998-12-19 19:00:33 +03:00
tzn = TIMEZONE(tp);
1993-03-21 12:45:37 +03:00
cpr = printf("On since %.16s (%s) on %s",
t, tzn, w->tty);
/*
* idle time is tough; if have one, print a comma,
* then spaces to pad out the device name, then the
* idle time. Follow with a comma if a remote login.
*/
delta = gmtime(&w->idletime);
if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
cpr += printf("%-*s idle ",
1997-10-19 18:05:56 +04:00
(int)(maxlen - strlen(w->tty) + 1), ",");
1993-03-21 12:45:37 +03:00
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 (*w->host) {
putchar(',');
++cpr;
}
}
if (!w->writable)
cpr += printf(" (messages off)");
break;
case LASTLOG:
if (w->loginat == 0) {
(void)printf("Never logged in.");
break;
}
tp = localtime(&w->loginat);
t = asctime(tp);
1998-12-19 19:00:33 +03:00
tzn = TIMEZONE(tp);
1993-03-21 12:45:37 +03:00
if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
cpr =
printf("Last login %.16s %.4s (%s) on %s",
t, t + 20, tzn, w->tty);
else
cpr = printf("Last login %.16s (%s) on %s",
t, tzn, w->tty);
break;
}
if (*w->host) {
if (LINE_LEN < (cpr + 6 + strlen(w->host)))
(void)printf("\n ");
(void)printf(" from %s", w->host);
}
putchar('\n');
}
if (pn->mailrecv == -1)
printf("No Mail.\n");
else if (pn->mailrecv > pn->mailread) {
tp = localtime(&pn->mailrecv);
t = asctime(tp);
1998-12-19 19:00:33 +03:00
tzn = TIMEZONE(tp);
printf("New mail received %.16s %.4s (%s)\n", t, t + 20, tzn);
tp = localtime(&pn->mailread);
t = asctime(tp);
1998-12-19 19:00:33 +03:00
tzn = TIMEZONE(tp);
printf(" Unread since %.16s %.4s (%s)\n", t, t + 20, tzn);
} else {
tp = localtime(&pn->mailread);
t = asctime(tp);
1998-12-19 19:00:33 +03:00
tzn = TIMEZONE(tp);
printf("Mail last read %.16s %.4s (%s)\n", t, t + 20, tzn);
}
1993-03-21 12:45:37 +03:00
}
1997-10-19 12:13:23 +04:00
static int
1993-03-21 12:45:37 +03:00
demi_print(str, oddfield)
char *str;
int oddfield;
{
static int lenlast;
int lenthis, maxlen;
lenthis = strlen(str);
if (oddfield) {
/*
* We left off on an odd number of fields. If we haven't
* crossed the midpoint of the screen, and we have room for
* the next field, print it on the same line; otherwise,
* print it on a new line.
*
* Note: we insist on having the right hand fields start
* no less than 5 tabs out.
*/
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); /* force one tab */
} else {
(void)printf("\n%s", str); /* go to next line */
oddfield = !oddfield; /* this'll be undone below */
}
} else
(void)printf("%s", str);
oddfield = !oddfield; /* toggle odd/even marker */
lenlast = lenthis;
return(oddfield);
}
1997-10-19 12:13:23 +04:00
static int
1993-03-21 12:45:37 +03:00
show_text(directory, file_name, header)
char *directory, *file_name, *header;
{
1997-10-19 12:13:23 +04:00
struct stat sb;
FILE *fp;
1997-10-19 12:13:23 +04:00
int ch, cnt, lastc;
char *p;
int fd, nr;
1993-03-21 12:45:37 +03:00
lastc = 0;
(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
1997-10-19 12:13:23 +04:00
if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
sb.st_size == 0)
1993-03-21 12:45:37 +03:00
return(0);
1997-10-19 12:13:23 +04:00
/* If short enough, and no newlines, show it on a single line.*/
if (sb.st_size <= 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);
1997-10-19 12:13:23 +04:00
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);
1993-03-21 12:45:37 +03:00
while ((ch = getc(fp)) != EOF)
vputc(lastc = ch);
if (lastc != '\n')
(void)putchar('\n');
(void)fclose(fp);
return(1);
}
1997-10-19 12:13:23 +04:00
static void
1993-03-21 12:45:37 +03:00
vputc(ch)
int ch;
1993-03-21 12:45:37 +03:00
{
char visout[5], *s2;
1993-03-21 12:45:37 +03:00
2002-09-12 05:31:41 +04:00
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);
1993-03-21 12:45:37 +03:00
}