Home | History | Annotate | Line # | Download | only in finger
lprint.c revision 1.11
      1 /*	$NetBSD: lprint.c,v 1.11 1998/07/26 15:14:54 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  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 #include <sys/cdefs.h>
     40 #ifndef lint
     41 #if 0
     42 static char sccsid[] = "@(#)lprint.c	8.3 (Berkeley) 4/28/95";
     43 #else
     44 __RCSID( "$NetBSD: lprint.c,v 1.11 1998/07/26 15:14:54 mycroft Exp $");
     45 #endif
     46 #endif /* not lint */
     47 
     48 #include <sys/types.h>
     49 #include <sys/stat.h>
     50 #include <sys/time.h>
     51 #include <fcntl.h>
     52 #include <time.h>
     53 #include <tzfile.h>
     54 #include <db.h>
     55 #include <err.h>
     56 #include <pwd.h>
     57 #include <utmp.h>
     58 #include <errno.h>
     59 #include <unistd.h>
     60 #include <stdio.h>
     61 #include <string.h>
     62 #include <time.h>
     63 #include <ctype.h>
     64 #include <string.h>
     65 #include <paths.h>
     66 #include <vis.h>
     67 
     68 #include "finger.h"
     69 #include "extern.h"
     70 
     71 #define	LINE_LEN	80
     72 #define	TAB_LEN		8		/* 8 spaces between tabs */
     73 #define	_PATH_FORWARD	".forward"
     74 #define	_PATH_PLAN	".plan"
     75 #define	_PATH_PROJECT	".project"
     76 
     77 static int	demi_print __P((char *, int));
     78 static void	lprint __P((PERSON *));
     79 static int	show_text __P((char *, char *, char *));
     80 static void	vputc __P((int));
     81 
     82 void
     83 lflag_print()
     84 {
     85 	PERSON *pn;
     86 	int sflag, r;
     87 	PERSON *tmp;
     88 	DBT data, key;
     89 
     90 	for (sflag = R_FIRST;; sflag = R_NEXT) {
     91 		r = (*db->seq)(db, &key, &data, sflag);
     92 		if (r == -1)
     93 			err(1, "db seq");
     94 		if (r == 1)
     95 			break;
     96 		memmove(&tmp, data.data, sizeof tmp);
     97 		pn = tmp;
     98 		if (sflag != R_FIRST)
     99 			putchar('\n');
    100 		lprint(pn);
    101 		if (!pplan) {
    102 			(void)show_text(pn->dir,
    103 			    _PATH_FORWARD, "Mail forwarded to");
    104 			(void)show_text(pn->dir, _PATH_PROJECT, "Project");
    105 			if (!show_text(pn->dir, _PATH_PLAN, "Plan"))
    106 				(void)printf("No Plan.\n");
    107 		}
    108 	}
    109 }
    110 
    111 static void
    112 lprint(pn)
    113 	PERSON *pn;
    114 {
    115 	struct tm *delta;
    116 	WHERE *w;
    117 	int cpr, len, maxlen;
    118 	struct tm *tp;
    119 	int oddfield;
    120 	char *t;
    121 	const char *tzn;
    122 
    123 	cpr = 0;
    124 	/*
    125 	 * long format --
    126 	 *	login name
    127 	 *	real name
    128 	 *	home directory
    129 	 *	shell
    130 	 *	office, office phone, home phone if available
    131 	 *	mail status
    132 	 */
    133 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
    134 	    pn->name, pn->realname, pn->dir);
    135 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
    136 
    137 	if (gflag)
    138 		goto no_gecos;
    139 	/*
    140 	 * try and print office, office phone, and home phone on one line;
    141 	 * if that fails, do line filling so it looks nice.
    142 	 */
    143 #define	OFFICE_TAG		"Office"
    144 #define	OFFICE_PHONE_TAG	"Office Phone"
    145 	oddfield = 0;
    146 	if (pn->office && pn->officephone &&
    147 	    strlen(pn->office) + strlen(pn->officephone) +
    148 	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
    149 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s",
    150 		    OFFICE_TAG, pn->office, prphone(pn->officephone));
    151 		oddfield = demi_print(tbuf, oddfield);
    152 	} else {
    153 		if (pn->office) {
    154 			(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
    155 			    OFFICE_TAG, pn->office);
    156 			oddfield = demi_print(tbuf, oddfield);
    157 		}
    158 		if (pn->officephone) {
    159 			(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
    160 			    OFFICE_PHONE_TAG, prphone(pn->officephone));
    161 			oddfield = demi_print(tbuf, oddfield);
    162 		}
    163 	}
    164 	if (pn->homephone) {
    165 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
    166 		    prphone(pn->homephone));
    167 		oddfield = demi_print(tbuf, oddfield);
    168 	}
    169 	if (oddfield)
    170 		putchar('\n');
    171 
    172 no_gecos:
    173 	/*
    174 	 * long format con't:
    175 	 * if logged in
    176 	 *	terminal
    177 	 *	idle time
    178 	 *	if messages allowed
    179 	 *	where logged in from
    180 	 * if not logged in
    181 	 *	when last logged in
    182 	 */
    183 	/* find out longest device name for this user for formatting */
    184 	for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
    185 		if ((len = strlen(w->tty)) > maxlen)
    186 			maxlen = len;
    187 	/* find rest of entries for user */
    188 	for (w = pn->whead; w != NULL; w = w->next) {
    189 		switch (w->info) {
    190 		case LOGGEDIN:
    191 			tp = localtime(&w->loginat);
    192 			t = asctime(tp);
    193 			tzn = tp->tm_zone;
    194 			cpr = printf("On since %.16s (%s) on %s",
    195 			    t, tzn, w->tty);
    196 			/*
    197 			 * idle time is tough; if have one, print a comma,
    198 			 * then spaces to pad out the device name, then the
    199 			 * idle time.  Follow with a comma if a remote login.
    200 			 */
    201 			delta = gmtime(&w->idletime);
    202 			if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
    203 				cpr += printf("%-*s idle ",
    204 				    (int)(maxlen - strlen(w->tty) + 1), ",");
    205 				if (delta->tm_yday > 0) {
    206 					cpr += printf("%d day%s ",
    207 					   delta->tm_yday,
    208 					   delta->tm_yday == 1 ? "" : "s");
    209 				}
    210 				cpr += printf("%d:%02d",
    211 				    delta->tm_hour, delta->tm_min);
    212 				if (*w->host) {
    213 					putchar(',');
    214 					++cpr;
    215 				}
    216 			}
    217 			if (!w->writable)
    218 				cpr += printf(" (messages off)");
    219 			break;
    220 		case LASTLOG:
    221 			if (w->loginat == 0) {
    222 				(void)printf("Never logged in.");
    223 				break;
    224 			}
    225 			tp = localtime(&w->loginat);
    226 			t = asctime(tp);
    227 			tzn = tp->tm_zone;
    228 			if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
    229 				cpr =
    230 				    printf("Last login %.16s %.4s (%s) on %s",
    231 				    t, t + 20, tzn, w->tty);
    232 			else
    233 				cpr = printf("Last login %.16s (%s) on %s",
    234 				    t, tzn, w->tty);
    235 			break;
    236 		}
    237 		if (*w->host) {
    238 			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
    239 				(void)printf("\n   ");
    240 			(void)printf(" from %s", w->host);
    241 		}
    242 		putchar('\n');
    243 	}
    244 	if (pn->mailrecv == -1)
    245 		printf("No Mail.\n");
    246 	else if (pn->mailrecv > pn->mailread) {
    247 		tp = localtime(&pn->mailrecv);
    248 		t = asctime(tp);
    249 		tzn = tp->tm_zone;
    250 		printf("New mail received %.16s %.4s (%s)\n", t, t + 20, tzn);
    251 		tp = localtime(&pn->mailread);
    252 		t = asctime(tp);
    253 		tzn = tp->tm_zone;
    254 		printf("     Unread since %.16s %.4s (%s)\n", t, t + 20, tzn);
    255 	} else {
    256 		tp = localtime(&pn->mailread);
    257 		t = asctime(tp);
    258 		tzn = tp->tm_zone;
    259 		printf("Mail last read %.16s %.4s (%s)\n", t, t + 20, tzn);
    260 	}
    261 }
    262 
    263 static int
    264 demi_print(str, oddfield)
    265 	char *str;
    266 	int oddfield;
    267 {
    268 	static int lenlast;
    269 	int lenthis, maxlen;
    270 
    271 	lenthis = strlen(str);
    272 	if (oddfield) {
    273 		/*
    274 		 * We left off on an odd number of fields.  If we haven't
    275 		 * crossed the midpoint of the screen, and we have room for
    276 		 * the next field, print it on the same line; otherwise,
    277 		 * print it on a new line.
    278 		 *
    279 		 * Note: we insist on having the right hand fields start
    280 		 * no less than 5 tabs out.
    281 		 */
    282 		maxlen = 5 * TAB_LEN;
    283 		if (maxlen < lenlast)
    284 			maxlen = lenlast;
    285 		if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
    286 		    lenthis) <= LINE_LEN) {
    287 			while(lenlast < (4 * TAB_LEN)) {
    288 				putchar('\t');
    289 				lenlast += TAB_LEN;
    290 			}
    291 			(void)printf("\t%s\n", str);	/* force one tab */
    292 		} else {
    293 			(void)printf("\n%s", str);	/* go to next line */
    294 			oddfield = !oddfield;	/* this'll be undone below */
    295 		}
    296 	} else
    297 		(void)printf("%s", str);
    298 	oddfield = !oddfield;			/* toggle odd/even marker */
    299 	lenlast = lenthis;
    300 	return(oddfield);
    301 }
    302 
    303 static int
    304 show_text(directory, file_name, header)
    305 	char *directory, *file_name, *header;
    306 {
    307 	struct stat sb;
    308 	FILE *fp;
    309 	int ch, cnt, lastc;
    310 	char *p;
    311 	int fd, nr;
    312 
    313 	lastc = 0;
    314 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
    315 	if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
    316 	    sb.st_size == 0)
    317 		return(0);
    318 
    319 	/* If short enough, and no newlines, show it on a single line.*/
    320 	if (sb.st_size <= LINE_LEN - strlen(header) - 5) {
    321 		nr = read(fd, tbuf, sizeof(tbuf));
    322 		if (nr <= 0) {
    323 			(void)close(fd);
    324 			return(0);
    325 		}
    326 		for (p = tbuf, cnt = nr; cnt--; ++p)
    327 			if (*p == '\n')
    328 				break;
    329 		if (cnt <= 1) {
    330 			(void)printf("%s: ", header);
    331 			for (p = tbuf, cnt = nr; cnt--; ++p)
    332 				vputc(lastc = *p);
    333 			if (lastc != '\n')
    334 				(void)putchar('\n');
    335 			(void)close(fd);
    336 			return(1);
    337 		}
    338 		else
    339 			(void)lseek(fd, 0L, SEEK_SET);
    340 	}
    341 	if ((fp = fdopen(fd, "r")) == NULL)
    342 		return(0);
    343 	(void)printf("%s:\n", header);
    344 	while ((ch = getc(fp)) != EOF)
    345 		vputc(lastc = ch);
    346 	if (lastc != '\n')
    347 		(void)putchar('\n');
    348 	(void)fclose(fp);
    349 	return(1);
    350 }
    351 
    352 static void
    353 vputc(ch)
    354 	int ch;
    355 {
    356 	char visout[5], *s2;
    357 
    358 	ch = toascii(ch);
    359 	vis(visout, ch, VIS_SAFE|VIS_NOSLASH, 0);
    360 	for (s2 = visout; *s2; s2++)
    361 		(void)putchar(*s2);
    362 }
    363