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