Home | History | Annotate | Line # | Download | only in finger
lprint.c revision 1.1
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1989 The Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * This code is derived from software contributed to Berkeley by
      6  1.1  cgd  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
      7  1.1  cgd  *
      8  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1  cgd  * modification, are permitted provided that the following conditions
     10  1.1  cgd  * are met:
     11  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1  cgd  *    must display the following acknowledgement:
     18  1.1  cgd  *	This product includes software developed by the University of
     19  1.1  cgd  *	California, Berkeley and its contributors.
     20  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  cgd  *    may be used to endorse or promote products derived from this software
     22  1.1  cgd  *    without specific prior written permission.
     23  1.1  cgd  *
     24  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  cgd  * SUCH DAMAGE.
     35  1.1  cgd  */
     36  1.1  cgd 
     37  1.1  cgd #ifndef lint
     38  1.1  cgd static char sccsid[] = "@(#)lprint.c	5.13 (Berkeley) 10/31/90";
     39  1.1  cgd #endif /* not lint */
     40  1.1  cgd 
     41  1.1  cgd #include <sys/types.h>
     42  1.1  cgd #include <sys/file.h>
     43  1.1  cgd #include <sys/stat.h>
     44  1.1  cgd #include <sys/time.h>
     45  1.1  cgd #include <tzfile.h>
     46  1.1  cgd #include <stdio.h>
     47  1.1  cgd #include <ctype.h>
     48  1.1  cgd #include <paths.h>
     49  1.1  cgd #include "finger.h"
     50  1.1  cgd 
     51  1.1  cgd #define	LINE_LEN	80
     52  1.1  cgd #define	TAB_LEN		8		/* 8 spaces between tabs */
     53  1.1  cgd #define	_PATH_PLAN	".plan"
     54  1.1  cgd #define	_PATH_PROJECT	".project"
     55  1.1  cgd 
     56  1.1  cgd lflag_print()
     57  1.1  cgd {
     58  1.1  cgd 	extern int pplan;
     59  1.1  cgd 	register PERSON *pn;
     60  1.1  cgd 
     61  1.1  cgd 	for (pn = phead;;) {
     62  1.1  cgd 		lprint(pn);
     63  1.1  cgd 		if (!pplan) {
     64  1.1  cgd 			(void)show_text(pn->dir, _PATH_PROJECT, "Project:");
     65  1.1  cgd 			if (!show_text(pn->dir, _PATH_PLAN, "Plan:"))
     66  1.1  cgd 				(void)printf("No Plan.\n");
     67  1.1  cgd 		}
     68  1.1  cgd 		if (!(pn = pn->next))
     69  1.1  cgd 			break;
     70  1.1  cgd 		putchar('\n');
     71  1.1  cgd 	}
     72  1.1  cgd }
     73  1.1  cgd 
     74  1.1  cgd lprint(pn)
     75  1.1  cgd 	register PERSON *pn;
     76  1.1  cgd {
     77  1.1  cgd 	extern time_t now;
     78  1.1  cgd 	register struct tm *delta;
     79  1.1  cgd 	register WHERE *w;
     80  1.1  cgd 	register int cpr, len, maxlen;
     81  1.1  cgd 	struct tm *tp;
     82  1.1  cgd 	int oddfield;
     83  1.1  cgd 	time_t time();
     84  1.1  cgd 	char *t, *tzn, *prphone();
     85  1.1  cgd 
     86  1.1  cgd 	/*
     87  1.1  cgd 	 * long format --
     88  1.1  cgd 	 *	login name
     89  1.1  cgd 	 *	real name
     90  1.1  cgd 	 *	home directory
     91  1.1  cgd 	 *	shell
     92  1.1  cgd 	 *	office, office phone, home phone if available
     93  1.1  cgd 	 */
     94  1.1  cgd 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
     95  1.1  cgd 	    pn->name, pn->realname, pn->dir);
     96  1.1  cgd 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
     97  1.1  cgd 
     98  1.1  cgd 	/*
     99  1.1  cgd 	 * try and print office, office phone, and home phone on one line;
    100  1.1  cgd 	 * if that fails, do line filling so it looks nice.
    101  1.1  cgd 	 */
    102  1.1  cgd #define	OFFICE_TAG		"Office"
    103  1.1  cgd #define	OFFICE_PHONE_TAG	"Office Phone"
    104  1.1  cgd 	oddfield = 0;
    105  1.1  cgd 	if (pn->office && pn->officephone &&
    106  1.1  cgd 	    strlen(pn->office) + strlen(pn->officephone) +
    107  1.1  cgd 	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
    108  1.1  cgd 		(void)sprintf(tbuf, "%s: %s, %s", OFFICE_TAG, pn->office,
    109  1.1  cgd 		    prphone(pn->officephone));
    110  1.1  cgd 		oddfield = demi_print(tbuf, oddfield);
    111  1.1  cgd 	} else {
    112  1.1  cgd 		if (pn->office) {
    113  1.1  cgd 			(void)sprintf(tbuf, "%s: %s", OFFICE_TAG, pn->office);
    114  1.1  cgd 			oddfield = demi_print(tbuf, oddfield);
    115  1.1  cgd 		}
    116  1.1  cgd 		if (pn->officephone) {
    117  1.1  cgd 			(void)sprintf(tbuf, "%s: %s", OFFICE_PHONE_TAG,
    118  1.1  cgd 			    prphone(pn->officephone));
    119  1.1  cgd 			oddfield = demi_print(tbuf, oddfield);
    120  1.1  cgd 		}
    121  1.1  cgd 	}
    122  1.1  cgd 	if (pn->homephone) {
    123  1.1  cgd 		(void)sprintf(tbuf, "%s: %s", "Home Phone",
    124  1.1  cgd 		    prphone(pn->homephone));
    125  1.1  cgd 		oddfield = demi_print(tbuf, oddfield);
    126  1.1  cgd 	}
    127  1.1  cgd 	if (oddfield)
    128  1.1  cgd 		putchar('\n');
    129  1.1  cgd 
    130  1.1  cgd 	/*
    131  1.1  cgd 	 * long format con't: * if logged in
    132  1.1  cgd 	 *	terminal
    133  1.1  cgd 	 *	idle time
    134  1.1  cgd 	 *	if messages allowed
    135  1.1  cgd 	 *	where logged in from
    136  1.1  cgd 	 * if not logged in
    137  1.1  cgd 	 *	when last logged in
    138  1.1  cgd 	 */
    139  1.1  cgd 	/* find out longest device name for this user for formatting */
    140  1.1  cgd 	for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
    141  1.1  cgd 		if ((len = strlen(w->tty)) > maxlen)
    142  1.1  cgd 			maxlen = len;
    143  1.1  cgd 	/* find rest of entries for user */
    144  1.1  cgd 	for (w = pn->whead; w != NULL; w = w->next) {
    145  1.1  cgd 		switch (w->info) {
    146  1.1  cgd 		case LOGGEDIN:
    147  1.1  cgd 			tp = localtime(&w->loginat);
    148  1.1  cgd 			t = asctime(tp);
    149  1.1  cgd 			tzn = tp->tm_zone;
    150  1.1  cgd 			cpr = printf("On since %.16s (%s) on %s",
    151  1.1  cgd 			    t, tzn, w->tty);
    152  1.1  cgd 			/*
    153  1.1  cgd 			 * idle time is tough; if have one, print a comma,
    154  1.1  cgd 			 * then spaces to pad out the device name, then the
    155  1.1  cgd 			 * idle time.  Follow with a comma if a remote login.
    156  1.1  cgd 			 */
    157  1.1  cgd 			delta = gmtime(&w->idletime);
    158  1.1  cgd 			if (delta->tm_yday || delta->tm_hour || delta->tm_min) {
    159  1.1  cgd 				cpr += printf("%-*s idle ",
    160  1.1  cgd 				    maxlen - strlen(w->tty) + 1, ",");
    161  1.1  cgd 				if (delta->tm_yday > 0) {
    162  1.1  cgd 					cpr += printf("%d day%s ",
    163  1.1  cgd 					   delta->tm_yday,
    164  1.1  cgd 					   delta->tm_yday == 1 ? "" : "s");
    165  1.1  cgd 				}
    166  1.1  cgd 				cpr += printf("%d:%02d",
    167  1.1  cgd 				    delta->tm_hour, delta->tm_min);
    168  1.1  cgd 				if (*w->host) {
    169  1.1  cgd 					putchar(',');
    170  1.1  cgd 					++cpr;
    171  1.1  cgd 				}
    172  1.1  cgd 			}
    173  1.1  cgd 			if (!w->writable)
    174  1.1  cgd 				cpr += printf(" (messages off)");
    175  1.1  cgd 			break;
    176  1.1  cgd 		case LASTLOG:
    177  1.1  cgd 			if (w->loginat == 0) {
    178  1.1  cgd 				(void)printf("Never logged in.");
    179  1.1  cgd 				break;
    180  1.1  cgd 			}
    181  1.1  cgd 			tp = localtime(&w->loginat);
    182  1.1  cgd 			t = asctime(tp);
    183  1.1  cgd 			tzn = tp->tm_zone;
    184  1.1  cgd 			if (now - w->loginat > SECSPERDAY * DAYSPERNYEAR / 2)
    185  1.1  cgd 				cpr =
    186  1.1  cgd 				    printf("Last login %.16s %.4s (%s) on %s",
    187  1.1  cgd 				    t, t + 20, tzn, w->tty);
    188  1.1  cgd 			else
    189  1.1  cgd 				cpr = printf("Last login %.16s (%s) on %s",
    190  1.1  cgd 				    t, tzn, w->tty);
    191  1.1  cgd 			break;
    192  1.1  cgd 		}
    193  1.1  cgd 		if (*w->host) {
    194  1.1  cgd 			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
    195  1.1  cgd 				(void)printf("\n   ");
    196  1.1  cgd 			(void)printf(" from %s", w->host);
    197  1.1  cgd 		}
    198  1.1  cgd 		putchar('\n');
    199  1.1  cgd 	}
    200  1.1  cgd }
    201  1.1  cgd 
    202  1.1  cgd demi_print(str, oddfield)
    203  1.1  cgd 	char *str;
    204  1.1  cgd 	int oddfield;
    205  1.1  cgd {
    206  1.1  cgd 	static int lenlast;
    207  1.1  cgd 	int lenthis, maxlen;
    208  1.1  cgd 
    209  1.1  cgd 	lenthis = strlen(str);
    210  1.1  cgd 	if (oddfield) {
    211  1.1  cgd 		/*
    212  1.1  cgd 		 * We left off on an odd number of fields.  If we haven't
    213  1.1  cgd 		 * crossed the midpoint of the screen, and we have room for
    214  1.1  cgd 		 * the next field, print it on the same line; otherwise,
    215  1.1  cgd 		 * print it on a new line.
    216  1.1  cgd 		 *
    217  1.1  cgd 		 * Note: we insist on having the right hand fields start
    218  1.1  cgd 		 * no less than 5 tabs out.
    219  1.1  cgd 		 */
    220  1.1  cgd 		maxlen = 5 * TAB_LEN;
    221  1.1  cgd 		if (maxlen < lenlast)
    222  1.1  cgd 			maxlen = lenlast;
    223  1.1  cgd 		if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
    224  1.1  cgd 		    lenthis) <= LINE_LEN) {
    225  1.1  cgd 			while(lenlast < (4 * TAB_LEN)) {
    226  1.1  cgd 				putchar('\t');
    227  1.1  cgd 				lenlast += TAB_LEN;
    228  1.1  cgd 			}
    229  1.1  cgd 			(void)printf("\t%s\n", str);	/* force one tab */
    230  1.1  cgd 		} else {
    231  1.1  cgd 			(void)printf("\n%s", str);	/* go to next line */
    232  1.1  cgd 			oddfield = !oddfield;	/* this'll be undone below */
    233  1.1  cgd 		}
    234  1.1  cgd 	} else
    235  1.1  cgd 		(void)printf("%s", str);
    236  1.1  cgd 	oddfield = !oddfield;			/* toggle odd/even marker */
    237  1.1  cgd 	lenlast = lenthis;
    238  1.1  cgd 	return(oddfield);
    239  1.1  cgd }
    240  1.1  cgd 
    241  1.1  cgd show_text(directory, file_name, header)
    242  1.1  cgd 	char *directory, *file_name, *header;
    243  1.1  cgd {
    244  1.1  cgd 	register int ch, lastc;
    245  1.1  cgd 	register FILE *fp;
    246  1.1  cgd 
    247  1.1  cgd 	(void)sprintf(tbuf, "%s/%s", directory, file_name);
    248  1.1  cgd 	if ((fp = fopen(tbuf, "r")) == NULL)
    249  1.1  cgd 		return(0);
    250  1.1  cgd 	(void)printf("%s\n", header);
    251  1.1  cgd 	while ((ch = getc(fp)) != EOF)
    252  1.1  cgd 		vputc(lastc = ch);
    253  1.1  cgd 	if (lastc != '\n')
    254  1.1  cgd 		(void)putchar('\n');
    255  1.1  cgd 	(void)fclose(fp);
    256  1.1  cgd 	return(1);
    257  1.1  cgd }
    258  1.1  cgd 
    259  1.1  cgd vputc(ch)
    260  1.1  cgd 	register int ch;
    261  1.1  cgd {
    262  1.1  cgd 	int meta;
    263  1.1  cgd 
    264  1.1  cgd 	if (!isascii(ch)) {
    265  1.1  cgd 		(void)putchar('M');
    266  1.1  cgd 		(void)putchar('-');
    267  1.1  cgd 		ch = toascii(ch);
    268  1.1  cgd 		meta = 1;
    269  1.1  cgd 	} else
    270  1.1  cgd 		meta = 0;
    271  1.1  cgd 	if (isprint(ch) || !meta && (ch == ' ' || ch == '\t' || ch == '\n'))
    272  1.1  cgd 		(void)putchar(ch);
    273  1.1  cgd 	else {
    274  1.1  cgd 		(void)putchar('^');
    275  1.1  cgd 		(void)putchar(ch == '\177' ? '?' : ch | 0100);
    276  1.1  cgd 	}
    277  1.1  cgd }
    278