Home | History | Annotate | Line # | Download | only in finger
lprint.c revision 1.2
      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.2 1993/08/01 18:16:01 mycroft 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 }
    202 
    203 demi_print(str, oddfield)
    204 	char *str;
    205 	int oddfield;
    206 {
    207 	static int lenlast;
    208 	int lenthis, maxlen;
    209 
    210 	lenthis = strlen(str);
    211 	if (oddfield) {
    212 		/*
    213 		 * We left off on an odd number of fields.  If we haven't
    214 		 * crossed the midpoint of the screen, and we have room for
    215 		 * the next field, print it on the same line; otherwise,
    216 		 * print it on a new line.
    217 		 *
    218 		 * Note: we insist on having the right hand fields start
    219 		 * no less than 5 tabs out.
    220 		 */
    221 		maxlen = 5 * TAB_LEN;
    222 		if (maxlen < lenlast)
    223 			maxlen = lenlast;
    224 		if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
    225 		    lenthis) <= LINE_LEN) {
    226 			while(lenlast < (4 * TAB_LEN)) {
    227 				putchar('\t');
    228 				lenlast += TAB_LEN;
    229 			}
    230 			(void)printf("\t%s\n", str);	/* force one tab */
    231 		} else {
    232 			(void)printf("\n%s", str);	/* go to next line */
    233 			oddfield = !oddfield;	/* this'll be undone below */
    234 		}
    235 	} else
    236 		(void)printf("%s", str);
    237 	oddfield = !oddfield;			/* toggle odd/even marker */
    238 	lenlast = lenthis;
    239 	return(oddfield);
    240 }
    241 
    242 show_text(directory, file_name, header)
    243 	char *directory, *file_name, *header;
    244 {
    245 	register int ch, lastc;
    246 	register FILE *fp;
    247 
    248 	(void)sprintf(tbuf, "%s/%s", directory, file_name);
    249 	if ((fp = fopen(tbuf, "r")) == NULL)
    250 		return(0);
    251 	(void)printf("%s\n", header);
    252 	while ((ch = getc(fp)) != EOF)
    253 		vputc(lastc = ch);
    254 	if (lastc != '\n')
    255 		(void)putchar('\n');
    256 	(void)fclose(fp);
    257 	return(1);
    258 }
    259 
    260 vputc(ch)
    261 	register int ch;
    262 {
    263 	int meta;
    264 
    265 	if (!isascii(ch)) {
    266 		(void)putchar('M');
    267 		(void)putchar('-');
    268 		ch = toascii(ch);
    269 		meta = 1;
    270 	} else
    271 		meta = 0;
    272 	if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n'))
    273 		(void)putchar(ch);
    274 	else {
    275 		(void)putchar('^');
    276 		(void)putchar(ch == '\177' ? '?' : ch | 0100);
    277 	}
    278 }
    279