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