lprint.c revision 102944
11590Srgrimes/* 21590Srgrimes * Copyright (c) 1989, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * This code is derived from software contributed to Berkeley by 61590Srgrimes * Tony Nardo of the Johns Hopkins University/Applied Physics Lab. 71590Srgrimes * 81590Srgrimes * Redistribution and use in source and binary forms, with or without 91590Srgrimes * modification, are permitted provided that the following conditions 101590Srgrimes * are met: 111590Srgrimes * 1. Redistributions of source code must retain the above copyright 121590Srgrimes * notice, this list of conditions and the following disclaimer. 131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141590Srgrimes * notice, this list of conditions and the following disclaimer in the 151590Srgrimes * documentation and/or other materials provided with the distribution. 161590Srgrimes * 3. All advertising materials mentioning features or use of this software 171590Srgrimes * must display the following acknowledgement: 181590Srgrimes * This product includes software developed by the University of 191590Srgrimes * California, Berkeley and its contributors. 201590Srgrimes * 4. Neither the name of the University nor the names of its contributors 211590Srgrimes * may be used to endorse or promote products derived from this software 221590Srgrimes * without specific prior written permission. 231590Srgrimes * 241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341590Srgrimes * SUCH DAMAGE. 351590Srgrimes */ 361590Srgrimes 3787628Sdwmalone#if 0 3887628Sdwmalone#ifndef lint 3987628Sdwmalonestatic char sccsid[] = "@(#)lprint.c 8.3 (Berkeley) 4/28/95"; 4087628Sdwmalone#endif 4187628Sdwmalone#endif 4287628Sdwmalone 4387229Smarkm#include <sys/cdefs.h> 4487229Smarkm__FBSDID("$FreeBSD: head/usr.bin/finger/lprint.c 102944 2002-09-04 23:29:10Z dwmalone $"); 4587229Smarkm 4691226Sbde#include <sys/types.h> 47102944Sdwmalone#include <sys/socket.h> 481590Srgrimes#include <sys/stat.h> 4991226Sbde#include <sys/time.h> 5072109Scharnier#include <ctype.h> 511590Srgrimes#include <db.h> 5223693Speter#include <err.h> 5372109Scharnier#include <fcntl.h> 5474586Sache#include <langinfo.h> 5572109Scharnier#include <paths.h> 561590Srgrimes#include <pwd.h> 571590Srgrimes#include <stdio.h> 581590Srgrimes#include <string.h> 5972109Scharnier#include <unistd.h> 6072109Scharnier#include <utmp.h> 611590Srgrimes#include "finger.h" 6265064Sbrian#include "pathnames.h" 6365064Sbrian#include "extern.h" 641590Srgrimes 651590Srgrimes#define LINE_LEN 80 661590Srgrimes#define TAB_LEN 8 /* 8 spaces between tabs */ 671590Srgrimes 6892920Simpstatic int demi_print(char *, int); 6992920Simpstatic void lprint(PERSON *); 7092920Simpstatic void vputc(unsigned char); 711590Srgrimes 721590Srgrimesvoid 73102944Sdwmalonelflag_print(void) 741590Srgrimes{ 7587229Smarkm PERSON *pn; 7687229Smarkm int sflag, r; 7723693Speter PERSON *tmp; 781590Srgrimes DBT data, key; 791590Srgrimes 801590Srgrimes for (sflag = R_FIRST;; sflag = R_NEXT) { 811590Srgrimes r = (*db->seq)(db, &key, &data, sflag); 821590Srgrimes if (r == -1) 8323693Speter err(1, "db seq"); 841590Srgrimes if (r == 1) 851590Srgrimes break; 8623693Speter memmove(&tmp, data.data, sizeof tmp); 8723693Speter pn = tmp; 881590Srgrimes if (sflag != R_FIRST) 891590Srgrimes putchar('\n'); 901590Srgrimes lprint(pn); 911590Srgrimes if (!pplan) { 921590Srgrimes (void)show_text(pn->dir, 931590Srgrimes _PATH_FORWARD, "Mail forwarded to"); 941590Srgrimes (void)show_text(pn->dir, _PATH_PROJECT, "Project"); 951590Srgrimes if (!show_text(pn->dir, _PATH_PLAN, "Plan")) 961590Srgrimes (void)printf("No Plan.\n"); 9770474Sdes (void)show_text(pn->dir, 9870655Sdes _PATH_PUBKEY, "Public key"); 991590Srgrimes } 1001590Srgrimes } 1011590Srgrimes} 1021590Srgrimes 1031590Srgrimesstatic void 104102944Sdwmalonelprint(PERSON *pn) 1051590Srgrimes{ 10687229Smarkm struct tm *delta; 10787229Smarkm WHERE *w; 10887229Smarkm int cpr, len, maxlen; 1091590Srgrimes struct tm *tp; 1101590Srgrimes int oddfield; 1119993Sache char t[80]; 1121590Srgrimes 11374586Sache if (d_first < 0) 11474586Sache d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 1151590Srgrimes /* 1161590Srgrimes * long format -- 1171590Srgrimes * login name 1181590Srgrimes * real name 1191590Srgrimes * home directory 1201590Srgrimes * shell 1211590Srgrimes * office, office phone, home phone if available 1222537Spst * mail status 1231590Srgrimes */ 1241590Srgrimes (void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s", 1251590Srgrimes pn->name, pn->realname, pn->dir); 1261590Srgrimes (void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL); 1271590Srgrimes 12899249Smini if (gflag) 12999249Smini goto no_gecos; 1301590Srgrimes /* 1311590Srgrimes * try and print office, office phone, and home phone on one line; 1321590Srgrimes * if that fails, do line filling so it looks nice. 1331590Srgrimes */ 1341590Srgrimes#define OFFICE_TAG "Office" 1351590Srgrimes#define OFFICE_PHONE_TAG "Office Phone" 1361590Srgrimes oddfield = 0; 1371590Srgrimes if (pn->office && pn->officephone && 1381590Srgrimes strlen(pn->office) + strlen(pn->officephone) + 1391590Srgrimes sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) { 1401590Srgrimes (void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s", 1411590Srgrimes OFFICE_TAG, pn->office, prphone(pn->officephone)); 1421590Srgrimes oddfield = demi_print(tbuf, oddfield); 1431590Srgrimes } else { 1441590Srgrimes if (pn->office) { 1451590Srgrimes (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", 1461590Srgrimes OFFICE_TAG, pn->office); 1471590Srgrimes oddfield = demi_print(tbuf, oddfield); 1481590Srgrimes } 1491590Srgrimes if (pn->officephone) { 1501590Srgrimes (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", 1511590Srgrimes OFFICE_PHONE_TAG, prphone(pn->officephone)); 1521590Srgrimes oddfield = demi_print(tbuf, oddfield); 1531590Srgrimes } 1541590Srgrimes } 1551590Srgrimes if (pn->homephone) { 1561590Srgrimes (void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone", 1571590Srgrimes prphone(pn->homephone)); 1581590Srgrimes oddfield = demi_print(tbuf, oddfield); 1591590Srgrimes } 1601590Srgrimes if (oddfield) 1611590Srgrimes putchar('\n'); 1621590Srgrimes 16399249Sminino_gecos: 1641590Srgrimes /* 1652537Spst * long format con't: 1662537Spst * if logged in 1671590Srgrimes * terminal 1681590Srgrimes * idle time 1691590Srgrimes * if messages allowed 1701590Srgrimes * where logged in from 1711590Srgrimes * if not logged in 1721590Srgrimes * when last logged in 1731590Srgrimes */ 1741590Srgrimes /* find out longest device name for this user for formatting */ 1751590Srgrimes for (w = pn->whead, maxlen = -1; w != NULL; w = w->next) 1761590Srgrimes if ((len = strlen(w->tty)) > maxlen) 1771590Srgrimes maxlen = len; 1781590Srgrimes /* find rest of entries for user */ 1791590Srgrimes for (w = pn->whead; w != NULL; w = w->next) { 18067467Sru if (w->info == LOGGEDIN) { 1811590Srgrimes tp = localtime(&w->loginat); 18274586Sache strftime(t, sizeof(t), 18374586Sache d_first ? "%a %e %b %R (%Z)" : "%a %b %e %R (%Z)", 18474586Sache tp); 18574586Sache cpr = printf("On since %s on %s", t, w->tty); 1861590Srgrimes /* 1871590Srgrimes * idle time is tough; if have one, print a comma, 1881590Srgrimes * then spaces to pad out the device name, then the 1891590Srgrimes * idle time. Follow with a comma if a remote login. 1901590Srgrimes */ 1911590Srgrimes delta = gmtime(&w->idletime); 1921590Srgrimes if (delta->tm_yday || delta->tm_hour || delta->tm_min) { 1931590Srgrimes cpr += printf("%-*s idle ", 19490390Speter maxlen - (int)strlen(w->tty) + 1, ","); 1951590Srgrimes if (delta->tm_yday > 0) { 1961590Srgrimes cpr += printf("%d day%s ", 1971590Srgrimes delta->tm_yday, 1981590Srgrimes delta->tm_yday == 1 ? "" : "s"); 1991590Srgrimes } 2001590Srgrimes cpr += printf("%d:%02d", 2011590Srgrimes delta->tm_hour, delta->tm_min); 2021590Srgrimes if (*w->host) { 2031590Srgrimes putchar(','); 2041590Srgrimes ++cpr; 2051590Srgrimes } 2061590Srgrimes } 2071590Srgrimes if (!w->writable) 2081590Srgrimes cpr += printf(" (messages off)"); 20967467Sru } else if (w->loginat == 0) { 21067467Sru cpr = printf("Never logged in."); 21167467Sru } else { 2121590Srgrimes tp = localtime(&w->loginat); 21374586Sache if (now - w->loginat > 86400 * 365 / 2) { 21474586Sache strftime(t, sizeof(t), 21574586Sache d_first ? "%a %e %b %R %Y (%Z)" : 21674586Sache "%a %b %e %R %Y (%Z)", 21774586Sache tp); 21874586Sache } else { 21974586Sache strftime(t, sizeof(t), 22074586Sache d_first ? "%a %e %b %R (%Z)" : 22174586Sache "%a %b %e %R (%Z)", 22274586Sache tp); 22374586Sache } 22474586Sache cpr = printf("Last login %s on %s", t, w->tty); 2251590Srgrimes } 2261590Srgrimes if (*w->host) { 2271590Srgrimes if (LINE_LEN < (cpr + 6 + strlen(w->host))) 2281590Srgrimes (void)printf("\n "); 2291590Srgrimes (void)printf(" from %s", w->host); 2301590Srgrimes } 2311590Srgrimes putchar('\n'); 2321590Srgrimes } 2332537Spst if (pn->mailrecv == -1) 2342537Spst printf("No Mail.\n"); 2352537Spst else if (pn->mailrecv > pn->mailread) { 2362537Spst tp = localtime(&pn->mailrecv); 23774586Sache strftime(t, sizeof(t), 23874586Sache d_first ? "%a %e %b %R %Y (%Z)" : 23974586Sache "%a %b %e %R %Y (%Z)", 24074586Sache tp); 24174586Sache printf("New mail received %s\n", t); 2422537Spst tp = localtime(&pn->mailread); 24374586Sache strftime(t, sizeof(t), 24474586Sache d_first ? "%a %e %b %R %Y (%Z)" : 24574586Sache "%a %b %e %R %Y (%Z)", 24674586Sache tp); 24774586Sache printf(" Unread since %s\n", t); 2482537Spst } else { 2492537Spst tp = localtime(&pn->mailread); 25074586Sache strftime(t, sizeof(t), 25174586Sache d_first ? "%a %e %b %R %Y (%Z)" : 25274586Sache "%a %b %e %R %Y (%Z)", 25374586Sache tp); 25474586Sache printf("Mail last read %s\n", t); 2552537Spst } 2561590Srgrimes} 2571590Srgrimes 2581590Srgrimesstatic int 259102944Sdwmalonedemi_print(char *str, int oddfield) 2601590Srgrimes{ 2611590Srgrimes static int lenlast; 2621590Srgrimes int lenthis, maxlen; 2631590Srgrimes 2641590Srgrimes lenthis = strlen(str); 2651590Srgrimes if (oddfield) { 2661590Srgrimes /* 2671590Srgrimes * We left off on an odd number of fields. If we haven't 2681590Srgrimes * crossed the midpoint of the screen, and we have room for 2691590Srgrimes * the next field, print it on the same line; otherwise, 2701590Srgrimes * print it on a new line. 2711590Srgrimes * 2721590Srgrimes * Note: we insist on having the right hand fields start 2731590Srgrimes * no less than 5 tabs out. 2741590Srgrimes */ 2751590Srgrimes maxlen = 5 * TAB_LEN; 2761590Srgrimes if (maxlen < lenlast) 2771590Srgrimes maxlen = lenlast; 2781590Srgrimes if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) + 2791590Srgrimes lenthis) <= LINE_LEN) { 2801590Srgrimes while(lenlast < (4 * TAB_LEN)) { 2811590Srgrimes putchar('\t'); 2821590Srgrimes lenlast += TAB_LEN; 2831590Srgrimes } 2841590Srgrimes (void)printf("\t%s\n", str); /* force one tab */ 2851590Srgrimes } else { 2861590Srgrimes (void)printf("\n%s", str); /* go to next line */ 2871590Srgrimes oddfield = !oddfield; /* this'll be undone below */ 2881590Srgrimes } 2891590Srgrimes } else 2901590Srgrimes (void)printf("%s", str); 2911590Srgrimes oddfield = !oddfield; /* toggle odd/even marker */ 2921590Srgrimes lenlast = lenthis; 2931590Srgrimes return(oddfield); 2941590Srgrimes} 2951590Srgrimes 29665064Sbrianint 297102944Sdwmaloneshow_text(const char *directory, const char *file_name, const char *header) 2981590Srgrimes{ 2991590Srgrimes struct stat sb; 30087229Smarkm FILE *fp; 30187229Smarkm int ch, cnt; 30287229Smarkm char *p, lastc; 3031590Srgrimes int fd, nr; 3041590Srgrimes 30567467Sru lastc = '\0'; 30667467Sru 3071590Srgrimes (void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name); 3081590Srgrimes if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) || 3091590Srgrimes sb.st_size == 0) 3101590Srgrimes return(0); 3111590Srgrimes 3121590Srgrimes /* If short enough, and no newlines, show it on a single line.*/ 3131590Srgrimes if (sb.st_size <= LINE_LEN - strlen(header) - 5) { 3141590Srgrimes nr = read(fd, tbuf, sizeof(tbuf)); 3151590Srgrimes if (nr <= 0) { 3161590Srgrimes (void)close(fd); 3171590Srgrimes return(0); 3181590Srgrimes } 3191590Srgrimes for (p = tbuf, cnt = nr; cnt--; ++p) 3201590Srgrimes if (*p == '\n') 3211590Srgrimes break; 3221590Srgrimes if (cnt <= 1) { 32365064Sbrian if (*header != '\0') 32465064Sbrian (void)printf("%s: ", header); 3251590Srgrimes for (p = tbuf, cnt = nr; cnt--; ++p) 32623971Sache if (*p != '\r') 32723971Sache vputc(lastc = *p); 3281590Srgrimes if (lastc != '\n') 3291590Srgrimes (void)putchar('\n'); 3301590Srgrimes (void)close(fd); 3311590Srgrimes return(1); 3321590Srgrimes } 3331590Srgrimes else 3341590Srgrimes (void)lseek(fd, 0L, SEEK_SET); 3351590Srgrimes } 3361590Srgrimes if ((fp = fdopen(fd, "r")) == NULL) 3371590Srgrimes return(0); 33865064Sbrian if (*header != '\0') 33965064Sbrian (void)printf("%s:\n", header); 3401590Srgrimes while ((ch = getc(fp)) != EOF) 34123971Sache if (ch != '\r') 34223971Sache vputc(lastc = ch); 3431590Srgrimes if (lastc != '\n') 3441590Srgrimes (void)putchar('\n'); 3451590Srgrimes (void)fclose(fp); 3461590Srgrimes return(1); 3471590Srgrimes} 3481590Srgrimes 3491590Srgrimesstatic void 350102944Sdwmalonevputc(unsigned char ch) 3511590Srgrimes{ 3521590Srgrimes int meta; 3531590Srgrimes 3549994Sache if (!isprint(ch) && !isascii(ch)) { 3551590Srgrimes (void)putchar('M'); 3561590Srgrimes (void)putchar('-'); 3571590Srgrimes ch = toascii(ch); 3581590Srgrimes meta = 1; 3591590Srgrimes } else 3601590Srgrimes meta = 0; 36167467Sru if (isprint(ch) || (!meta && (ch == ' ' || ch == '\t' || ch == '\n'))) 3621590Srgrimes (void)putchar(ch); 3631590Srgrimes else { 3641590Srgrimes (void)putchar('^'); 3651590Srgrimes (void)putchar(ch == '\177' ? '?' : ch | 0100); 3661590Srgrimes } 3671590Srgrimes} 368