xref: /csrg-svn/usr.bin/finger/lprint.c (revision 38052)
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*38052Sbostic static char sccsid[] = "@(#)lprint.c	5.5 (Berkeley) 05/18/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>
2837660Sbostic #include "finger.h"
2937660Sbostic #include "pathnames.h"
3037660Sbostic 
3137660Sbostic #define	LINE_LEN	80
3237660Sbostic #define	TAB_LEN		8		/* 8 spaces between tabs */
3337660Sbostic #define	_PATH_PLAN	".plan"
3437660Sbostic #define	_PATH_PROJECT	".project"
3537660Sbostic 
3637660Sbostic lflag_print()
3737660Sbostic {
3837660Sbostic 	extern int pplan;
3937660Sbostic 	register PERSON *pn;
4037660Sbostic 
4137664Sedward 	for (pn = phead;;) {
4237660Sbostic 		lprint(pn);
4337660Sbostic 		if (!pplan) {
4437664Sedward 			(void)show_text(pn->dir, _PATH_PROJECT, "Project:");
4537660Sbostic 			if (!show_text(pn->dir, _PATH_PLAN, "Plan:"))
4637660Sbostic 				(void)printf("No Plan.\n");
4737660Sbostic 		}
4837660Sbostic 		if (!(pn = pn->next))
4937660Sbostic 			break;
5037660Sbostic 		putchar('\n');
5137660Sbostic 	}
5237660Sbostic }
5337660Sbostic 
5437660Sbostic lprint(pn)
5537660Sbostic 	register PERSON *pn;
5637660Sbostic {
5737660Sbostic 	extern time_t now;
5837660Sbostic 	register struct tm *delta;
5937664Sedward 	register WHERE *w;
6037660Sbostic 	register int cpr, len, maxlen;
6137660Sbostic 	int oddfield;
6237660Sbostic 	time_t time();
63*38052Sbostic 	char *t, *ctime(), *prphone();
6437660Sbostic 
6537660Sbostic 	/*
6637660Sbostic 	 * long format --
6737660Sbostic 	 *	login name
6837660Sbostic 	 *	real name
6937660Sbostic 	 *	home directory
7037660Sbostic 	 *	shell
7137660Sbostic 	 *	office, office phone, home phone if available
7237660Sbostic 	 */
7337660Sbostic 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
7437660Sbostic 	    pn->name, pn->realname, pn->dir);
7537660Sbostic 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
7637660Sbostic 
7737660Sbostic 	/*
7837660Sbostic 	 * try and print office, office phone, and home phone on one line;
7937660Sbostic 	 * if that fails, do line filling so it looks nice.
8037660Sbostic 	 */
8137660Sbostic #define	OFFICE_TAG		"Office"
8237660Sbostic #define	OFFICE_PHONE_TAG	"Office Phone"
8337660Sbostic 	oddfield = 0;
8437660Sbostic 	if (pn->office && pn->officephone &&
8537660Sbostic 	    strlen(pn->office) + strlen(pn->officephone) +
8637660Sbostic 	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
8737660Sbostic 		(void)sprintf(tbuf, "%s: %s, %s", OFFICE_TAG, pn->office,
88*38052Sbostic 		    prphone(pn->officephone));
8937660Sbostic 		oddfield = demi_print(tbuf, oddfield);
9037660Sbostic 	} else {
9137660Sbostic 		if (pn->office) {
9237660Sbostic 			(void)sprintf(tbuf, "%s: %s", OFFICE_TAG, pn->office);
9337660Sbostic 			oddfield = demi_print(tbuf, oddfield);
9437660Sbostic 		}
9537660Sbostic 		if (pn->officephone) {
9637660Sbostic 			(void)sprintf(tbuf, "%s: %s", OFFICE_PHONE_TAG,
97*38052Sbostic 			    prphone(pn->officephone));
9837660Sbostic 			oddfield = demi_print(tbuf, oddfield);
9937660Sbostic 		}
10037660Sbostic 	}
10137660Sbostic 	if (pn->homephone) {
102*38052Sbostic 		(void)sprintf(tbuf, "%s: %s", "Home Phone",
103*38052Sbostic 		    prphone(pn->homephone));
10437660Sbostic 		oddfield = demi_print(tbuf, oddfield);
10537660Sbostic 	}
10637660Sbostic 	if (oddfield)
10737660Sbostic 		putchar('\n');
10837660Sbostic 
10937660Sbostic 	/*
11037664Sedward 	 * long format con't: * if logged in
11137660Sbostic 	 *	terminal
11237660Sbostic 	 *	idle time
11337660Sbostic 	 *	if messages allowed
11437660Sbostic 	 *	where logged in from
11537664Sedward 	 * if not logged in
11637664Sedward 	 *	when last logged in
11737660Sbostic 	 */
11837664Sedward 	/* find out longest device name for this user for formatting */
11937775Sbostic 	for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
12037664Sedward 		if ((len = strlen(w->tty)) > maxlen)
12137664Sedward 			maxlen = len;
12237664Sedward 	/* find rest of entries for user */
12337664Sedward 	for (w = pn->whead; w != NULL; w = w->next) {
12437664Sedward 		switch (w->info) {
12537664Sedward 		case LOGGEDIN:
12637660Sbostic 			cpr = printf("On since %16.16s on %s",
12737664Sedward 			    ctime(&w->loginat), w->tty);
12837660Sbostic 			/*
12937660Sbostic 			 * idle time is tough; if have one, print a comma,
13037660Sbostic 			 * then spaces to pad out the device name, then the
13137660Sbostic 			 * idle time.  Follow with a comma if a remote login.
13237660Sbostic 			 */
13337664Sedward 			delta = gmtime(&w->idletime);
13437660Sbostic 			if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
13537660Sbostic 				cpr += printf("%-*s idle ",
13637664Sedward 				    maxlen - strlen(w->tty) + 1, ",");
13737660Sbostic 				if (delta->tm_yday > 0) {
13837660Sbostic 					cpr += printf("%d day%s ",
13937660Sbostic 					   delta->tm_yday,
14037660Sbostic 					   delta->tm_yday == 1 ? "" : "s");
14137660Sbostic 				}
14237660Sbostic 				cpr += printf("%d:%02d",
14337660Sbostic 				    delta->tm_hour, delta->tm_min);
14437664Sedward 				if (*w->host) {
14537660Sbostic 					putchar(',');
14637660Sbostic 					++cpr;
14737660Sbostic 				}
14837660Sbostic 			}
14937664Sedward 			if (!w->writable)
15037660Sbostic 				cpr += printf(" (messages off)");
15137664Sedward 			break;
15237664Sedward 		case LASTLOG:
15337664Sedward 			if (w->loginat == 0) {
15437664Sedward 				(void)printf("Never logged in.");
15537664Sedward 				break;
15637660Sbostic 			}
15737664Sedward 			t = ctime(&w->loginat);
15837664Sedward 			if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
15937664Sedward 				cpr = printf("Last login %10.10s, %4.4s on %s",
16037664Sedward 				    t, t + 20, w->tty);
16137664Sedward 			else
16237664Sedward 				cpr = printf("Last login %16.16s on %s",
16337664Sedward 					t, w->tty);
16437664Sedward 			break;
16537660Sbostic 		}
16637664Sedward 		if (*w->host) {
16737664Sedward 			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
16837660Sbostic 				(void)printf("\n   ");
16937664Sedward 			(void)printf(" from %s", w->host);
17037660Sbostic 		}
17137660Sbostic 		putchar('\n');
17237660Sbostic 	}
17337660Sbostic }
17437660Sbostic 
17537660Sbostic demi_print(str, oddfield)
17637660Sbostic 	char *str;
17737660Sbostic 	int oddfield;
17837660Sbostic {
17937660Sbostic 	static int lenlast;
18037660Sbostic 	int lenthis, maxlen;
18137660Sbostic 
18237660Sbostic 	lenthis = strlen(str);
18337660Sbostic 	if (oddfield) {
18437660Sbostic 		/*
18537660Sbostic 		 * We left off on an odd number of fields.  If we haven't
18637660Sbostic 		 * crossed the midpoint of the screen, and we have room for
18737660Sbostic 		 * the next field, print it on the same line; otherwise,
18837660Sbostic 		 * print it on a new line.
18937660Sbostic 		 *
19037660Sbostic 		 * Note: we insist on having the right hand fields start
19137660Sbostic 		 * no less than 5 tabs out.
19237660Sbostic 		 */
19337660Sbostic 		maxlen = 5 * TAB_LEN;
19437660Sbostic 		if (maxlen < lenlast)
19537660Sbostic 			maxlen = lenlast;
19637660Sbostic 		if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
19737660Sbostic 		    lenthis) <= LINE_LEN) {
19837660Sbostic 			while(lenlast < (4 * TAB_LEN)) {
19937660Sbostic 				putchar('\t');
20037660Sbostic 				lenlast += TAB_LEN;
20137660Sbostic 			}
20237660Sbostic 			(void)printf("\t%s\n", str);	/* force one tab */
20337660Sbostic 		} else {
20437660Sbostic 			(void)printf("\n%s", str);	/* go to next line */
20537660Sbostic 			oddfield = !oddfield;	/* this'll be undone below */
20637660Sbostic 		}
20737660Sbostic 	} else
20837660Sbostic 		(void)printf("%s", str);
20937660Sbostic 	oddfield = !oddfield;			/* toggle odd/even marker */
21037660Sbostic 	lenlast = lenthis;
21137660Sbostic 	return(oddfield);
21237660Sbostic }
21337660Sbostic 
21437660Sbostic show_text(directory, file_name, header)
21537660Sbostic 	char *directory, *file_name, *header;
21637660Sbostic {
21737660Sbostic 	register int fd, n;
21837660Sbostic 
21937660Sbostic 	(void)sprintf(tbuf, "%s/%s", directory, file_name);
22037660Sbostic 	if ((fd = open(tbuf, O_RDONLY, 0)) < 0)
22137660Sbostic 		return(0);
22237660Sbostic 	(void)printf("%s\n", header);
22337660Sbostic 	(void)fflush(stdout);
22437660Sbostic 	while ((n = read(fd, tbuf, sizeof(tbuf))) > 0)
22537660Sbostic 		if (write(1, tbuf, n) != n)
22637660Sbostic 			break;
22737660Sbostic 	(void)close(fd);
22837660Sbostic 	return(1);
22937660Sbostic }
230