Home | History | Annotate | Line # | Download | only in finger
lprint.c revision 1.21
      1 /*	$NetBSD: lprint.c,v 1.21 2006/01/04 01:17:54 perry 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. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)lprint.c	8.3 (Berkeley) 4/28/95";
     39 #else
     40 __RCSID( "$NetBSD: lprint.c,v 1.21 2006/01/04 01:17:54 perry Exp $");
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <sys/types.h>
     45 #include <sys/stat.h>
     46 #include <sys/time.h>
     47 #include <fcntl.h>
     48 #include <tzfile.h>
     49 #include <db.h>
     50 #include <err.h>
     51 #include <pwd.h>
     52 #include <errno.h>
     53 #include <unistd.h>
     54 #include <stdio.h>
     55 #include <string.h>
     56 #include <time.h>
     57 #include <ctype.h>
     58 #include <paths.h>
     59 #include <vis.h>
     60 
     61 #include "utmpentry.h"
     62 #include "finger.h"
     63 #include "extern.h"
     64 
     65 #define	LINE_LEN	80
     66 #define	TAB_LEN		8		/* 8 spaces between tabs */
     67 #define	_PATH_FORWARD	".forward"
     68 #define	_PATH_PLAN	".plan"
     69 #define	_PATH_PROJECT	".project"
     70 
     71 static int	demi_print(char *, int);
     72 static void	lprint(PERSON *);
     73 static int	show_text(char *, char *, char *);
     74 static void	vputc(int);
     75 
     76 #ifdef __SVR4
     77 #define TIMEZONE(a)	tzname[0]
     78 #else
     79 #define TIMEZONE(a)	(a)->tm_zone
     80 #endif
     81 
     82 void
     83 lflag_print(void)
     84 {
     85 	PERSON *pn;
     86 	int sflag, r;
     87 	PERSON *tmp;
     88 	DBT data, key;
     89 
     90 	if (db == NULL)
     91 		return;
     92 
     93 	for (sflag = R_FIRST;; sflag = R_NEXT) {
     94 		r = (*db->seq)(db, &key, &data, sflag);
     95 		if (r == -1)
     96 			err(1, "db seq");
     97 		if (r == 1)
     98 			break;
     99 		memmove(&tmp, data.data, sizeof tmp);
    100 		pn = tmp;
    101 		if (sflag != R_FIRST)
    102 			putchar('\n');
    103 		lprint(pn);
    104 		if (!pplan) {
    105 			(void)show_text(pn->dir,
    106 			    _PATH_FORWARD, "Mail forwarded to");
    107 			(void)show_text(pn->dir, _PATH_PROJECT, "Project");
    108 			if (!show_text(pn->dir, _PATH_PLAN, "Plan"))
    109 				(void)printf("No Plan.\n");
    110 		}
    111 	}
    112 }
    113 
    114 static void
    115 lprint(PERSON *pn)
    116 {
    117 	struct tm *delta;
    118 	WHERE *w;
    119 	int cpr, len, maxlen;
    120 	struct tm *tp;
    121 	int oddfield;
    122 	char *t;
    123 	const char *tzn;
    124 
    125 	cpr = 0;
    126 	/*
    127 	 * long format --
    128 	 *	login name
    129 	 *	real name
    130 	 *	home directory
    131 	 *	shell
    132 	 *	office, office phone, home phone if available
    133 	 *	mail status
    134 	 */
    135 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
    136 	    pn->name, pn->realname, pn->dir);
    137 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
    138 
    139 	if (gflag)
    140 		goto no_gecos;
    141 	/*
    142 	 * try and print office, office phone, and home phone on one line;
    143 	 * if that fails, do line filling so it looks nice.
    144 	 */
    145 #define	OFFICE_TAG		"Office"
    146 #define	OFFICE_PHONE_TAG	"Office Phone"
    147 	oddfield = 0;
    148 	if (pn->office && pn->officephone &&
    149 	    strlen(pn->office) + strlen(pn->officephone) +
    150 	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
    151 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s",
    152 		    OFFICE_TAG, pn->office, prphone(pn->officephone));
    153 		oddfield = demi_print(tbuf, oddfield);
    154 	} else {
    155 		if (pn->office) {
    156 			(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
    157 			    OFFICE_TAG, pn->office);
    158 			oddfield = demi_print(tbuf, oddfield);
    159 		}
    160 		if (pn->officephone) {
    161 			(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
    162 			    OFFICE_PHONE_TAG, prphone(pn->officephone));
    163 			oddfield = demi_print(tbuf, oddfield);
    164 		}
    165 	}
    166 	if (pn->homephone) {
    167 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
    168 		    prphone(pn->homephone));
    169 		oddfield = demi_print(tbuf, oddfield);
    170 	}
    171 	if (oddfield)
    172 		putchar('\n');
    173 
    174 no_gecos:
    175 	/*
    176 	 * long format con't:
    177 	 * if logged in
    178 	 *	terminal
    179 	 *	idle time
    180 	 *	if messages allowed
    181 	 *	where logged in from
    182 	 * if not logged in
    183 	 *	when last logged in
    184 	 */
    185 	/* find out longest device name for this user for formatting */
    186 	for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
    187 		if ((len = strlen(w->tty)) > maxlen)
    188 			maxlen = len;
    189 	/* find rest of entries for user */
    190 	for (w = pn->whead; w != NULL; w = w->next) {
    191 		switch (w->info) {
    192 		case LOGGEDIN:
    193 			tp = localtime(&w->loginat);
    194 			t = asctime(tp);
    195 			tzn = TIMEZONE(tp);
    196 			cpr = printf("On since %.16s (%s) on %s",
    197 			    t, tzn, w->tty);
    198 			/*
    199 			 * idle time is tough; if have one, print a comma,
    200 			 * then spaces to pad out the device name, then the
    201 			 * idle time.  Follow with a comma if a remote login.
    202 			 */
    203 			delta = gmtime(&w->idletime);
    204 			if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
    205 				cpr += printf("%-*s idle ",
    206 				    (int)(maxlen - strlen(w->tty) + 1), ",");
    207 				if (delta->tm_yday > 0) {
    208 					cpr += printf("%d day%s ",
    209 					   delta->tm_yday,
    210 					   delta->tm_yday == 1 ? "" : "s");
    211 				}
    212 				cpr += printf("%d:%02d",
    213 				    delta->tm_hour, delta->tm_min);
    214 				if (*w->host) {
    215 					putchar(',');
    216 					++cpr;
    217 				}
    218 			}
    219 			if (!w->writable)
    220 				cpr += printf(" (messages off)");
    221 			break;
    222 		case LASTLOG:
    223 			if (w->loginat == 0) {
    224 				(void)printf("Never logged in.");
    225 				break;
    226 			}
    227 			tp = localtime(&w->loginat);
    228 			t = asctime(tp);
    229 			tzn = TIMEZONE(tp);
    230 			if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
    231 				cpr =
    232 				    printf("Last login %.16s %.4s (%s) on %s",
    233 				    t, t + 20, tzn, w->tty);
    234 			else
    235 				cpr = printf("Last login %.16s (%s) on %s",
    236 				    t, tzn, w->tty);
    237 			break;
    238 		}
    239 		if (*w->host) {
    240 			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
    241 				(void)printf("\n   ");
    242 			(void)printf(" from %s", w->host);
    243 		}
    244 		putchar('\n');
    245 	}
    246 	if (pn->mailrecv == -1)
    247 		printf("No Mail.\n");
    248 	else if (pn->mailrecv > pn->mailread) {
    249 		tp = localtime(&pn->mailrecv);
    250 		t = asctime(tp);
    251 		tzn = TIMEZONE(tp);
    252 		printf("New mail received %.16s %.4s (%s)\n", t, t + 20, tzn);
    253 		tp = localtime(&pn->mailread);
    254 		t = asctime(tp);
    255 		tzn = TIMEZONE(tp);
    256 		printf("     Unread since %.16s %.4s (%s)\n", t, t + 20, tzn);
    257 	} else {
    258 		tp = localtime(&pn->mailread);
    259 		t = asctime(tp);
    260 		tzn = TIMEZONE(tp);
    261 		printf("Mail last read %.16s %.4s (%s)\n", t, t + 20, tzn);
    262 	}
    263 }
    264 
    265 static int
    266 demi_print(char *str, 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(char *directory, char *file_name, char *header)
    305 {
    306 	struct stat sb;
    307 	FILE *fp;
    308 	int ch, cnt, lastc;
    309 	char *p;
    310 	int fd, nr;
    311 
    312 	lastc = 0;
    313 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
    314 	if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
    315 	    sb.st_size == 0)
    316 		return(0);
    317 
    318 	/* If short enough, and no newlines, show it on a single line.*/
    319 	if (sb.st_size <= LINE_LEN - strlen(header) - 5) {
    320 		nr = read(fd, tbuf, sizeof(tbuf));
    321 		if (nr <= 0) {
    322 			(void)close(fd);
    323 			return(0);
    324 		}
    325 		for (p = tbuf, cnt = nr; cnt--; ++p)
    326 			if (*p == '\n')
    327 				break;
    328 		if (cnt <= 1) {
    329 			(void)printf("%s: ", header);
    330 			for (p = tbuf, cnt = nr; cnt--; ++p)
    331 				vputc(lastc = (unsigned char)*p);
    332 			if (lastc != '\n')
    333 				(void)putchar('\n');
    334 			(void)close(fd);
    335 			return(1);
    336 		}
    337 		else
    338 			(void)lseek(fd, 0L, SEEK_SET);
    339 	}
    340 	if ((fp = fdopen(fd, "r")) == NULL)
    341 		return(0);
    342 	(void)printf("%s:\n", header);
    343 	while ((ch = getc(fp)) != EOF)
    344 		vputc(lastc = ch);
    345 	if (lastc != '\n')
    346 		(void)putchar('\n');
    347 	(void)fclose(fp);
    348 	return(1);
    349 }
    350 
    351 static void
    352 vputc(int ch)
    353 {
    354 	char visout[5], *s2;
    355 
    356 	if (eightflag || isprint(ch) || isspace(ch)) {
    357 	    (void)putchar(ch);
    358 	    return;
    359 	}
    360 	ch = toascii(ch);
    361 	vis(visout, ch, VIS_SAFE|VIS_NOSLASH, 0);
    362 	for (s2 = visout; *s2; s2++)
    363 		(void)putchar(*s2);
    364 }
    365