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