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