xref: /csrg-svn/usr.bin/finger/lprint.c (revision 37664)
137660Sbostic /*
237660Sbostic  * Copyright (c) 1989 The Regents of the University of California.
337660Sbostic  * All rights reserved.
437660Sbostic  *
537660Sbostic  * This code is derived from software contributed to Berkeley by
637660Sbostic  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
737660Sbostic  *
837660Sbostic  * Redistribution and use in source and binary forms are permitted
937660Sbostic  * provided that the above copyright notice and this paragraph are
1037660Sbostic  * duplicated in all such forms and that any documentation,
1137660Sbostic  * advertising materials, and other materials related to such
1237660Sbostic  * distribution and use acknowledge that the software was developed
1337660Sbostic  * by the University of California, Berkeley.  The name of the
1437660Sbostic  * University may not be used to endorse or promote products derived
1537660Sbostic  * from this software without specific prior written permission.
1637660Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1737660Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1837660Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1937660Sbostic  */
2037660Sbostic 
2137660Sbostic #ifndef lint
22*37664Sedward static char sccsid[] = "@(#)lprint.c	5.2 (Berkeley) 05/07/89";
2337660Sbostic #endif /* not lint */
2437660Sbostic 
2537660Sbostic #include <sys/types.h>
2637660Sbostic #include <sys/file.h>
2737660Sbostic #include <sys/stat.h>
2837660Sbostic #include <sys/time.h>
2937660Sbostic #include <tzfile.h>
3037660Sbostic #include <stdio.h>
3137660Sbostic #include "finger.h"
3237660Sbostic #include "pathnames.h"
3337660Sbostic 
3437660Sbostic #define	LINE_LEN	80
3537660Sbostic #define	TAB_LEN		8		/* 8 spaces between tabs */
3637660Sbostic #define	_PATH_PLAN	".plan"
3737660Sbostic #define	_PATH_PROJECT	".project"
3837660Sbostic 
3937660Sbostic lflag_print()
4037660Sbostic {
4137660Sbostic 	extern int pplan;
4237660Sbostic 	register PERSON *pn;
4337660Sbostic 
44*37664Sedward 	for (pn = phead;;) {
4537660Sbostic 		lprint(pn);
4637660Sbostic 		if (!pplan) {
47*37664Sedward 			(void)show_text(pn->dir, _PATH_PROJECT, "Project:");
4837660Sbostic 			if (!show_text(pn->dir, _PATH_PLAN, "Plan:"))
4937660Sbostic 				(void)printf("No Plan.\n");
5037660Sbostic 		}
5137660Sbostic 		if (!(pn = pn->next))
5237660Sbostic 			break;
5337660Sbostic 		putchar('\n');
5437660Sbostic 	}
5537660Sbostic }
5637660Sbostic 
5737660Sbostic lprint(pn)
5837660Sbostic 	register PERSON *pn;
5937660Sbostic {
6037660Sbostic 	extern time_t now;
6137660Sbostic 	register struct tm *delta;
62*37664Sedward 	register WHERE *w;
6337660Sbostic 	register int cpr, len, maxlen;
6437660Sbostic 	int oddfield;
6537660Sbostic 	time_t time();
6637660Sbostic 	char *t, *ctime();
6737660Sbostic 
6837660Sbostic 	/*
6937660Sbostic 	 * long format --
7037660Sbostic 	 *	login name
7137660Sbostic 	 *	real name
7237660Sbostic 	 *	home directory
7337660Sbostic 	 *	shell
7437660Sbostic 	 *	office, office phone, home phone if available
7537660Sbostic 	 */
7637660Sbostic 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
7737660Sbostic 	    pn->name, pn->realname, pn->dir);
7837660Sbostic 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
7937660Sbostic 
8037660Sbostic 	/*
8137660Sbostic 	 * try and print office, office phone, and home phone on one line;
8237660Sbostic 	 * if that fails, do line filling so it looks nice.
8337660Sbostic 	 */
8437660Sbostic #define	OFFICE_TAG		"Office"
8537660Sbostic #define	OFFICE_PHONE_TAG	"Office Phone"
8637660Sbostic 	oddfield = 0;
8737660Sbostic 	if (pn->office && pn->officephone &&
8837660Sbostic 	    strlen(pn->office) + strlen(pn->officephone) +
8937660Sbostic 	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
9037660Sbostic 		(void)sprintf(tbuf, "%s: %s, %s", OFFICE_TAG, pn->office,
9137660Sbostic 		    pn->officephone);
9237660Sbostic 		oddfield = demi_print(tbuf, oddfield);
9337660Sbostic 	} else {
9437660Sbostic 		if (pn->office) {
9537660Sbostic 			(void)sprintf(tbuf, "%s: %s", OFFICE_TAG, pn->office);
9637660Sbostic 			oddfield = demi_print(tbuf, oddfield);
9737660Sbostic 		}
9837660Sbostic 		if (pn->officephone) {
9937660Sbostic 			(void)sprintf(tbuf, "%s: %s", OFFICE_PHONE_TAG,
10037660Sbostic 			    pn->officephone);
10137660Sbostic 			oddfield = demi_print(tbuf, oddfield);
10237660Sbostic 		}
10337660Sbostic 	}
10437660Sbostic 	if (pn->homephone) {
10537660Sbostic 		(void)sprintf(tbuf, "%s: %s", "Home Phone", pn->homephone);
10637660Sbostic 		oddfield = demi_print(tbuf, oddfield);
10737660Sbostic 	}
10837660Sbostic 	if (oddfield)
10937660Sbostic 		putchar('\n');
11037660Sbostic 
11137660Sbostic 	/*
112*37664Sedward 	 * long format con't: * if logged in
11337660Sbostic 	 *	terminal
11437660Sbostic 	 *	idle time
11537660Sbostic 	 *	if messages allowed
11637660Sbostic 	 *	where logged in from
117*37664Sedward 	 * if not logged in
118*37664Sedward 	 *	when last logged in
11937660Sbostic 	 */
120*37664Sedward 	/* find out longest device name for this user for formatting */
121*37664Sedward 	for (w = pn->whead; w != NULL; w = w->next)
122*37664Sedward 		if ((len = strlen(w->tty)) > maxlen)
123*37664Sedward 			maxlen = len;
124*37664Sedward 	/* find rest of entries for user */
125*37664Sedward 	for (w = pn->whead; w != NULL; w = w->next) {
126*37664Sedward 		switch (w->info) {
127*37664Sedward 		case LOGGEDIN:
12837660Sbostic 			cpr = printf("On since %16.16s on %s",
129*37664Sedward 			    ctime(&w->loginat), w->tty);
13037660Sbostic 			/*
13137660Sbostic 			 * idle time is tough; if have one, print a comma,
13237660Sbostic 			 * then spaces to pad out the device name, then the
13337660Sbostic 			 * idle time.  Follow with a comma if a remote login.
13437660Sbostic 			 */
135*37664Sedward 			delta = gmtime(&w->idletime);
13637660Sbostic 			if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
13737660Sbostic 				cpr += printf("%-*s idle ",
138*37664Sedward 				    maxlen - strlen(w->tty) + 1, ",");
13937660Sbostic 				if (delta->tm_yday > 0) {
14037660Sbostic 					cpr += printf("%d day%s ",
14137660Sbostic 					   delta->tm_yday,
14237660Sbostic 					   delta->tm_yday == 1 ? "" : "s");
14337660Sbostic 				}
14437660Sbostic 				cpr += printf("%d:%02d",
14537660Sbostic 				    delta->tm_hour, delta->tm_min);
146*37664Sedward 				if (*w->host) {
14737660Sbostic 					putchar(',');
14837660Sbostic 					++cpr;
14937660Sbostic 				}
15037660Sbostic 			}
151*37664Sedward 			if (!w->writable)
15237660Sbostic 				cpr += printf(" (messages off)");
153*37664Sedward 			break;
154*37664Sedward 		case LASTLOG:
155*37664Sedward 			if (w->loginat == 0) {
156*37664Sedward 				(void)printf("Never logged in.");
157*37664Sedward 				break;
15837660Sbostic 			}
159*37664Sedward 			t = ctime(&w->loginat);
160*37664Sedward 			if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
161*37664Sedward 				cpr = printf("Last login %10.10s, %4.4s on %s",
162*37664Sedward 				    t, t + 20, w->tty);
163*37664Sedward 			else
164*37664Sedward 				cpr = printf("Last login %16.16s on %s",
165*37664Sedward 					t, w->tty);
166*37664Sedward 			break;
16737660Sbostic 		}
168*37664Sedward 		if (*w->host) {
169*37664Sedward 			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
17037660Sbostic 				(void)printf("\n   ");
171*37664Sedward 			(void)printf(" from %s", w->host);
17237660Sbostic 		}
17337660Sbostic 		putchar('\n');
17437660Sbostic 	}
17537660Sbostic 	/*
17637660Sbostic 	 * long format con't:
17737660Sbostic 	 *	mail status
17837660Sbostic 	 */
17937660Sbostic 	chkmail(pn);
18037660Sbostic }
18137660Sbostic 
18237660Sbostic demi_print(str, oddfield)
18337660Sbostic 	char *str;
18437660Sbostic 	int oddfield;
18537660Sbostic {
18637660Sbostic 	static int lenlast;
18737660Sbostic 	int lenthis, maxlen;
18837660Sbostic 
18937660Sbostic 	lenthis = strlen(str);
19037660Sbostic 	if (oddfield) {
19137660Sbostic 		/*
19237660Sbostic 		 * We left off on an odd number of fields.  If we haven't
19337660Sbostic 		 * crossed the midpoint of the screen, and we have room for
19437660Sbostic 		 * the next field, print it on the same line; otherwise,
19537660Sbostic 		 * print it on a new line.
19637660Sbostic 		 *
19737660Sbostic 		 * Note: we insist on having the right hand fields start
19837660Sbostic 		 * no less than 5 tabs out.
19937660Sbostic 		 */
20037660Sbostic 		maxlen = 5 * TAB_LEN;
20137660Sbostic 		if (maxlen < lenlast)
20237660Sbostic 			maxlen = lenlast;
20337660Sbostic 		if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
20437660Sbostic 		    lenthis) <= LINE_LEN) {
20537660Sbostic 			while(lenlast < (4 * TAB_LEN)) {
20637660Sbostic 				putchar('\t');
20737660Sbostic 				lenlast += TAB_LEN;
20837660Sbostic 			}
20937660Sbostic 			(void)printf("\t%s\n", str);	/* force one tab */
21037660Sbostic 		} else {
21137660Sbostic 			(void)printf("\n%s", str);	/* go to next line */
21237660Sbostic 			oddfield = !oddfield;	/* this'll be undone below */
21337660Sbostic 		}
21437660Sbostic 	} else
21537660Sbostic 		(void)printf("%s", str);
21637660Sbostic 	oddfield = !oddfield;			/* toggle odd/even marker */
21737660Sbostic 	lenlast = lenthis;
21837660Sbostic 	return(oddfield);
21937660Sbostic }
22037660Sbostic 
22137660Sbostic show_text(directory, file_name, header)
22237660Sbostic 	char *directory, *file_name, *header;
22337660Sbostic {
22437660Sbostic 	register int fd, n;
22537660Sbostic 
22637660Sbostic 	(void)sprintf(tbuf, "%s/%s", directory, file_name);
22737660Sbostic 	if ((fd = open(tbuf, O_RDONLY, 0)) < 0)
22837660Sbostic 		return(0);
22937660Sbostic 	(void)printf("%s\n", header);
23037660Sbostic 	(void)fflush(stdout);
23137660Sbostic 	while ((n = read(fd, tbuf, sizeof(tbuf))) > 0)
23237660Sbostic 		if (write(1, tbuf, n) != n)
23337660Sbostic 			break;
23437660Sbostic 	(void)close(fd);
23537660Sbostic 	return(1);
23637660Sbostic }
23737660Sbostic 
23837660Sbostic chkmail(pn)
23937660Sbostic 	PERSON *pn;
24037660Sbostic {
24137660Sbostic 	register char *date;
24237660Sbostic 	struct stat sb;
24337660Sbostic 
24437660Sbostic 	/*
24537660Sbostic 	 * build path of user's mail box and get stats; if missing
24637660Sbostic 	 * or empty, no mail.
24737660Sbostic 	 */
24837660Sbostic 	(void)sprintf(tbuf, "%s/%s", _PATH_MAILDIR, pn->name);
24937660Sbostic 	if (stat(tbuf, &sb) < 0 || !sb.st_size) {
25037660Sbostic 		(void)printf("No unread mail.\n");
25137660Sbostic 		return;
25237660Sbostic 	}
25337660Sbostic 
25437660Sbostic 	/*
25537660Sbostic 	 * if access time matches the modification time then we know
25637660Sbostic 	 * that new mail was received but we haven't a clue as to when
25737660Sbostic 	 * it was last read.
25837660Sbostic 	 */
25937660Sbostic 	date = ctime(&sb.st_ctime);
26037660Sbostic 	if (sb.st_atime == sb.st_ctime)
26137660Sbostic 		(void)printf("New mail received %16.16s.\n", date);
26237660Sbostic 	else if (sb.st_atime > sb.st_ctime) {
26337660Sbostic 		date = ctime(&sb.st_atime);
26437660Sbostic 		(void)printf("No new mail, last read %16.16s.\n", date);
26537660Sbostic 	} else {
26637660Sbostic 		(void)printf("Mail received %16.16s; ", date);
26737660Sbostic 		date = ctime(&sb.st_atime);
26837660Sbostic 		(void)printf("last read %16.16s.\n", date);
26937660Sbostic 	}
27037660Sbostic }
271