Home | History | Annotate | Line # | Download | only in finger
lprint.c revision 1.14
      1  1.14      tron /*	$NetBSD: lprint.c,v 1.14 2002/08/05 08:04:03 tron Exp $	*/
      2   1.7       tls 
      3   1.1       cgd /*
      4   1.9       mrg  * Copyright (c) 1989, 1993
      5   1.9       mrg  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19   1.1       cgd  *    must display the following acknowledgement:
     20   1.1       cgd  *	This product includes software developed by the University of
     21   1.1       cgd  *	California, Berkeley and its contributors.
     22   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23   1.1       cgd  *    may be used to endorse or promote products derived from this software
     24   1.1       cgd  *    without specific prior written permission.
     25   1.1       cgd  *
     26   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1       cgd  * SUCH DAMAGE.
     37   1.1       cgd  */
     38   1.1       cgd 
     39   1.9       mrg #include <sys/cdefs.h>
     40   1.1       cgd #ifndef lint
     41   1.9       mrg #if 0
     42   1.9       mrg static char sccsid[] = "@(#)lprint.c	8.3 (Berkeley) 4/28/95";
     43   1.9       mrg #else
     44  1.14      tron __RCSID( "$NetBSD: lprint.c,v 1.14 2002/08/05 08:04:03 tron Exp $");
     45   1.9       mrg #endif
     46   1.1       cgd #endif /* not lint */
     47   1.1       cgd 
     48   1.1       cgd #include <sys/types.h>
     49   1.1       cgd #include <sys/stat.h>
     50   1.1       cgd #include <sys/time.h>
     51   1.9       mrg #include <fcntl.h>
     52   1.9       mrg #include <time.h>
     53   1.1       cgd #include <tzfile.h>
     54   1.9       mrg #include <db.h>
     55   1.9       mrg #include <err.h>
     56   1.9       mrg #include <pwd.h>
     57   1.9       mrg #include <errno.h>
     58   1.9       mrg #include <unistd.h>
     59   1.1       cgd #include <stdio.h>
     60   1.4       cgd #include <string.h>
     61   1.5     lukem #include <time.h>
     62   1.1       cgd #include <ctype.h>
     63   1.9       mrg #include <string.h>
     64   1.1       cgd #include <paths.h>
     65   1.5     lukem #include <vis.h>
     66   1.9       mrg 
     67  1.13  christos #include "utmpentry.h"
     68   1.1       cgd #include "finger.h"
     69   1.5     lukem #include "extern.h"
     70   1.1       cgd 
     71   1.1       cgd #define	LINE_LEN	80
     72   1.1       cgd #define	TAB_LEN		8		/* 8 spaces between tabs */
     73   1.9       mrg #define	_PATH_FORWARD	".forward"
     74   1.1       cgd #define	_PATH_PLAN	".plan"
     75   1.1       cgd #define	_PATH_PROJECT	".project"
     76   1.1       cgd 
     77   1.9       mrg static int	demi_print __P((char *, int));
     78   1.9       mrg static void	lprint __P((PERSON *));
     79   1.9       mrg static int	show_text __P((char *, char *, char *));
     80   1.9       mrg static void	vputc __P((int));
     81   1.9       mrg 
     82  1.12  christos #ifdef __SVR4
     83  1.12  christos #define TIMEZONE(a)	tzname[0]
     84  1.12  christos #else
     85  1.12  christos #define TIMEZONE(a)	(a)->tm_zone
     86  1.12  christos #endif
     87  1.12  christos 
     88   1.5     lukem void
     89   1.1       cgd lflag_print()
     90   1.1       cgd {
     91   1.5     lukem 	PERSON *pn;
     92   1.9       mrg 	int sflag, r;
     93   1.9       mrg 	PERSON *tmp;
     94   1.9       mrg 	DBT data, key;
     95  1.14      tron 
     96  1.14      tron 	if (db == NULL)
     97  1.14      tron 		return;
     98   1.9       mrg 
     99   1.9       mrg 	for (sflag = R_FIRST;; sflag = R_NEXT) {
    100   1.9       mrg 		r = (*db->seq)(db, &key, &data, sflag);
    101   1.9       mrg 		if (r == -1)
    102   1.9       mrg 			err(1, "db seq");
    103   1.9       mrg 		if (r == 1)
    104   1.9       mrg 			break;
    105   1.9       mrg 		memmove(&tmp, data.data, sizeof tmp);
    106   1.9       mrg 		pn = tmp;
    107   1.9       mrg 		if (sflag != R_FIRST)
    108   1.9       mrg 			putchar('\n');
    109   1.1       cgd 		lprint(pn);
    110   1.1       cgd 		if (!pplan) {
    111   1.9       mrg 			(void)show_text(pn->dir,
    112   1.9       mrg 			    _PATH_FORWARD, "Mail forwarded to");
    113   1.9       mrg 			(void)show_text(pn->dir, _PATH_PROJECT, "Project");
    114   1.9       mrg 			if (!show_text(pn->dir, _PATH_PLAN, "Plan"))
    115   1.1       cgd 				(void)printf("No Plan.\n");
    116   1.1       cgd 		}
    117   1.1       cgd 	}
    118   1.1       cgd }
    119   1.1       cgd 
    120   1.9       mrg static void
    121   1.1       cgd lprint(pn)
    122   1.5     lukem 	PERSON *pn;
    123   1.1       cgd {
    124   1.5     lukem 	struct tm *delta;
    125   1.5     lukem 	WHERE *w;
    126   1.5     lukem 	int cpr, len, maxlen;
    127   1.1       cgd 	struct tm *tp;
    128   1.1       cgd 	int oddfield;
    129  1.11   mycroft 	char *t;
    130  1.11   mycroft 	const char *tzn;
    131   1.1       cgd 
    132   1.5     lukem 	cpr = 0;
    133   1.1       cgd 	/*
    134   1.1       cgd 	 * long format --
    135   1.1       cgd 	 *	login name
    136   1.1       cgd 	 *	real name
    137   1.1       cgd 	 *	home directory
    138   1.1       cgd 	 *	shell
    139   1.1       cgd 	 *	office, office phone, home phone if available
    140   1.5     lukem 	 *	mail status
    141   1.1       cgd 	 */
    142   1.1       cgd 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
    143   1.1       cgd 	    pn->name, pn->realname, pn->dir);
    144   1.1       cgd 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
    145   1.1       cgd 
    146   1.8       mrg 	if (gflag)
    147   1.8       mrg 		goto no_gecos;
    148   1.1       cgd 	/*
    149   1.1       cgd 	 * try and print office, office phone, and home phone on one line;
    150   1.1       cgd 	 * if that fails, do line filling so it looks nice.
    151   1.1       cgd 	 */
    152   1.1       cgd #define	OFFICE_TAG		"Office"
    153   1.1       cgd #define	OFFICE_PHONE_TAG	"Office Phone"
    154   1.1       cgd 	oddfield = 0;
    155   1.1       cgd 	if (pn->office && pn->officephone &&
    156   1.1       cgd 	    strlen(pn->office) + strlen(pn->officephone) +
    157   1.1       cgd 	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
    158   1.9       mrg 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s",
    159   1.9       mrg 		    OFFICE_TAG, pn->office, prphone(pn->officephone));
    160   1.1       cgd 		oddfield = demi_print(tbuf, oddfield);
    161   1.1       cgd 	} else {
    162   1.1       cgd 		if (pn->office) {
    163   1.9       mrg 			(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
    164   1.9       mrg 			    OFFICE_TAG, pn->office);
    165   1.1       cgd 			oddfield = demi_print(tbuf, oddfield);
    166   1.1       cgd 		}
    167   1.1       cgd 		if (pn->officephone) {
    168   1.9       mrg 			(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
    169   1.9       mrg 			    OFFICE_PHONE_TAG, prphone(pn->officephone));
    170   1.1       cgd 			oddfield = demi_print(tbuf, oddfield);
    171   1.1       cgd 		}
    172   1.1       cgd 	}
    173   1.1       cgd 	if (pn->homephone) {
    174   1.5     lukem 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
    175   1.1       cgd 		    prphone(pn->homephone));
    176   1.1       cgd 		oddfield = demi_print(tbuf, oddfield);
    177   1.1       cgd 	}
    178   1.1       cgd 	if (oddfield)
    179   1.1       cgd 		putchar('\n');
    180   1.1       cgd 
    181   1.8       mrg no_gecos:
    182   1.1       cgd 	/*
    183   1.5     lukem 	 * long format con't:
    184   1.5     lukem 	 * if logged in
    185   1.1       cgd 	 *	terminal
    186   1.1       cgd 	 *	idle time
    187   1.1       cgd 	 *	if messages allowed
    188   1.1       cgd 	 *	where logged in from
    189   1.1       cgd 	 * if not logged in
    190   1.1       cgd 	 *	when last logged in
    191   1.1       cgd 	 */
    192   1.1       cgd 	/* find out longest device name for this user for formatting */
    193   1.1       cgd 	for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
    194   1.1       cgd 		if ((len = strlen(w->tty)) > maxlen)
    195   1.1       cgd 			maxlen = len;
    196   1.1       cgd 	/* find rest of entries for user */
    197   1.1       cgd 	for (w = pn->whead; w != NULL; w = w->next) {
    198   1.1       cgd 		switch (w->info) {
    199   1.1       cgd 		case LOGGEDIN:
    200   1.1       cgd 			tp = localtime(&w->loginat);
    201   1.1       cgd 			t = asctime(tp);
    202  1.12  christos 			tzn = TIMEZONE(tp);
    203   1.1       cgd 			cpr = printf("On since %.16s (%s) on %s",
    204   1.1       cgd 			    t, tzn, w->tty);
    205   1.1       cgd 			/*
    206   1.1       cgd 			 * idle time is tough; if have one, print a comma,
    207   1.1       cgd 			 * then spaces to pad out the device name, then the
    208   1.1       cgd 			 * idle time.  Follow with a comma if a remote login.
    209   1.1       cgd 			 */
    210   1.1       cgd 			delta = gmtime(&w->idletime);
    211   1.1       cgd 			if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
    212   1.1       cgd 				cpr += printf("%-*s idle ",
    213  1.10       mrg 				    (int)(maxlen - strlen(w->tty) + 1), ",");
    214   1.1       cgd 				if (delta->tm_yday > 0) {
    215   1.1       cgd 					cpr += printf("%d day%s ",
    216   1.1       cgd 					   delta->tm_yday,
    217   1.1       cgd 					   delta->tm_yday == 1 ? "" : "s");
    218   1.1       cgd 				}
    219   1.1       cgd 				cpr += printf("%d:%02d",
    220   1.1       cgd 				    delta->tm_hour, delta->tm_min);
    221   1.1       cgd 				if (*w->host) {
    222   1.1       cgd 					putchar(',');
    223   1.1       cgd 					++cpr;
    224   1.1       cgd 				}
    225   1.1       cgd 			}
    226   1.1       cgd 			if (!w->writable)
    227   1.1       cgd 				cpr += printf(" (messages off)");
    228   1.1       cgd 			break;
    229   1.1       cgd 		case LASTLOG:
    230   1.1       cgd 			if (w->loginat == 0) {
    231   1.1       cgd 				(void)printf("Never logged in.");
    232   1.1       cgd 				break;
    233   1.1       cgd 			}
    234   1.1       cgd 			tp = localtime(&w->loginat);
    235   1.1       cgd 			t = asctime(tp);
    236  1.12  christos 			tzn = TIMEZONE(tp);
    237   1.1       cgd 			if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
    238   1.1       cgd 				cpr =
    239   1.1       cgd 				    printf("Last login %.16s %.4s (%s) on %s",
    240   1.1       cgd 				    t, t + 20, tzn, w->tty);
    241   1.1       cgd 			else
    242   1.1       cgd 				cpr = printf("Last login %.16s (%s) on %s",
    243   1.1       cgd 				    t, tzn, w->tty);
    244   1.1       cgd 			break;
    245   1.1       cgd 		}
    246   1.1       cgd 		if (*w->host) {
    247   1.1       cgd 			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
    248   1.1       cgd 				(void)printf("\n   ");
    249   1.1       cgd 			(void)printf(" from %s", w->host);
    250   1.1       cgd 		}
    251   1.1       cgd 		putchar('\n');
    252   1.3    brezak 	}
    253   1.3    brezak 	if (pn->mailrecv == -1)
    254   1.3    brezak 		printf("No Mail.\n");
    255   1.3    brezak 	else if (pn->mailrecv > pn->mailread) {
    256   1.3    brezak 		tp = localtime(&pn->mailrecv);
    257   1.3    brezak 		t = asctime(tp);
    258  1.12  christos 		tzn = TIMEZONE(tp);
    259   1.3    brezak 		printf("New mail received %.16s %.4s (%s)\n", t, t + 20, tzn);
    260   1.3    brezak 		tp = localtime(&pn->mailread);
    261   1.3    brezak 		t = asctime(tp);
    262  1.12  christos 		tzn = TIMEZONE(tp);
    263   1.3    brezak 		printf("     Unread since %.16s %.4s (%s)\n", t, t + 20, tzn);
    264   1.3    brezak 	} else {
    265   1.3    brezak 		tp = localtime(&pn->mailread);
    266   1.3    brezak 		t = asctime(tp);
    267  1.12  christos 		tzn = TIMEZONE(tp);
    268   1.3    brezak 		printf("Mail last read %.16s %.4s (%s)\n", t, t + 20, tzn);
    269   1.1       cgd 	}
    270   1.1       cgd }
    271   1.1       cgd 
    272   1.9       mrg static int
    273   1.1       cgd demi_print(str, oddfield)
    274   1.1       cgd 	char *str;
    275   1.1       cgd 	int oddfield;
    276   1.1       cgd {
    277   1.1       cgd 	static int lenlast;
    278   1.1       cgd 	int lenthis, maxlen;
    279   1.1       cgd 
    280   1.1       cgd 	lenthis = strlen(str);
    281   1.1       cgd 	if (oddfield) {
    282   1.1       cgd 		/*
    283   1.1       cgd 		 * We left off on an odd number of fields.  If we haven't
    284   1.1       cgd 		 * crossed the midpoint of the screen, and we have room for
    285   1.1       cgd 		 * the next field, print it on the same line; otherwise,
    286   1.1       cgd 		 * print it on a new line.
    287   1.1       cgd 		 *
    288   1.1       cgd 		 * Note: we insist on having the right hand fields start
    289   1.1       cgd 		 * no less than 5 tabs out.
    290   1.1       cgd 		 */
    291   1.1       cgd 		maxlen = 5 * TAB_LEN;
    292   1.1       cgd 		if (maxlen < lenlast)
    293   1.1       cgd 			maxlen = lenlast;
    294   1.1       cgd 		if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
    295   1.1       cgd 		    lenthis) <= LINE_LEN) {
    296   1.1       cgd 			while(lenlast < (4 * TAB_LEN)) {
    297   1.1       cgd 				putchar('\t');
    298   1.1       cgd 				lenlast += TAB_LEN;
    299   1.1       cgd 			}
    300   1.1       cgd 			(void)printf("\t%s\n", str);	/* force one tab */
    301   1.1       cgd 		} else {
    302   1.1       cgd 			(void)printf("\n%s", str);	/* go to next line */
    303   1.1       cgd 			oddfield = !oddfield;	/* this'll be undone below */
    304   1.1       cgd 		}
    305   1.1       cgd 	} else
    306   1.1       cgd 		(void)printf("%s", str);
    307   1.1       cgd 	oddfield = !oddfield;			/* toggle odd/even marker */
    308   1.1       cgd 	lenlast = lenthis;
    309   1.1       cgd 	return(oddfield);
    310   1.1       cgd }
    311   1.1       cgd 
    312   1.9       mrg static int
    313   1.1       cgd show_text(directory, file_name, header)
    314   1.1       cgd 	char *directory, *file_name, *header;
    315   1.1       cgd {
    316   1.9       mrg 	struct stat sb;
    317   1.5     lukem 	FILE *fp;
    318   1.9       mrg 	int ch, cnt, lastc;
    319   1.9       mrg 	char *p;
    320   1.9       mrg 	int fd, nr;
    321   1.1       cgd 
    322   1.5     lukem 	lastc = 0;
    323   1.5     lukem 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
    324   1.9       mrg 	if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
    325   1.9       mrg 	    sb.st_size == 0)
    326   1.9       mrg 		return(0);
    327   1.9       mrg 
    328   1.9       mrg 	/* If short enough, and no newlines, show it on a single line.*/
    329   1.9       mrg 	if (sb.st_size <= LINE_LEN - strlen(header) - 5) {
    330   1.9       mrg 		nr = read(fd, tbuf, sizeof(tbuf));
    331   1.9       mrg 		if (nr <= 0) {
    332   1.9       mrg 			(void)close(fd);
    333   1.9       mrg 			return(0);
    334   1.9       mrg 		}
    335   1.9       mrg 		for (p = tbuf, cnt = nr; cnt--; ++p)
    336   1.9       mrg 			if (*p == '\n')
    337   1.9       mrg 				break;
    338   1.9       mrg 		if (cnt <= 1) {
    339   1.9       mrg 			(void)printf("%s: ", header);
    340   1.9       mrg 			for (p = tbuf, cnt = nr; cnt--; ++p)
    341   1.9       mrg 				vputc(lastc = *p);
    342   1.9       mrg 			if (lastc != '\n')
    343   1.9       mrg 				(void)putchar('\n');
    344   1.9       mrg 			(void)close(fd);
    345   1.9       mrg 			return(1);
    346   1.9       mrg 		}
    347   1.9       mrg 		else
    348   1.9       mrg 			(void)lseek(fd, 0L, SEEK_SET);
    349   1.9       mrg 	}
    350   1.9       mrg 	if ((fp = fdopen(fd, "r")) == NULL)
    351   1.1       cgd 		return(0);
    352   1.9       mrg 	(void)printf("%s:\n", header);
    353   1.1       cgd 	while ((ch = getc(fp)) != EOF)
    354   1.1       cgd 		vputc(lastc = ch);
    355   1.1       cgd 	if (lastc != '\n')
    356   1.1       cgd 		(void)putchar('\n');
    357   1.1       cgd 	(void)fclose(fp);
    358   1.1       cgd 	return(1);
    359   1.1       cgd }
    360   1.1       cgd 
    361   1.9       mrg static void
    362   1.1       cgd vputc(ch)
    363   1.5     lukem 	int ch;
    364   1.1       cgd {
    365   1.5     lukem 	char visout[5], *s2;
    366   1.1       cgd 
    367   1.5     lukem 	ch = toascii(ch);
    368   1.5     lukem 	vis(visout, ch, VIS_SAFE|VIS_NOSLASH, 0);
    369   1.5     lukem 	for (s2 = visout; *s2; s2++)
    370   1.5     lukem 		(void)putchar(*s2);
    371   1.1       cgd }
    372