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