Home | History | Annotate | Line # | Download | only in finger
lprint.c revision 1.22.8.1
      1  1.22.8.1       snj /*	$NetBSD: lprint.c,v 1.22.8.1 2015/02/13 09:19:59 snj Exp $	*/
      2       1.7       tls 
      3       1.1       cgd /*
      4       1.9       mrg  * Copyright (c) 1989, 1993
      5       1.9       mrg  *	The Regents of the University of California.  All rights reserved.
      6       1.1       cgd  *
      7       1.1       cgd  * This code is derived from software contributed to Berkeley by
      8       1.1       cgd  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
      9       1.1       cgd  *
     10       1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11       1.1       cgd  * modification, are permitted provided that the following conditions
     12       1.1       cgd  * are met:
     13       1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14       1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15       1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18      1.19       agc  * 3. Neither the name of the University nor the names of its contributors
     19       1.1       cgd  *    may be used to endorse or promote products derived from this software
     20       1.1       cgd  *    without specific prior written permission.
     21       1.1       cgd  *
     22       1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23       1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24       1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25       1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26       1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27       1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28       1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29       1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30       1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31       1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32       1.1       cgd  * SUCH DAMAGE.
     33       1.1       cgd  */
     34       1.1       cgd 
     35       1.9       mrg #include <sys/cdefs.h>
     36       1.1       cgd #ifndef lint
     37       1.9       mrg #if 0
     38       1.9       mrg static char sccsid[] = "@(#)lprint.c	8.3 (Berkeley) 4/28/95";
     39       1.9       mrg #else
     40  1.22.8.1       snj __RCSID( "$NetBSD: lprint.c,v 1.22.8.1 2015/02/13 09:19:59 snj Exp $");
     41       1.9       mrg #endif
     42       1.1       cgd #endif /* not lint */
     43       1.1       cgd 
     44       1.1       cgd #include <sys/types.h>
     45       1.1       cgd #include <sys/stat.h>
     46       1.1       cgd #include <sys/time.h>
     47       1.9       mrg #include <fcntl.h>
     48       1.1       cgd #include <tzfile.h>
     49       1.9       mrg #include <db.h>
     50       1.9       mrg #include <err.h>
     51       1.9       mrg #include <pwd.h>
     52       1.9       mrg #include <errno.h>
     53       1.9       mrg #include <unistd.h>
     54       1.1       cgd #include <stdio.h>
     55       1.4       cgd #include <string.h>
     56       1.5     lukem #include <time.h>
     57       1.1       cgd #include <ctype.h>
     58       1.1       cgd #include <paths.h>
     59       1.5     lukem #include <vis.h>
     60       1.9       mrg 
     61      1.13  christos #include "utmpentry.h"
     62       1.1       cgd #include "finger.h"
     63       1.5     lukem #include "extern.h"
     64       1.1       cgd 
     65       1.1       cgd #define	LINE_LEN	80
     66       1.1       cgd #define	TAB_LEN		8		/* 8 spaces between tabs */
     67       1.9       mrg #define	_PATH_FORWARD	".forward"
     68       1.1       cgd #define	_PATH_PLAN	".plan"
     69       1.1       cgd #define	_PATH_PROJECT	".project"
     70       1.1       cgd 
     71      1.22     lukem static int	demi_print(const char *, int);
     72      1.21     perry static void	lprint(PERSON *);
     73      1.22     lukem static int	show_text(const char *, const char *, const char *);
     74      1.21     perry static void	vputc(int);
     75       1.9       mrg 
     76      1.12  christos #ifdef __SVR4
     77      1.12  christos #define TIMEZONE(a)	tzname[0]
     78      1.12  christos #else
     79      1.12  christos #define TIMEZONE(a)	(a)->tm_zone
     80      1.12  christos #endif
     81      1.12  christos 
     82       1.5     lukem void
     83      1.21     perry lflag_print(void)
     84       1.1       cgd {
     85       1.5     lukem 	PERSON *pn;
     86       1.9       mrg 	int sflag, r;
     87       1.9       mrg 	PERSON *tmp;
     88       1.9       mrg 	DBT data, key;
     89      1.14      tron 
     90      1.14      tron 	if (db == NULL)
     91      1.14      tron 		return;
     92       1.9       mrg 
     93       1.9       mrg 	for (sflag = R_FIRST;; sflag = R_NEXT) {
     94       1.9       mrg 		r = (*db->seq)(db, &key, &data, sflag);
     95       1.9       mrg 		if (r == -1)
     96       1.9       mrg 			err(1, "db seq");
     97       1.9       mrg 		if (r == 1)
     98       1.9       mrg 			break;
     99       1.9       mrg 		memmove(&tmp, data.data, sizeof tmp);
    100       1.9       mrg 		pn = tmp;
    101       1.9       mrg 		if (sflag != R_FIRST)
    102       1.9       mrg 			putchar('\n');
    103       1.1       cgd 		lprint(pn);
    104       1.1       cgd 		if (!pplan) {
    105       1.9       mrg 			(void)show_text(pn->dir,
    106       1.9       mrg 			    _PATH_FORWARD, "Mail forwarded to");
    107       1.9       mrg 			(void)show_text(pn->dir, _PATH_PROJECT, "Project");
    108       1.9       mrg 			if (!show_text(pn->dir, _PATH_PLAN, "Plan"))
    109       1.1       cgd 				(void)printf("No Plan.\n");
    110       1.1       cgd 		}
    111       1.1       cgd 	}
    112       1.1       cgd }
    113       1.1       cgd 
    114  1.22.8.1       snj static size_t
    115  1.22.8.1       snj visify(char *buf, size_t blen, const char *str)
    116  1.22.8.1       snj {
    117  1.22.8.1       snj 	int len = strnvisx(buf, blen, str, strlen(str), VIS_WHITE|VIS_CSTYLE);
    118  1.22.8.1       snj 	if (len == -1) {
    119  1.22.8.1       snj 		buf[0] = '\0';
    120  1.22.8.1       snj 		return 0;
    121  1.22.8.1       snj 	}
    122  1.22.8.1       snj 	return len;
    123  1.22.8.1       snj }
    124  1.22.8.1       snj 
    125  1.22.8.1       snj static void
    126  1.22.8.1       snj fmt_time(char *buf, size_t blen, time_t ti, time_t n)
    127  1.22.8.1       snj {
    128  1.22.8.1       snj 	struct tm *tp = localtime(&ti);
    129  1.22.8.1       snj 	if (tp != NULL) {
    130  1.22.8.1       snj 		char *t = asctime(tp);
    131  1.22.8.1       snj 		char *tzn = TIMEZONE(tp);
    132  1.22.8.1       snj 		if (n == (time_t)-1 ||
    133  1.22.8.1       snj 		    n - ti > SECSPERDAY * DAYSPERNYEAR / 2)
    134  1.22.8.1       snj 			snprintf(buf, blen, "%.16s %.4s (%s)", t, t + 20, tzn);
    135  1.22.8.1       snj 		else
    136  1.22.8.1       snj 			snprintf(buf, blen, "%.16s (%s)", t, tzn);
    137  1.22.8.1       snj 	} else
    138  1.22.8.1       snj 		snprintf(buf, blen, "[*bad time 0x%llx*]", (long long)ti);
    139  1.22.8.1       snj }
    140  1.22.8.1       snj 
    141  1.22.8.1       snj /*
    142  1.22.8.1       snj  * idle time is tough; if have one, print a comma,
    143  1.22.8.1       snj  * then spaces to pad out the device name, then the
    144  1.22.8.1       snj  * idle time.  Follow with a comma if a remote login.
    145  1.22.8.1       snj  */
    146  1.22.8.1       snj static int
    147  1.22.8.1       snj print_idle(time_t t, int maxlen, size_t hostlen, size_t ttylen) {
    148  1.22.8.1       snj 
    149  1.22.8.1       snj 	int cpr;
    150  1.22.8.1       snj 	struct tm *delta = gmtime(&t);
    151  1.22.8.1       snj 
    152  1.22.8.1       snj 	if (delta == NULL)
    153  1.22.8.1       snj 		return printf("Bad idle 0x%llx", (long long)t);
    154  1.22.8.1       snj 
    155  1.22.8.1       snj 	if (delta->tm_yday == 0 && delta->tm_hour == 0 && delta->tm_min == 0)
    156  1.22.8.1       snj 		return 0;
    157  1.22.8.1       snj 
    158  1.22.8.1       snj 	cpr = printf("%-*s idle ", (int)(maxlen - ttylen + 1), ",");
    159  1.22.8.1       snj 	if (delta->tm_yday > 0) {
    160  1.22.8.1       snj 		cpr += printf("%d day%s ", delta->tm_yday,
    161  1.22.8.1       snj 		   delta->tm_yday == 1 ? "" : "s");
    162  1.22.8.1       snj 	}
    163  1.22.8.1       snj 	cpr += printf("%d:%02d", delta->tm_hour, delta->tm_min);
    164  1.22.8.1       snj 	if (hostlen) {
    165  1.22.8.1       snj 		putchar(',');
    166  1.22.8.1       snj 		++cpr;
    167  1.22.8.1       snj 	}
    168  1.22.8.1       snj 	return cpr;
    169  1.22.8.1       snj }
    170  1.22.8.1       snj 
    171       1.9       mrg static void
    172      1.21     perry lprint(PERSON *pn)
    173       1.1       cgd {
    174       1.5     lukem 	WHERE *w;
    175       1.5     lukem 	int cpr, len, maxlen;
    176       1.1       cgd 	int oddfield;
    177  1.22.8.1       snj 	char timebuf[128], ttybuf[64], hostbuf[512];
    178  1.22.8.1       snj 	size_t ttylen, hostlen;
    179       1.1       cgd 
    180       1.5     lukem 	cpr = 0;
    181       1.1       cgd 	/*
    182       1.1       cgd 	 * long format --
    183       1.1       cgd 	 *	login name
    184       1.1       cgd 	 *	real name
    185       1.1       cgd 	 *	home directory
    186       1.1       cgd 	 *	shell
    187       1.1       cgd 	 *	office, office phone, home phone if available
    188       1.5     lukem 	 *	mail status
    189       1.1       cgd 	 */
    190       1.1       cgd 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
    191       1.1       cgd 	    pn->name, pn->realname, pn->dir);
    192       1.1       cgd 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
    193       1.1       cgd 
    194       1.8       mrg 	if (gflag)
    195       1.8       mrg 		goto no_gecos;
    196       1.1       cgd 	/*
    197       1.1       cgd 	 * try and print office, office phone, and home phone on one line;
    198       1.1       cgd 	 * if that fails, do line filling so it looks nice.
    199       1.1       cgd 	 */
    200       1.1       cgd #define	OFFICE_TAG		"Office"
    201       1.1       cgd #define	OFFICE_PHONE_TAG	"Office Phone"
    202       1.1       cgd 	oddfield = 0;
    203       1.1       cgd 	if (pn->office && pn->officephone &&
    204       1.1       cgd 	    strlen(pn->office) + strlen(pn->officephone) +
    205       1.1       cgd 	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
    206  1.22.8.1       snj 		(void)snprintf(timebuf, sizeof(timebuf), "%s: %s, %s",
    207       1.9       mrg 		    OFFICE_TAG, pn->office, prphone(pn->officephone));
    208  1.22.8.1       snj 		oddfield = demi_print(timebuf, oddfield);
    209       1.1       cgd 	} else {
    210       1.1       cgd 		if (pn->office) {
    211  1.22.8.1       snj 			(void)snprintf(timebuf, sizeof(timebuf), "%s: %s",
    212       1.9       mrg 			    OFFICE_TAG, pn->office);
    213  1.22.8.1       snj 			oddfield = demi_print(timebuf, oddfield);
    214       1.1       cgd 		}
    215       1.1       cgd 		if (pn->officephone) {
    216  1.22.8.1       snj 			(void)snprintf(timebuf, sizeof(timebuf), "%s: %s",
    217       1.9       mrg 			    OFFICE_PHONE_TAG, prphone(pn->officephone));
    218  1.22.8.1       snj 			oddfield = demi_print(timebuf, oddfield);
    219       1.1       cgd 		}
    220       1.1       cgd 	}
    221       1.1       cgd 	if (pn->homephone) {
    222  1.22.8.1       snj 		(void)snprintf(timebuf, sizeof(timebuf), "%s: %s", "Home Phone",
    223       1.1       cgd 		    prphone(pn->homephone));
    224  1.22.8.1       snj 		oddfield = demi_print(timebuf, oddfield);
    225       1.1       cgd 	}
    226       1.1       cgd 	if (oddfield)
    227       1.1       cgd 		putchar('\n');
    228       1.1       cgd 
    229       1.8       mrg no_gecos:
    230       1.1       cgd 	/*
    231       1.5     lukem 	 * long format con't:
    232       1.5     lukem 	 * if logged in
    233       1.1       cgd 	 *	terminal
    234       1.1       cgd 	 *	idle time
    235       1.1       cgd 	 *	if messages allowed
    236       1.1       cgd 	 *	where logged in from
    237       1.1       cgd 	 * if not logged in
    238       1.1       cgd 	 *	when last logged in
    239       1.1       cgd 	 */
    240       1.1       cgd 	/* find out longest device name for this user for formatting */
    241  1.22.8.1       snj 	for (w = pn->whead, maxlen = -1; w != NULL; w = w->next) {
    242  1.22.8.1       snj 		visify(ttybuf, sizeof(ttybuf), w->tty);
    243  1.22.8.1       snj 		if ((len = strlen(ttybuf)) > maxlen)
    244       1.1       cgd 			maxlen = len;
    245  1.22.8.1       snj 	}
    246       1.1       cgd 	/* find rest of entries for user */
    247       1.1       cgd 	for (w = pn->whead; w != NULL; w = w->next) {
    248  1.22.8.1       snj 		ttylen = visify(ttybuf, sizeof(ttybuf), w->tty);
    249  1.22.8.1       snj 		hostlen = visify(hostbuf, sizeof(hostbuf), w->host);
    250       1.1       cgd 		switch (w->info) {
    251       1.1       cgd 		case LOGGEDIN:
    252  1.22.8.1       snj 			fmt_time(timebuf, sizeof(timebuf), w->loginat, -1);
    253  1.22.8.1       snj 			cpr = printf("On since %s on %s", timebuf, ttybuf);
    254  1.22.8.1       snj 
    255  1.22.8.1       snj 			cpr += print_idle(w->idletime, maxlen, hostlen,
    256  1.22.8.1       snj 			    ttylen);
    257  1.22.8.1       snj 
    258       1.1       cgd 			if (!w->writable)
    259       1.1       cgd 				cpr += printf(" (messages off)");
    260       1.1       cgd 			break;
    261       1.1       cgd 		case LASTLOG:
    262       1.1       cgd 			if (w->loginat == 0) {
    263       1.1       cgd 				(void)printf("Never logged in.");
    264       1.1       cgd 				break;
    265       1.1       cgd 			}
    266  1.22.8.1       snj 			fmt_time(timebuf, sizeof(timebuf), w->loginat, now);
    267  1.22.8.1       snj 			cpr = printf("Last login %s on %s", timebuf, ttybuf);
    268       1.1       cgd 			break;
    269       1.1       cgd 		}
    270  1.22.8.1       snj 		if (hostlen) {
    271  1.22.8.1       snj 			if (LINE_LEN < (cpr + 6 + hostlen))
    272       1.1       cgd 				(void)printf("\n   ");
    273  1.22.8.1       snj 			(void)printf(" from %s", hostbuf);
    274       1.1       cgd 		}
    275       1.1       cgd 		putchar('\n');
    276       1.3    brezak 	}
    277       1.3    brezak 	if (pn->mailrecv == -1)
    278       1.3    brezak 		printf("No Mail.\n");
    279       1.3    brezak 	else if (pn->mailrecv > pn->mailread) {
    280  1.22.8.1       snj 		fmt_time(timebuf, sizeof(timebuf), pn->mailrecv, -1);
    281  1.22.8.1       snj 		printf("New mail received %s\n", timebuf);
    282  1.22.8.1       snj 		fmt_time(timebuf, sizeof(timebuf), pn->mailread, -1);
    283  1.22.8.1       snj 		printf("     Unread since %s\n", timebuf);
    284       1.3    brezak 	} else {
    285  1.22.8.1       snj 		fmt_time(timebuf, sizeof(timebuf), pn->mailread, -1);
    286  1.22.8.1       snj 		printf("Mail last read %s\n", timebuf);
    287       1.1       cgd 	}
    288       1.1       cgd }
    289       1.1       cgd 
    290       1.9       mrg static int
    291      1.22     lukem demi_print(const char *str, int oddfield)
    292       1.1       cgd {
    293       1.1       cgd 	static int lenlast;
    294       1.1       cgd 	int lenthis, maxlen;
    295       1.1       cgd 
    296       1.1       cgd 	lenthis = strlen(str);
    297       1.1       cgd 	if (oddfield) {
    298       1.1       cgd 		/*
    299       1.1       cgd 		 * We left off on an odd number of fields.  If we haven't
    300       1.1       cgd 		 * crossed the midpoint of the screen, and we have room for
    301       1.1       cgd 		 * the next field, print it on the same line; otherwise,
    302       1.1       cgd 		 * print it on a new line.
    303       1.1       cgd 		 *
    304       1.1       cgd 		 * Note: we insist on having the right hand fields start
    305       1.1       cgd 		 * no less than 5 tabs out.
    306       1.1       cgd 		 */
    307       1.1       cgd 		maxlen = 5 * TAB_LEN;
    308       1.1       cgd 		if (maxlen < lenlast)
    309       1.1       cgd 			maxlen = lenlast;
    310       1.1       cgd 		if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
    311       1.1       cgd 		    lenthis) <= LINE_LEN) {
    312       1.1       cgd 			while(lenlast < (4 * TAB_LEN)) {
    313       1.1       cgd 				putchar('\t');
    314       1.1       cgd 				lenlast += TAB_LEN;
    315       1.1       cgd 			}
    316       1.1       cgd 			(void)printf("\t%s\n", str);	/* force one tab */
    317       1.1       cgd 		} else {
    318       1.1       cgd 			(void)printf("\n%s", str);	/* go to next line */
    319       1.1       cgd 			oddfield = !oddfield;	/* this'll be undone below */
    320       1.1       cgd 		}
    321       1.1       cgd 	} else
    322       1.1       cgd 		(void)printf("%s", str);
    323       1.1       cgd 	oddfield = !oddfield;			/* toggle odd/even marker */
    324       1.1       cgd 	lenlast = lenthis;
    325       1.1       cgd 	return(oddfield);
    326       1.1       cgd }
    327       1.1       cgd 
    328       1.9       mrg static int
    329      1.22     lukem show_text(const char *directory, const char *file_name, const char *header)
    330       1.1       cgd {
    331       1.9       mrg 	struct stat sb;
    332       1.5     lukem 	FILE *fp;
    333       1.9       mrg 	int ch, cnt, lastc;
    334       1.9       mrg 	char *p;
    335       1.9       mrg 	int fd, nr;
    336       1.1       cgd 
    337       1.5     lukem 	lastc = 0;
    338       1.5     lukem 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
    339       1.9       mrg 	if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
    340       1.9       mrg 	    sb.st_size == 0)
    341       1.9       mrg 		return(0);
    342       1.9       mrg 
    343       1.9       mrg 	/* If short enough, and no newlines, show it on a single line.*/
    344      1.22     lukem 	if (sb.st_size <= (off_t)(LINE_LEN - strlen(header) - 5)) {
    345       1.9       mrg 		nr = read(fd, tbuf, sizeof(tbuf));
    346       1.9       mrg 		if (nr <= 0) {
    347       1.9       mrg 			(void)close(fd);
    348       1.9       mrg 			return(0);
    349       1.9       mrg 		}
    350       1.9       mrg 		for (p = tbuf, cnt = nr; cnt--; ++p)
    351       1.9       mrg 			if (*p == '\n')
    352       1.9       mrg 				break;
    353       1.9       mrg 		if (cnt <= 1) {
    354       1.9       mrg 			(void)printf("%s: ", header);
    355       1.9       mrg 			for (p = tbuf, cnt = nr; cnt--; ++p)
    356      1.17       kim 				vputc(lastc = (unsigned char)*p);
    357       1.9       mrg 			if (lastc != '\n')
    358       1.9       mrg 				(void)putchar('\n');
    359       1.9       mrg 			(void)close(fd);
    360       1.9       mrg 			return(1);
    361       1.9       mrg 		}
    362       1.9       mrg 		else
    363       1.9       mrg 			(void)lseek(fd, 0L, SEEK_SET);
    364       1.9       mrg 	}
    365       1.9       mrg 	if ((fp = fdopen(fd, "r")) == NULL)
    366       1.1       cgd 		return(0);
    367       1.9       mrg 	(void)printf("%s:\n", header);
    368       1.1       cgd 	while ((ch = getc(fp)) != EOF)
    369       1.1       cgd 		vputc(lastc = ch);
    370       1.1       cgd 	if (lastc != '\n')
    371       1.1       cgd 		(void)putchar('\n');
    372       1.1       cgd 	(void)fclose(fp);
    373       1.1       cgd 	return(1);
    374       1.1       cgd }
    375       1.1       cgd 
    376       1.9       mrg static void
    377      1.21     perry vputc(int ch)
    378       1.1       cgd {
    379       1.5     lukem 	char visout[5], *s2;
    380       1.1       cgd 
    381      1.18       kim 	if (eightflag || isprint(ch) || isspace(ch)) {
    382      1.17       kim 	    (void)putchar(ch);
    383      1.17       kim 	    return;
    384      1.17       kim 	}
    385       1.5     lukem 	ch = toascii(ch);
    386       1.5     lukem 	vis(visout, ch, VIS_SAFE|VIS_NOSLASH, 0);
    387       1.5     lukem 	for (s2 = visout; *s2; s2++)
    388       1.5     lukem 		(void)putchar(*s2);
    389       1.1       cgd }
    390