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