xref: /csrg-svn/usr.bin/finger/lprint.c (revision 38912)
137660Sbostic /*
237660Sbostic  * Copyright (c) 1989 The Regents of the University of California.
337660Sbostic  * All rights reserved.
437660Sbostic  *
537660Sbostic  * Redistribution and use in source and binary forms are permitted
637660Sbostic  * provided that the above copyright notice and this paragraph are
737660Sbostic  * duplicated in all such forms and that any documentation,
837660Sbostic  * advertising materials, and other materials related to such
937660Sbostic  * distribution and use acknowledge that the software was developed
1037660Sbostic  * by the University of California, Berkeley.  The name of the
1137660Sbostic  * University may not be used to endorse or promote products derived
1237660Sbostic  * from this software without specific prior written permission.
1337660Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1437660Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1537660Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1637660Sbostic  */
1737660Sbostic 
1837660Sbostic #ifndef lint
19*38912Sbostic static char sccsid[] = "@(#)lprint.c	5.7 (Berkeley) 09/02/89";
2037660Sbostic #endif /* not lint */
2137660Sbostic 
2237660Sbostic #include <sys/types.h>
2337660Sbostic #include <sys/file.h>
2437660Sbostic #include <sys/stat.h>
2537660Sbostic #include <sys/time.h>
2637660Sbostic #include <tzfile.h>
2737660Sbostic #include <stdio.h>
2838901Sbostic #include <ctype.h>
2937660Sbostic #include "finger.h"
3037660Sbostic #include "pathnames.h"
3137660Sbostic 
3237660Sbostic #define	LINE_LEN	80
3337660Sbostic #define	TAB_LEN		8		/* 8 spaces between tabs */
3437660Sbostic #define	_PATH_PLAN	".plan"
3537660Sbostic #define	_PATH_PROJECT	".project"
3637660Sbostic 
3737660Sbostic lflag_print()
3837660Sbostic {
3937660Sbostic 	extern int pplan;
4037660Sbostic 	register PERSON *pn;
4137660Sbostic 
4237664Sedward 	for (pn = phead;;) {
4337660Sbostic 		lprint(pn);
4437660Sbostic 		if (!pplan) {
4537664Sedward 			(void)show_text(pn->dir, _PATH_PROJECT, "Project:");
4637660Sbostic 			if (!show_text(pn->dir, _PATH_PLAN, "Plan:"))
4737660Sbostic 				(void)printf("No Plan.\n");
4837660Sbostic 		}
4937660Sbostic 		if (!(pn = pn->next))
5037660Sbostic 			break;
5137660Sbostic 		putchar('\n');
5237660Sbostic 	}
5337660Sbostic }
5437660Sbostic 
5537660Sbostic lprint(pn)
5637660Sbostic 	register PERSON *pn;
5737660Sbostic {
5837660Sbostic 	extern time_t now;
5937660Sbostic 	register struct tm *delta;
6037664Sedward 	register WHERE *w;
6137660Sbostic 	register int cpr, len, maxlen;
6237660Sbostic 	int oddfield;
6337660Sbostic 	time_t time();
6438052Sbostic 	char *t, *ctime(), *prphone();
6537660Sbostic 
6637660Sbostic 	/*
6737660Sbostic 	 * long format --
6837660Sbostic 	 *	login name
6937660Sbostic 	 *	real name
7037660Sbostic 	 *	home directory
7137660Sbostic 	 *	shell
7237660Sbostic 	 *	office, office phone, home phone if available
7337660Sbostic 	 */
7437660Sbostic 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
7537660Sbostic 	    pn->name, pn->realname, pn->dir);
7637660Sbostic 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
7737660Sbostic 
7837660Sbostic 	/*
7937660Sbostic 	 * try and print office, office phone, and home phone on one line;
8037660Sbostic 	 * if that fails, do line filling so it looks nice.
8137660Sbostic 	 */
8237660Sbostic #define	OFFICE_TAG		"Office"
8337660Sbostic #define	OFFICE_PHONE_TAG	"Office Phone"
8437660Sbostic 	oddfield = 0;
8537660Sbostic 	if (pn->office && pn->officephone &&
8637660Sbostic 	    strlen(pn->office) + strlen(pn->officephone) +
8737660Sbostic 	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
8837660Sbostic 		(void)sprintf(tbuf, "%s: %s, %s", OFFICE_TAG, pn->office,
8938052Sbostic 		    prphone(pn->officephone));
9037660Sbostic 		oddfield = demi_print(tbuf, oddfield);
9137660Sbostic 	} else {
9237660Sbostic 		if (pn->office) {
9337660Sbostic 			(void)sprintf(tbuf, "%s: %s", OFFICE_TAG, pn->office);
9437660Sbostic 			oddfield = demi_print(tbuf, oddfield);
9537660Sbostic 		}
9637660Sbostic 		if (pn->officephone) {
9737660Sbostic 			(void)sprintf(tbuf, "%s: %s", OFFICE_PHONE_TAG,
9838052Sbostic 			    prphone(pn->officephone));
9937660Sbostic 			oddfield = demi_print(tbuf, oddfield);
10037660Sbostic 		}
10137660Sbostic 	}
10237660Sbostic 	if (pn->homephone) {
10338052Sbostic 		(void)sprintf(tbuf, "%s: %s", "Home Phone",
10438052Sbostic 		    prphone(pn->homephone));
10537660Sbostic 		oddfield = demi_print(tbuf, oddfield);
10637660Sbostic 	}
10737660Sbostic 	if (oddfield)
10837660Sbostic 		putchar('\n');
10937660Sbostic 
11037660Sbostic 	/*
11137664Sedward 	 * long format con't: * if logged in
11237660Sbostic 	 *	terminal
11337660Sbostic 	 *	idle time
11437660Sbostic 	 *	if messages allowed
11537660Sbostic 	 *	where logged in from
11637664Sedward 	 * if not logged in
11737664Sedward 	 *	when last logged in
11837660Sbostic 	 */
11937664Sedward 	/* find out longest device name for this user for formatting */
12037775Sbostic 	for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
12137664Sedward 		if ((len = strlen(w->tty)) > maxlen)
12237664Sedward 			maxlen = len;
12337664Sedward 	/* find rest of entries for user */
12437664Sedward 	for (w = pn->whead; w != NULL; w = w->next) {
12537664Sedward 		switch (w->info) {
12637664Sedward 		case LOGGEDIN:
12737660Sbostic 			cpr = printf("On since %16.16s on %s",
12837664Sedward 			    ctime(&w->loginat), w->tty);
12937660Sbostic 			/*
13037660Sbostic 			 * idle time is tough; if have one, print a comma,
13137660Sbostic 			 * then spaces to pad out the device name, then the
13237660Sbostic 			 * idle time.  Follow with a comma if a remote login.
13337660Sbostic 			 */
13437664Sedward 			delta = gmtime(&w->idletime);
13537660Sbostic 			if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
13637660Sbostic 				cpr += printf("%-*s idle ",
13737664Sedward 				    maxlen - strlen(w->tty) + 1, ",");
13837660Sbostic 				if (delta->tm_yday > 0) {
13937660Sbostic 					cpr += printf("%d day%s ",
14037660Sbostic 					   delta->tm_yday,
14137660Sbostic 					   delta->tm_yday == 1 ? "" : "s");
14237660Sbostic 				}
14337660Sbostic 				cpr += printf("%d:%02d",
14437660Sbostic 				    delta->tm_hour, delta->tm_min);
14537664Sedward 				if (*w->host) {
14637660Sbostic 					putchar(',');
14737660Sbostic 					++cpr;
14837660Sbostic 				}
14937660Sbostic 			}
15037664Sedward 			if (!w->writable)
15137660Sbostic 				cpr += printf(" (messages off)");
15237664Sedward 			break;
15337664Sedward 		case LASTLOG:
15437664Sedward 			if (w->loginat == 0) {
15537664Sedward 				(void)printf("Never logged in.");
15637664Sedward 				break;
15737660Sbostic 			}
15837664Sedward 			t = ctime(&w->loginat);
15937664Sedward 			if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
16037664Sedward 				cpr = printf("Last login %10.10s, %4.4s on %s",
16137664Sedward 				    t, t + 20, w->tty);
16237664Sedward 			else
16337664Sedward 				cpr = printf("Last login %16.16s on %s",
16437664Sedward 					t, w->tty);
16537664Sedward 			break;
16637660Sbostic 		}
16737664Sedward 		if (*w->host) {
16837664Sedward 			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
16937660Sbostic 				(void)printf("\n   ");
17037664Sedward 			(void)printf(" from %s", w->host);
17137660Sbostic 		}
17237660Sbostic 		putchar('\n');
17337660Sbostic 	}
17437660Sbostic }
17537660Sbostic 
17637660Sbostic demi_print(str, oddfield)
17737660Sbostic 	char *str;
17837660Sbostic 	int oddfield;
17937660Sbostic {
18037660Sbostic 	static int lenlast;
18137660Sbostic 	int lenthis, maxlen;
18237660Sbostic 
18337660Sbostic 	lenthis = strlen(str);
18437660Sbostic 	if (oddfield) {
18537660Sbostic 		/*
18637660Sbostic 		 * We left off on an odd number of fields.  If we haven't
18737660Sbostic 		 * crossed the midpoint of the screen, and we have room for
18837660Sbostic 		 * the next field, print it on the same line; otherwise,
18937660Sbostic 		 * print it on a new line.
19037660Sbostic 		 *
19137660Sbostic 		 * Note: we insist on having the right hand fields start
19237660Sbostic 		 * no less than 5 tabs out.
19337660Sbostic 		 */
19437660Sbostic 		maxlen = 5 * TAB_LEN;
19537660Sbostic 		if (maxlen < lenlast)
19637660Sbostic 			maxlen = lenlast;
19737660Sbostic 		if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
19837660Sbostic 		    lenthis) <= LINE_LEN) {
19937660Sbostic 			while(lenlast < (4 * TAB_LEN)) {
20037660Sbostic 				putchar('\t');
20137660Sbostic 				lenlast += TAB_LEN;
20237660Sbostic 			}
20337660Sbostic 			(void)printf("\t%s\n", str);	/* force one tab */
20437660Sbostic 		} else {
20537660Sbostic 			(void)printf("\n%s", str);	/* go to next line */
20637660Sbostic 			oddfield = !oddfield;	/* this'll be undone below */
20737660Sbostic 		}
20837660Sbostic 	} else
20937660Sbostic 		(void)printf("%s", str);
21037660Sbostic 	oddfield = !oddfield;			/* toggle odd/even marker */
21137660Sbostic 	lenlast = lenthis;
21237660Sbostic 	return(oddfield);
21337660Sbostic }
21437660Sbostic 
21537660Sbostic show_text(directory, file_name, header)
21637660Sbostic 	char *directory, *file_name, *header;
21737660Sbostic {
21838901Sbostic 	register int ch;
21937660Sbostic 
22037660Sbostic 	(void)sprintf(tbuf, "%s/%s", directory, file_name);
22138901Sbostic 	if (!freopen(tbuf, "r", stdin))
22237660Sbostic 		return(0);
22337660Sbostic 	(void)printf("%s\n", header);
22438901Sbostic 	while ((ch = getchar(fp)) != EOF)
22538901Sbostic 		vputc(ch);
22638901Sbostic 	if (ch != '\n')
22738901Sbostic 		(void)putchar('\n');
22837660Sbostic 	return(1);
22937660Sbostic }
23038901Sbostic 
23138901Sbostic vputc(ch)
23238901Sbostic 	register int ch;
23338901Sbostic {
23438901Sbostic 	int meta;
23538901Sbostic 
23638901Sbostic 	if (!isascii(ch)) {
23738901Sbostic 		(void)putchar('M');
23838901Sbostic 		(void)putchar('-');
23938901Sbostic 		ch = toascii(ch);
24038901Sbostic 		meta = 1;
24138901Sbostic 	} else
24238901Sbostic 		meta = 0;
243*38912Sbostic 	if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n'))
24438901Sbostic 		(void)putchar(ch);
24538901Sbostic 	else {
24638901Sbostic 		(void)putchar('^');
24738901Sbostic 		(void)putchar(ch == '\177' ? '?' : ch | 0100);
24838901Sbostic 	}
24938901Sbostic }
250