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