Home | History | Annotate | Line # | Download | only in finger
lprint.c revision 1.7
      1  1.7      tls /*	$NetBSD: lprint.c,v 1.7 1997/01/09 20:19:22 tls Exp $	*/
      2  1.7      tls 
      3  1.1      cgd /*
      4  1.1      cgd  * Copyright (c) 1989 The Regents of the University of California.
      5  1.1      cgd  * 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.1      cgd #ifndef lint
     40  1.2  mycroft /*static char sccsid[] = "from: @(#)lprint.c	5.13 (Berkeley) 10/31/90";*/
     41  1.7      tls static char rcsid[] = "$NetBSD: lprint.c,v 1.7 1997/01/09 20:19:22 tls Exp $";
     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/file.h>
     46  1.1      cgd #include <sys/stat.h>
     47  1.1      cgd #include <sys/time.h>
     48  1.1      cgd #include <tzfile.h>
     49  1.1      cgd #include <stdio.h>
     50  1.4      cgd #include <string.h>
     51  1.5    lukem #include <time.h>
     52  1.1      cgd #include <ctype.h>
     53  1.1      cgd #include <paths.h>
     54  1.5    lukem #include <vis.h>
     55  1.1      cgd #include "finger.h"
     56  1.5    lukem #include "extern.h"
     57  1.1      cgd 
     58  1.1      cgd #define	LINE_LEN	80
     59  1.1      cgd #define	TAB_LEN		8		/* 8 spaces between tabs */
     60  1.1      cgd #define	_PATH_PLAN	".plan"
     61  1.1      cgd #define	_PATH_PROJECT	".project"
     62  1.1      cgd 
     63  1.5    lukem void
     64  1.1      cgd lflag_print()
     65  1.1      cgd {
     66  1.5    lukem 	PERSON *pn;
     67  1.1      cgd 
     68  1.1      cgd 	for (pn = phead;;) {
     69  1.1      cgd 		lprint(pn);
     70  1.1      cgd 		if (!pplan) {
     71  1.1      cgd 			(void)show_text(pn->dir, _PATH_PROJECT, "Project:");
     72  1.1      cgd 			if (!show_text(pn->dir, _PATH_PLAN, "Plan:"))
     73  1.1      cgd 				(void)printf("No Plan.\n");
     74  1.1      cgd 		}
     75  1.1      cgd 		if (!(pn = pn->next))
     76  1.1      cgd 			break;
     77  1.1      cgd 		putchar('\n');
     78  1.1      cgd 	}
     79  1.1      cgd }
     80  1.1      cgd 
     81  1.5    lukem void
     82  1.1      cgd lprint(pn)
     83  1.5    lukem 	PERSON *pn;
     84  1.1      cgd {
     85  1.5    lukem 	struct tm *delta;
     86  1.5    lukem 	WHERE *w;
     87  1.5    lukem 	int cpr, len, maxlen;
     88  1.1      cgd 	struct tm *tp;
     89  1.1      cgd 	int oddfield;
     90  1.5    lukem 	char *t, *tzn;
     91  1.1      cgd 
     92  1.5    lukem 	cpr = 0;
     93  1.1      cgd 	/*
     94  1.1      cgd 	 * long format --
     95  1.1      cgd 	 *	login name
     96  1.1      cgd 	 *	real name
     97  1.1      cgd 	 *	home directory
     98  1.1      cgd 	 *	shell
     99  1.1      cgd 	 *	office, office phone, home phone if available
    100  1.5    lukem 	 *	mail status
    101  1.1      cgd 	 */
    102  1.1      cgd 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
    103  1.1      cgd 	    pn->name, pn->realname, pn->dir);
    104  1.1      cgd 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
    105  1.1      cgd 
    106  1.1      cgd 	/*
    107  1.1      cgd 	 * try and print office, office phone, and home phone on one line;
    108  1.1      cgd 	 * if that fails, do line filling so it looks nice.
    109  1.1      cgd 	 */
    110  1.1      cgd #define	OFFICE_TAG		"Office"
    111  1.1      cgd #define	OFFICE_PHONE_TAG	"Office Phone"
    112  1.1      cgd 	oddfield = 0;
    113  1.1      cgd 	if (pn->office && pn->officephone &&
    114  1.1      cgd 	    strlen(pn->office) + strlen(pn->officephone) +
    115  1.1      cgd 	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
    116  1.5    lukem 		(void)snprintf(tbuf, sizeof(tbuf),
    117  1.5    lukem 		    "%s: %s, %s", OFFICE_TAG, pn->office,
    118  1.1      cgd 		    prphone(pn->officephone));
    119  1.1      cgd 		oddfield = demi_print(tbuf, oddfield);
    120  1.1      cgd 	} else {
    121  1.1      cgd 		if (pn->office) {
    122  1.5    lukem 			(void)snprintf(tbuf, sizeof(tbuf),
    123  1.5    lukem 			    "%s: %s", OFFICE_TAG, pn->office);
    124  1.1      cgd 			oddfield = demi_print(tbuf, oddfield);
    125  1.1      cgd 		}
    126  1.1      cgd 		if (pn->officephone) {
    127  1.5    lukem 			(void)snprintf(tbuf, sizeof(tbuf),
    128  1.5    lukem 			    "%s: %s", OFFICE_PHONE_TAG,
    129  1.1      cgd 			    prphone(pn->officephone));
    130  1.1      cgd 			oddfield = demi_print(tbuf, oddfield);
    131  1.1      cgd 		}
    132  1.1      cgd 	}
    133  1.1      cgd 	if (pn->homephone) {
    134  1.5    lukem 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
    135  1.1      cgd 		    prphone(pn->homephone));
    136  1.1      cgd 		oddfield = demi_print(tbuf, oddfield);
    137  1.1      cgd 	}
    138  1.1      cgd 	if (oddfield)
    139  1.1      cgd 		putchar('\n');
    140  1.1      cgd 
    141  1.1      cgd 	/*
    142  1.5    lukem 	 * long format con't:
    143  1.5    lukem 	 * if logged in
    144  1.1      cgd 	 *	terminal
    145  1.1      cgd 	 *	idle time
    146  1.1      cgd 	 *	if messages allowed
    147  1.1      cgd 	 *	where logged in from
    148  1.1      cgd 	 * if not logged in
    149  1.1      cgd 	 *	when last logged in
    150  1.1      cgd 	 */
    151  1.1      cgd 	/* find out longest device name for this user for formatting */
    152  1.1      cgd 	for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
    153  1.1      cgd 		if ((len = strlen(w->tty)) > maxlen)
    154  1.1      cgd 			maxlen = len;
    155  1.1      cgd 	/* find rest of entries for user */
    156  1.1      cgd 	for (w = pn->whead; w != NULL; w = w->next) {
    157  1.1      cgd 		switch (w->info) {
    158  1.1      cgd 		case LOGGEDIN:
    159  1.1      cgd 			tp = localtime(&w->loginat);
    160  1.1      cgd 			t = asctime(tp);
    161  1.1      cgd 			tzn = tp->tm_zone;
    162  1.1      cgd 			cpr = printf("On since %.16s (%s) on %s",
    163  1.1      cgd 			    t, tzn, w->tty);
    164  1.1      cgd 			/*
    165  1.1      cgd 			 * idle time is tough; if have one, print a comma,
    166  1.1      cgd 			 * then spaces to pad out the device name, then the
    167  1.1      cgd 			 * idle time.  Follow with a comma if a remote login.
    168  1.1      cgd 			 */
    169  1.1      cgd 			delta = gmtime(&w->idletime);
    170  1.1      cgd 			if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
    171  1.1      cgd 				cpr += printf("%-*s idle ",
    172  1.1      cgd 				    maxlen - strlen(w->tty) + 1, ",");
    173  1.1      cgd 				if (delta->tm_yday > 0) {
    174  1.1      cgd 					cpr += printf("%d day%s ",
    175  1.1      cgd 					   delta->tm_yday,
    176  1.1      cgd 					   delta->tm_yday == 1 ? "" : "s");
    177  1.1      cgd 				}
    178  1.1      cgd 				cpr += printf("%d:%02d",
    179  1.1      cgd 				    delta->tm_hour, delta->tm_min);
    180  1.1      cgd 				if (*w->host) {
    181  1.1      cgd 					putchar(',');
    182  1.1      cgd 					++cpr;
    183  1.1      cgd 				}
    184  1.1      cgd 			}
    185  1.1      cgd 			if (!w->writable)
    186  1.1      cgd 				cpr += printf(" (messages off)");
    187  1.1      cgd 			break;
    188  1.1      cgd 		case LASTLOG:
    189  1.1      cgd 			if (w->loginat == 0) {
    190  1.1      cgd 				(void)printf("Never logged in.");
    191  1.1      cgd 				break;
    192  1.1      cgd 			}
    193  1.1      cgd 			tp = localtime(&w->loginat);
    194  1.1      cgd 			t = asctime(tp);
    195  1.1      cgd 			tzn = tp->tm_zone;
    196  1.1      cgd 			if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
    197  1.1      cgd 				cpr =
    198  1.1      cgd 				    printf("Last login %.16s %.4s (%s) on %s",
    199  1.1      cgd 				    t, t + 20, tzn, w->tty);
    200  1.1      cgd 			else
    201  1.1      cgd 				cpr = printf("Last login %.16s (%s) on %s",
    202  1.1      cgd 				    t, tzn, w->tty);
    203  1.1      cgd 			break;
    204  1.1      cgd 		}
    205  1.1      cgd 		if (*w->host) {
    206  1.1      cgd 			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
    207  1.1      cgd 				(void)printf("\n   ");
    208  1.1      cgd 			(void)printf(" from %s", w->host);
    209  1.1      cgd 		}
    210  1.1      cgd 		putchar('\n');
    211  1.3   brezak 	}
    212  1.3   brezak 	if (pn->mailrecv == -1)
    213  1.3   brezak 		printf("No Mail.\n");
    214  1.3   brezak 	else if (pn->mailrecv > pn->mailread) {
    215  1.3   brezak 		tp = localtime(&pn->mailrecv);
    216  1.3   brezak 		t = asctime(tp);
    217  1.3   brezak 		tzn = tp->tm_zone;
    218  1.3   brezak 		printf("New mail received %.16s %.4s (%s)\n", t, t + 20, tzn);
    219  1.3   brezak 		tp = localtime(&pn->mailread);
    220  1.3   brezak 		t = asctime(tp);
    221  1.3   brezak 		tzn = tp->tm_zone;
    222  1.3   brezak 		printf("     Unread since %.16s %.4s (%s)\n", t, t + 20, tzn);
    223  1.3   brezak 	} else {
    224  1.3   brezak 		tp = localtime(&pn->mailread);
    225  1.3   brezak 		t = asctime(tp);
    226  1.3   brezak 		tzn = tp->tm_zone;
    227  1.3   brezak 		printf("Mail last read %.16s %.4s (%s)\n", t, t + 20, tzn);
    228  1.1      cgd 	}
    229  1.1      cgd }
    230  1.1      cgd 
    231  1.5    lukem int
    232  1.1      cgd demi_print(str, oddfield)
    233  1.1      cgd 	char *str;
    234  1.1      cgd 	int oddfield;
    235  1.1      cgd {
    236  1.1      cgd 	static int lenlast;
    237  1.1      cgd 	int lenthis, maxlen;
    238  1.1      cgd 
    239  1.1      cgd 	lenthis = strlen(str);
    240  1.1      cgd 	if (oddfield) {
    241  1.1      cgd 		/*
    242  1.1      cgd 		 * We left off on an odd number of fields.  If we haven't
    243  1.1      cgd 		 * crossed the midpoint of the screen, and we have room for
    244  1.1      cgd 		 * the next field, print it on the same line; otherwise,
    245  1.1      cgd 		 * print it on a new line.
    246  1.1      cgd 		 *
    247  1.1      cgd 		 * Note: we insist on having the right hand fields start
    248  1.1      cgd 		 * no less than 5 tabs out.
    249  1.1      cgd 		 */
    250  1.1      cgd 		maxlen = 5 * TAB_LEN;
    251  1.1      cgd 		if (maxlen < lenlast)
    252  1.1      cgd 			maxlen = lenlast;
    253  1.1      cgd 		if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
    254  1.1      cgd 		    lenthis) <= LINE_LEN) {
    255  1.1      cgd 			while(lenlast < (4 * TAB_LEN)) {
    256  1.1      cgd 				putchar('\t');
    257  1.1      cgd 				lenlast += TAB_LEN;
    258  1.1      cgd 			}
    259  1.1      cgd 			(void)printf("\t%s\n", str);	/* force one tab */
    260  1.1      cgd 		} else {
    261  1.1      cgd 			(void)printf("\n%s", str);	/* go to next line */
    262  1.1      cgd 			oddfield = !oddfield;	/* this'll be undone below */
    263  1.1      cgd 		}
    264  1.1      cgd 	} else
    265  1.1      cgd 		(void)printf("%s", str);
    266  1.1      cgd 	oddfield = !oddfield;			/* toggle odd/even marker */
    267  1.1      cgd 	lenlast = lenthis;
    268  1.1      cgd 	return(oddfield);
    269  1.1      cgd }
    270  1.1      cgd 
    271  1.5    lukem int
    272  1.1      cgd show_text(directory, file_name, header)
    273  1.1      cgd 	char *directory, *file_name, *header;
    274  1.1      cgd {
    275  1.5    lukem 	int ch, lastc;
    276  1.5    lukem 	FILE *fp;
    277  1.1      cgd 
    278  1.5    lukem 	lastc = 0;
    279  1.5    lukem 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
    280  1.1      cgd 	if ((fp = fopen(tbuf, "r")) == NULL)
    281  1.1      cgd 		return(0);
    282  1.1      cgd 	(void)printf("%s\n", header);
    283  1.1      cgd 	while ((ch = getc(fp)) != EOF)
    284  1.1      cgd 		vputc(lastc = ch);
    285  1.1      cgd 	if (lastc != '\n')
    286  1.1      cgd 		(void)putchar('\n');
    287  1.1      cgd 	(void)fclose(fp);
    288  1.1      cgd 	return(1);
    289  1.1      cgd }
    290  1.1      cgd 
    291  1.5    lukem void
    292  1.1      cgd vputc(ch)
    293  1.5    lukem 	int ch;
    294  1.1      cgd {
    295  1.5    lukem 	char visout[5], *s2;
    296  1.1      cgd 
    297  1.5    lukem 	ch = toascii(ch);
    298  1.5    lukem 	vis(visout, ch, VIS_SAFE|VIS_NOSLASH, 0);
    299  1.5    lukem 	for (s2 = visout; *s2; s2++)
    300  1.5    lukem 		(void)putchar(*s2);
    301  1.1      cgd }
    302