Home | History | Annotate | Line # | Download | only in finger
lprint.c revision 1.5
      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.5 1996/11/21 06:01:50 lukem 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 <string.h>
     49 #include <time.h>
     50 #include <ctype.h>
     51 #include <paths.h>
     52 #include <vis.h>
     53 #include "finger.h"
     54 #include "extern.h"
     55 
     56 #define	LINE_LEN	80
     57 #define	TAB_LEN		8		/* 8 spaces between tabs */
     58 #define	_PATH_PLAN	".plan"
     59 #define	_PATH_PROJECT	".project"
     60 
     61 void
     62 lflag_print()
     63 {
     64 	PERSON *pn;
     65 
     66 	for (pn = phead;;) {
     67 		lprint(pn);
     68 		if (!pplan) {
     69 			(void)show_text(pn->dir, _PATH_PROJECT, "Project:");
     70 			if (!show_text(pn->dir, _PATH_PLAN, "Plan:"))
     71 				(void)printf("No Plan.\n");
     72 		}
     73 		if (!(pn = pn->next))
     74 			break;
     75 		putchar('\n');
     76 	}
     77 }
     78 
     79 void
     80 lprint(pn)
     81 	PERSON *pn;
     82 {
     83 	struct tm *delta;
     84 	WHERE *w;
     85 	int cpr, len, maxlen;
     86 	struct tm *tp;
     87 	int oddfield;
     88 	char *t, *tzn;
     89 
     90 	cpr = 0;
     91 	/*
     92 	 * long format --
     93 	 *	login name
     94 	 *	real name
     95 	 *	home directory
     96 	 *	shell
     97 	 *	office, office phone, home phone if available
     98 	 *	mail status
     99 	 */
    100 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
    101 	    pn->name, pn->realname, pn->dir);
    102 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
    103 
    104 	/*
    105 	 * try and print office, office phone, and home phone on one line;
    106 	 * if that fails, do line filling so it looks nice.
    107 	 */
    108 #define	OFFICE_TAG		"Office"
    109 #define	OFFICE_PHONE_TAG	"Office Phone"
    110 	oddfield = 0;
    111 	if (pn->office && pn->officephone &&
    112 	    strlen(pn->office) + strlen(pn->officephone) +
    113 	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
    114 		(void)snprintf(tbuf, sizeof(tbuf),
    115 		    "%s: %s, %s", OFFICE_TAG, pn->office,
    116 		    prphone(pn->officephone));
    117 		tbuf[sizeof(tbuf)-1] = '\0';
    118 		oddfield = demi_print(tbuf, oddfield);
    119 	} else {
    120 		if (pn->office) {
    121 			(void)snprintf(tbuf, sizeof(tbuf),
    122 			    "%s: %s", OFFICE_TAG, pn->office);
    123 			tbuf[sizeof(tbuf)-1] = '\0';
    124 			oddfield = demi_print(tbuf, oddfield);
    125 		}
    126 		if (pn->officephone) {
    127 			(void)snprintf(tbuf, sizeof(tbuf),
    128 			    "%s: %s", OFFICE_PHONE_TAG,
    129 			    prphone(pn->officephone));
    130 			tbuf[sizeof(tbuf)-1] = '\0';
    131 			oddfield = demi_print(tbuf, oddfield);
    132 		}
    133 	}
    134 	if (pn->homephone) {
    135 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
    136 		    prphone(pn->homephone));
    137 		tbuf[sizeof(tbuf)-1] = '\0';
    138 		oddfield = demi_print(tbuf, oddfield);
    139 	}
    140 	if (oddfield)
    141 		putchar('\n');
    142 
    143 	/*
    144 	 * long format con't:
    145 	 * if logged in
    146 	 *	terminal
    147 	 *	idle time
    148 	 *	if messages allowed
    149 	 *	where logged in from
    150 	 * if not logged in
    151 	 *	when last logged in
    152 	 */
    153 	/* find out longest device name for this user for formatting */
    154 	for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
    155 		if ((len = strlen(w->tty)) > maxlen)
    156 			maxlen = len;
    157 	/* find rest of entries for user */
    158 	for (w = pn->whead; w != NULL; w = w->next) {
    159 		switch (w->info) {
    160 		case LOGGEDIN:
    161 			tp = localtime(&w->loginat);
    162 			t = asctime(tp);
    163 			tzn = tp->tm_zone;
    164 			cpr = printf("On since %.16s (%s) on %s",
    165 			    t, tzn, w->tty);
    166 			/*
    167 			 * idle time is tough; if have one, print a comma,
    168 			 * then spaces to pad out the device name, then the
    169 			 * idle time.  Follow with a comma if a remote login.
    170 			 */
    171 			delta = gmtime(&w->idletime);
    172 			if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
    173 				cpr += printf("%-*s idle ",
    174 				    maxlen - strlen(w->tty) + 1, ",");
    175 				if (delta->tm_yday > 0) {
    176 					cpr += printf("%d day%s ",
    177 					   delta->tm_yday,
    178 					   delta->tm_yday == 1 ? "" : "s");
    179 				}
    180 				cpr += printf("%d:%02d",
    181 				    delta->tm_hour, delta->tm_min);
    182 				if (*w->host) {
    183 					putchar(',');
    184 					++cpr;
    185 				}
    186 			}
    187 			if (!w->writable)
    188 				cpr += printf(" (messages off)");
    189 			break;
    190 		case LASTLOG:
    191 			if (w->loginat == 0) {
    192 				(void)printf("Never logged in.");
    193 				break;
    194 			}
    195 			tp = localtime(&w->loginat);
    196 			t = asctime(tp);
    197 			tzn = tp->tm_zone;
    198 			if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
    199 				cpr =
    200 				    printf("Last login %.16s %.4s (%s) on %s",
    201 				    t, t + 20, tzn, w->tty);
    202 			else
    203 				cpr = printf("Last login %.16s (%s) on %s",
    204 				    t, tzn, w->tty);
    205 			break;
    206 		}
    207 		if (*w->host) {
    208 			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
    209 				(void)printf("\n   ");
    210 			(void)printf(" from %s", w->host);
    211 		}
    212 		putchar('\n');
    213 	}
    214 	if (pn->mailrecv == -1)
    215 		printf("No Mail.\n");
    216 	else if (pn->mailrecv > pn->mailread) {
    217 		tp = localtime(&pn->mailrecv);
    218 		t = asctime(tp);
    219 		tzn = tp->tm_zone;
    220 		printf("New mail received %.16s %.4s (%s)\n", t, t + 20, tzn);
    221 		tp = localtime(&pn->mailread);
    222 		t = asctime(tp);
    223 		tzn = tp->tm_zone;
    224 		printf("     Unread since %.16s %.4s (%s)\n", t, t + 20, tzn);
    225 	} else {
    226 		tp = localtime(&pn->mailread);
    227 		t = asctime(tp);
    228 		tzn = tp->tm_zone;
    229 		printf("Mail last read %.16s %.4s (%s)\n", t, t + 20, tzn);
    230 	}
    231 }
    232 
    233 int
    234 demi_print(str, oddfield)
    235 	char *str;
    236 	int oddfield;
    237 {
    238 	static int lenlast;
    239 	int lenthis, maxlen;
    240 
    241 	lenthis = strlen(str);
    242 	if (oddfield) {
    243 		/*
    244 		 * We left off on an odd number of fields.  If we haven't
    245 		 * crossed the midpoint of the screen, and we have room for
    246 		 * the next field, print it on the same line; otherwise,
    247 		 * print it on a new line.
    248 		 *
    249 		 * Note: we insist on having the right hand fields start
    250 		 * no less than 5 tabs out.
    251 		 */
    252 		maxlen = 5 * TAB_LEN;
    253 		if (maxlen < lenlast)
    254 			maxlen = lenlast;
    255 		if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
    256 		    lenthis) <= LINE_LEN) {
    257 			while(lenlast < (4 * TAB_LEN)) {
    258 				putchar('\t');
    259 				lenlast += TAB_LEN;
    260 			}
    261 			(void)printf("\t%s\n", str);	/* force one tab */
    262 		} else {
    263 			(void)printf("\n%s", str);	/* go to next line */
    264 			oddfield = !oddfield;	/* this'll be undone below */
    265 		}
    266 	} else
    267 		(void)printf("%s", str);
    268 	oddfield = !oddfield;			/* toggle odd/even marker */
    269 	lenlast = lenthis;
    270 	return(oddfield);
    271 }
    272 
    273 int
    274 show_text(directory, file_name, header)
    275 	char *directory, *file_name, *header;
    276 {
    277 	int ch, lastc;
    278 	FILE *fp;
    279 
    280 	lastc = 0;
    281 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
    282 	tbuf[sizeof(tbuf)-1] = '\0';
    283 	if ((fp = fopen(tbuf, "r")) == NULL)
    284 		return(0);
    285 	(void)printf("%s\n", header);
    286 	while ((ch = getc(fp)) != EOF)
    287 		vputc(lastc = ch);
    288 	if (lastc != '\n')
    289 		(void)putchar('\n');
    290 	(void)fclose(fp);
    291 	return(1);
    292 }
    293 
    294 void
    295 vputc(ch)
    296 	int ch;
    297 {
    298 	char visout[5], *s2;
    299 
    300 	ch = toascii(ch);
    301 	vis(visout, ch, VIS_SAFE|VIS_NOSLASH, 0);
    302 	for (s2 = visout; *s2; s2++)
    303 		(void)putchar(*s2);
    304 }
    305