freebsd-nq/usr.bin/finger/lprint.c

368 lines
9.7 KiB
C
Raw Normal View History

1994-05-27 12:33:43 +00:00
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* 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.
*/
#if 0
1994-05-27 12:33:43 +00:00
#ifndef lint
static char sccsid[] = "@(#)lprint.c 8.3 (Berkeley) 4/28/95";
2001-02-06 20:13:48 +00:00
#endif
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
1994-05-27 12:33:43 +00:00
#include <sys/types.h>
#include <sys/socket.h>
1994-05-27 12:33:43 +00:00
#include <sys/stat.h>
#include <sys/time.h>
2001-02-06 20:13:48 +00:00
#include <ctype.h>
1994-05-27 12:33:43 +00:00
#include <db.h>
1997-03-11 13:43:33 +00:00
#include <err.h>
2001-02-06 20:13:48 +00:00
#include <fcntl.h>
2001-03-21 18:43:49 +00:00
#include <langinfo.h>
2001-02-06 20:13:48 +00:00
#include <paths.h>
1994-05-27 12:33:43 +00:00
#include <pwd.h>
#include <stdio.h>
#include <string.h>
2001-02-06 20:13:48 +00:00
#include <unistd.h>
#include <utmpx.h>
1994-05-27 12:33:43 +00:00
#include "finger.h"
#include "pathnames.h"
1994-05-27 12:33:43 +00:00
#define LINE_LEN 80
#define TAB_LEN 8 /* 8 spaces between tabs */
2002-03-22 01:22:50 +00:00
static int demi_print(char *, int);
static void lprint(PERSON *);
static void vputc(unsigned char);
1994-05-27 12:33:43 +00:00
void
lflag_print(void)
1994-05-27 12:33:43 +00:00
{
PERSON *pn;
int sflag, r;
1997-03-11 13:43:33 +00:00
PERSON *tmp;
1994-05-27 12:33:43 +00:00
DBT data, key;
for (sflag = R_FIRST;; sflag = R_NEXT) {
r = (*db->seq)(db, &key, &data, sflag);
if (r == -1)
1997-03-11 13:43:33 +00:00
err(1, "db seq");
1994-05-27 12:33:43 +00:00
if (r == 1)
break;
1997-03-11 13:43:33 +00:00
memmove(&tmp, data.data, sizeof tmp);
pn = tmp;
1994-05-27 12:33:43 +00:00
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");
(void)show_text(pn->dir,
_PATH_PUBKEY, "Public key");
1994-05-27 12:33:43 +00:00
}
}
}
static void
lprint(PERSON *pn)
1994-05-27 12:33:43 +00:00
{
struct tm *delta;
WHERE *w;
int cpr, len, maxlen;
1994-05-27 12:33:43 +00:00
struct tm *tp;
int oddfield;
char t[80];
1994-05-27 12:33:43 +00:00
2001-03-21 18:43:49 +00:00
if (d_first < 0)
d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
1994-05-27 12:33:43 +00:00
/*
* long format --
* login name
* real name
* home directory
* shell
* office, office phone, home phone if available
* mail status
1994-05-27 12:33:43 +00: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;
1994-05-27 12:33:43 +00: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) {
(void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s",
OFFICE_TAG, pn->office, prphone(pn->officephone));
oddfield = demi_print(tbuf, oddfield);
} else {
if (pn->office) {
(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
OFFICE_TAG, pn->office);
oddfield = demi_print(tbuf, oddfield);
}
if (pn->officephone) {
(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
OFFICE_PHONE_TAG, prphone(pn->officephone));
oddfield = demi_print(tbuf, oddfield);
}
}
if (pn->homephone) {
(void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
prphone(pn->homephone));
oddfield = demi_print(tbuf, oddfield);
}
if (oddfield)
putchar('\n');
no_gecos:
1994-05-27 12:33:43 +00:00
/*
* long format con't:
* if logged in
1994-05-27 12:33:43 +00: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) {
2000-10-23 12:18:34 +00:00
if (w->info == LOGGEDIN) {
1994-05-27 12:33:43 +00:00
tp = localtime(&w->loginat);
2001-03-21 18:43:49 +00:00
strftime(t, sizeof(t),
d_first ? "%a %e %b %R (%Z)" : "%a %b %e %R (%Z)",
tp);
cpr = printf("On since %s on %s", t, w->tty);
1994-05-27 12:33:43 +00:00
/*
* 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 (w->idletime != -1 && (delta->tm_yday ||
delta->tm_hour || delta->tm_min)) {
1994-05-27 12:33:43 +00:00
cpr += printf("%-*s idle ",
maxlen - (int)strlen(w->tty) + 1, ",");
1994-05-27 12:33:43 +00: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)");
2000-10-23 12:18:34 +00:00
} else if (w->loginat == 0) {
cpr = printf("Never logged in.");
} else {
1994-05-27 12:33:43 +00:00
tp = localtime(&w->loginat);
2001-03-21 18:43:49 +00:00
if (now - w->loginat > 86400 * 365 / 2) {
strftime(t, sizeof(t),
d_first ? "%a %e %b %R %Y (%Z)" :
"%a %b %e %R %Y (%Z)",
tp);
} else {
strftime(t, sizeof(t),
d_first ? "%a %e %b %R (%Z)" :
"%a %b %e %R (%Z)",
tp);
}
cpr = printf("Last login %s on %s", t, w->tty);
1994-05-27 12:33:43 +00:00
}
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);
2001-03-21 18:43:49 +00:00
strftime(t, sizeof(t),
d_first ? "%a %e %b %R %Y (%Z)" :
"%a %b %e %R %Y (%Z)",
tp);
printf("New mail received %s\n", t);
tp = localtime(&pn->mailread);
2001-03-21 18:43:49 +00:00
strftime(t, sizeof(t),
d_first ? "%a %e %b %R %Y (%Z)" :
"%a %b %e %R %Y (%Z)",
tp);
printf(" Unread since %s\n", t);
} else {
tp = localtime(&pn->mailread);
2001-03-21 18:43:49 +00:00
strftime(t, sizeof(t),
d_first ? "%a %e %b %R %Y (%Z)" :
"%a %b %e %R %Y (%Z)",
tp);
printf("Mail last read %s\n", t);
}
1994-05-27 12:33:43 +00:00
}
static int
demi_print(char *str, int oddfield)
1994-05-27 12:33:43 +00:00
{
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);
}
int
show_text(const char *directory, const char *file_name, const char *header)
1994-05-27 12:33:43 +00:00
{
struct stat sb;
FILE *fp;
int ch, cnt;
char *p, lastc;
1994-05-27 12:33:43 +00:00
int fd, nr;
2000-10-23 12:18:34 +00:00
lastc = '\0';
1994-05-27 12:33:43 +00:00
(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 short enough, and no newlines, show it on a single line.*/
if (sb.st_size <= LINE_LEN - strlen(header) - 5) {
1994-05-27 12:33:43 +00:00
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) {
if (*header != '\0')
(void)printf("%s: ", header);
1994-05-27 12:33:43 +00:00
for (p = tbuf, cnt = nr; cnt--; ++p)
if (*p != '\r')
vputc(lastc = *p);
1994-05-27 12:33:43 +00: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);
if (*header != '\0')
(void)printf("%s:\n", header);
1994-05-27 12:33:43 +00:00
while ((ch = getc(fp)) != EOF)
if (ch != '\r')
vputc(lastc = ch);
1994-05-27 12:33:43 +00:00
if (lastc != '\n')
(void)putchar('\n');
(void)fclose(fp);
return(1);
}
static void
vputc(unsigned char ch)
1994-05-27 12:33:43 +00:00
{
int meta;
if (!isprint(ch) && !isascii(ch)) {
1994-05-27 12:33:43 +00:00
(void)putchar('M');
(void)putchar('-');
ch = toascii(ch);
meta = 1;
} else
meta = 0;
2000-10-23 12:18:34 +00:00
if (isprint(ch) || (!meta && (ch == ' ' || ch == '\t' || ch == '\n')))
1994-05-27 12:33:43 +00:00
(void)putchar(ch);
else {
(void)putchar('^');
(void)putchar(ch == '\177' ? '?' : ch | 0100);
}
}