xref: /csrg-svn/usr.bin/finger/lprint.c (revision 42731)
137660Sbostic /*
237660Sbostic  * Copyright (c) 1989 The Regents of the University of California.
337660Sbostic  * All rights reserved.
437660Sbostic  *
540027Sbostic  * This code is derived from software contributed to Berkeley by
640027Sbostic  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
740027Sbostic  *
8*42731Sbostic  * %sccs.include.redist.c%
937660Sbostic  */
1037660Sbostic 
1137660Sbostic #ifndef lint
12*42731Sbostic static char sccsid[] = "@(#)lprint.c	5.10 (Berkeley) 06/01/90";
1337660Sbostic #endif /* not lint */
1437660Sbostic 
1537660Sbostic #include <sys/types.h>
1637660Sbostic #include <sys/file.h>
1737660Sbostic #include <sys/stat.h>
1837660Sbostic #include <sys/time.h>
1937660Sbostic #include <tzfile.h>
2037660Sbostic #include <stdio.h>
2138901Sbostic #include <ctype.h>
2237660Sbostic #include "finger.h"
2337660Sbostic #include "pathnames.h"
2437660Sbostic 
2537660Sbostic #define	LINE_LEN	80
2637660Sbostic #define	TAB_LEN		8		/* 8 spaces between tabs */
2737660Sbostic #define	_PATH_PLAN	".plan"
2837660Sbostic #define	_PATH_PROJECT	".project"
2937660Sbostic 
3037660Sbostic lflag_print()
3137660Sbostic {
3237660Sbostic 	extern int pplan;
3337660Sbostic 	register PERSON *pn;
3437660Sbostic 
3537664Sedward 	for (pn = phead;;) {
3637660Sbostic 		lprint(pn);
3737660Sbostic 		if (!pplan) {
3837664Sedward 			(void)show_text(pn->dir, _PATH_PROJECT, "Project:");
3937660Sbostic 			if (!show_text(pn->dir, _PATH_PLAN, "Plan:"))
4037660Sbostic 				(void)printf("No Plan.\n");
4137660Sbostic 		}
4237660Sbostic 		if (!(pn = pn->next))
4337660Sbostic 			break;
4437660Sbostic 		putchar('\n');
4537660Sbostic 	}
4637660Sbostic }
4737660Sbostic 
4837660Sbostic lprint(pn)
4937660Sbostic 	register PERSON *pn;
5037660Sbostic {
5137660Sbostic 	extern time_t now;
5237660Sbostic 	register struct tm *delta;
5337664Sedward 	register WHERE *w;
5437660Sbostic 	register int cpr, len, maxlen;
5537660Sbostic 	int oddfield;
5637660Sbostic 	time_t time();
5738052Sbostic 	char *t, *ctime(), *prphone();
5837660Sbostic 
5937660Sbostic 	/*
6037660Sbostic 	 * long format --
6137660Sbostic 	 *	login name
6237660Sbostic 	 *	real name
6337660Sbostic 	 *	home directory
6437660Sbostic 	 *	shell
6537660Sbostic 	 *	office, office phone, home phone if available
6637660Sbostic 	 */
6737660Sbostic 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
6837660Sbostic 	    pn->name, pn->realname, pn->dir);
6937660Sbostic 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
7037660Sbostic 
7137660Sbostic 	/*
7237660Sbostic 	 * try and print office, office phone, and home phone on one line;
7337660Sbostic 	 * if that fails, do line filling so it looks nice.
7437660Sbostic 	 */
7537660Sbostic #define	OFFICE_TAG		"Office"
7637660Sbostic #define	OFFICE_PHONE_TAG	"Office Phone"
7737660Sbostic 	oddfield = 0;
7837660Sbostic 	if (pn->office && pn->officephone &&
7937660Sbostic 	    strlen(pn->office) + strlen(pn->officephone) +
8037660Sbostic 	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
8137660Sbostic 		(void)sprintf(tbuf, "%s: %s, %s", OFFICE_TAG, pn->office,
8238052Sbostic 		    prphone(pn->officephone));
8337660Sbostic 		oddfield = demi_print(tbuf, oddfield);
8437660Sbostic 	} else {
8537660Sbostic 		if (pn->office) {
8637660Sbostic 			(void)sprintf(tbuf, "%s: %s", OFFICE_TAG, pn->office);
8737660Sbostic 			oddfield = demi_print(tbuf, oddfield);
8837660Sbostic 		}
8937660Sbostic 		if (pn->officephone) {
9037660Sbostic 			(void)sprintf(tbuf, "%s: %s", OFFICE_PHONE_TAG,
9138052Sbostic 			    prphone(pn->officephone));
9237660Sbostic 			oddfield = demi_print(tbuf, oddfield);
9337660Sbostic 		}
9437660Sbostic 	}
9537660Sbostic 	if (pn->homephone) {
9638052Sbostic 		(void)sprintf(tbuf, "%s: %s", "Home Phone",
9738052Sbostic 		    prphone(pn->homephone));
9837660Sbostic 		oddfield = demi_print(tbuf, oddfield);
9937660Sbostic 	}
10037660Sbostic 	if (oddfield)
10137660Sbostic 		putchar('\n');
10237660Sbostic 
10337660Sbostic 	/*
10437664Sedward 	 * long format con't: * if logged in
10537660Sbostic 	 *	terminal
10637660Sbostic 	 *	idle time
10737660Sbostic 	 *	if messages allowed
10837660Sbostic 	 *	where logged in from
10937664Sedward 	 * if not logged in
11037664Sedward 	 *	when last logged in
11137660Sbostic 	 */
11237664Sedward 	/* find out longest device name for this user for formatting */
11337775Sbostic 	for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
11437664Sedward 		if ((len = strlen(w->tty)) > maxlen)
11537664Sedward 			maxlen = len;
11637664Sedward 	/* find rest of entries for user */
11737664Sedward 	for (w = pn->whead; w != NULL; w = w->next) {
11837664Sedward 		switch (w->info) {
11937664Sedward 		case LOGGEDIN:
12037660Sbostic 			cpr = printf("On since %16.16s on %s",
12137664Sedward 			    ctime(&w->loginat), w->tty);
12237660Sbostic 			/*
12337660Sbostic 			 * idle time is tough; if have one, print a comma,
12437660Sbostic 			 * then spaces to pad out the device name, then the
12537660Sbostic 			 * idle time.  Follow with a comma if a remote login.
12637660Sbostic 			 */
12737664Sedward 			delta = gmtime(&w->idletime);
12837660Sbostic 			if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
12937660Sbostic 				cpr += printf("%-*s idle ",
13037664Sedward 				    maxlen - strlen(w->tty) + 1, ",");
13137660Sbostic 				if (delta->tm_yday > 0) {
13237660Sbostic 					cpr += printf("%d day%s ",
13337660Sbostic 					   delta->tm_yday,
13437660Sbostic 					   delta->tm_yday == 1 ? "" : "s");
13537660Sbostic 				}
13637660Sbostic 				cpr += printf("%d:%02d",
13737660Sbostic 				    delta->tm_hour, delta->tm_min);
13837664Sedward 				if (*w->host) {
13937660Sbostic 					putchar(',');
14037660Sbostic 					++cpr;
14137660Sbostic 				}
14237660Sbostic 			}
14337664Sedward 			if (!w->writable)
14437660Sbostic 				cpr += printf(" (messages off)");
14537664Sedward 			break;
14637664Sedward 		case LASTLOG:
14737664Sedward 			if (w->loginat == 0) {
14837664Sedward 				(void)printf("Never logged in.");
14937664Sedward 				break;
15037660Sbostic 			}
15137664Sedward 			t = ctime(&w->loginat);
15237664Sedward 			if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
15337664Sedward 				cpr = printf("Last login %10.10s, %4.4s on %s",
15437664Sedward 				    t, t + 20, w->tty);
15537664Sedward 			else
15637664Sedward 				cpr = printf("Last login %16.16s on %s",
15737664Sedward 					t, w->tty);
15837664Sedward 			break;
15937660Sbostic 		}
16037664Sedward 		if (*w->host) {
16137664Sedward 			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
16237660Sbostic 				(void)printf("\n   ");
16337664Sedward 			(void)printf(" from %s", w->host);
16437660Sbostic 		}
16537660Sbostic 		putchar('\n');
16637660Sbostic 	}
16737660Sbostic }
16837660Sbostic 
16937660Sbostic demi_print(str, oddfield)
17037660Sbostic 	char *str;
17137660Sbostic 	int oddfield;
17237660Sbostic {
17337660Sbostic 	static int lenlast;
17437660Sbostic 	int lenthis, maxlen;
17537660Sbostic 
17637660Sbostic 	lenthis = strlen(str);
17737660Sbostic 	if (oddfield) {
17837660Sbostic 		/*
17937660Sbostic 		 * We left off on an odd number of fields.  If we haven't
18037660Sbostic 		 * crossed the midpoint of the screen, and we have room for
18137660Sbostic 		 * the next field, print it on the same line; otherwise,
18237660Sbostic 		 * print it on a new line.
18337660Sbostic 		 *
18437660Sbostic 		 * Note: we insist on having the right hand fields start
18537660Sbostic 		 * no less than 5 tabs out.
18637660Sbostic 		 */
18737660Sbostic 		maxlen = 5 * TAB_LEN;
18837660Sbostic 		if (maxlen < lenlast)
18937660Sbostic 			maxlen = lenlast;
19037660Sbostic 		if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
19137660Sbostic 		    lenthis) <= LINE_LEN) {
19237660Sbostic 			while(lenlast < (4 * TAB_LEN)) {
19337660Sbostic 				putchar('\t');
19437660Sbostic 				lenlast += TAB_LEN;
19537660Sbostic 			}
19637660Sbostic 			(void)printf("\t%s\n", str);	/* force one tab */
19737660Sbostic 		} else {
19837660Sbostic 			(void)printf("\n%s", str);	/* go to next line */
19937660Sbostic 			oddfield = !oddfield;	/* this'll be undone below */
20037660Sbostic 		}
20137660Sbostic 	} else
20237660Sbostic 		(void)printf("%s", str);
20337660Sbostic 	oddfield = !oddfield;			/* toggle odd/even marker */
20437660Sbostic 	lenlast = lenthis;
20537660Sbostic 	return(oddfield);
20637660Sbostic }
20737660Sbostic 
20837660Sbostic show_text(directory, file_name, header)
20937660Sbostic 	char *directory, *file_name, *header;
21037660Sbostic {
21139241Sedward 	register int ch, lastc;
21239241Sedward 	register FILE *fp;
21337660Sbostic 
21437660Sbostic 	(void)sprintf(tbuf, "%s/%s", directory, file_name);
21539241Sedward 	if ((fp = fopen(tbuf, "r")) == NULL)
21637660Sbostic 		return(0);
21737660Sbostic 	(void)printf("%s\n", header);
21839241Sedward 	while ((ch = getc(fp)) != EOF)
21939241Sedward 		vputc(lastc = ch);
22039241Sedward 	if (lastc != '\n')
22138901Sbostic 		(void)putchar('\n');
22239241Sedward 	(void)fclose(fp);
22337660Sbostic 	return(1);
22437660Sbostic }
22538901Sbostic 
22638901Sbostic vputc(ch)
22738901Sbostic 	register int ch;
22838901Sbostic {
22938901Sbostic 	int meta;
23038901Sbostic 
23138901Sbostic 	if (!isascii(ch)) {
23238901Sbostic 		(void)putchar('M');
23338901Sbostic 		(void)putchar('-');
23438901Sbostic 		ch = toascii(ch);
23538901Sbostic 		meta = 1;
23638901Sbostic 	} else
23738901Sbostic 		meta = 0;
23838912Sbostic 	if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n'))
23938901Sbostic 		(void)putchar(ch);
24038901Sbostic 	else {
24138901Sbostic 		(void)putchar('^');
24238901Sbostic 		(void)putchar(ch == '\177' ? '?' : ch | 0100);
24338901Sbostic 	}
24438901Sbostic }
245