xref: /freebsd/usr.bin/finger/lprint.c (revision 5e3934b1)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1989, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
89b50d902SRodney W. Grimes  * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
99b50d902SRodney W. Grimes  *
109b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
119b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
129b50d902SRodney W. Grimes  * are met:
139b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
159b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
169b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
179b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
199b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
209b50d902SRodney W. Grimes  *    without specific prior written permission.
219b50d902SRodney W. Grimes  *
229b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329b50d902SRodney W. Grimes  * SUCH DAMAGE.
339b50d902SRodney W. Grimes  */
349b50d902SRodney W. Grimes 
35724f7602SBruce Evans #include <sys/types.h>
36f4ac32deSDavid Malone #include <sys/socket.h>
379b50d902SRodney W. Grimes #include <sys/stat.h>
38724f7602SBruce Evans #include <sys/time.h>
394c86f3adSPhilippe Charnier #include <ctype.h>
409b50d902SRodney W. Grimes #include <db.h>
41df3f5d9dSPeter Wemm #include <err.h>
424c86f3adSPhilippe Charnier #include <fcntl.h>
43f4d292b7SAndrey A. Chernov #include <langinfo.h>
449b50d902SRodney W. Grimes #include <paths.h>
454c86f3adSPhilippe Charnier #include <pwd.h>
464c86f3adSPhilippe Charnier #include <stdio.h>
474c86f3adSPhilippe Charnier #include <string.h>
484c86f3adSPhilippe Charnier #include <unistd.h>
49550dcc59SEd Schouten #include <utmpx.h>
509b50d902SRodney W. Grimes #include "finger.h"
51040864acSBrian Somers #include "pathnames.h"
529b50d902SRodney W. Grimes 
539b50d902SRodney W. Grimes #define	LINE_LEN	80
549b50d902SRodney W. Grimes #define	TAB_LEN		8		/* 8 spaces between tabs */
559b50d902SRodney W. Grimes 
56f1bb2cd2SWarner Losh static int	demi_print(char *, int);
57f1bb2cd2SWarner Losh static void	lprint(PERSON *);
58f1bb2cd2SWarner Losh static void     vputc(unsigned char);
599b50d902SRodney W. Grimes 
609b50d902SRodney W. Grimes void
lflag_print(void)61f4ac32deSDavid Malone lflag_print(void)
629b50d902SRodney W. Grimes {
634e030ba6SMark Murray 	PERSON *pn;
644e030ba6SMark Murray 	int sflag, r;
65df3f5d9dSPeter Wemm 	PERSON *tmp;
669b50d902SRodney W. Grimes 	DBT data, key;
679b50d902SRodney W. Grimes 
689b50d902SRodney W. Grimes 	for (sflag = R_FIRST;; sflag = R_NEXT) {
699b50d902SRodney W. Grimes 		r = (*db->seq)(db, &key, &data, sflag);
709b50d902SRodney W. Grimes 		if (r == -1)
71df3f5d9dSPeter Wemm 			err(1, "db seq");
729b50d902SRodney W. Grimes 		if (r == 1)
739b50d902SRodney W. Grimes 			break;
74df3f5d9dSPeter Wemm 		memmove(&tmp, data.data, sizeof tmp);
75df3f5d9dSPeter Wemm 		pn = tmp;
769b50d902SRodney W. Grimes 		if (sflag != R_FIRST)
779b50d902SRodney W. Grimes 			putchar('\n');
789b50d902SRodney W. Grimes 		lprint(pn);
799b50d902SRodney W. Grimes 		if (!pplan) {
809b50d902SRodney W. Grimes 			(void)show_text(pn->dir,
819b50d902SRodney W. Grimes 			    _PATH_FORWARD, "Mail forwarded to");
829b50d902SRodney W. Grimes 			(void)show_text(pn->dir, _PATH_PROJECT, "Project");
839b50d902SRodney W. Grimes 			if (!show_text(pn->dir, _PATH_PLAN, "Plan"))
849b50d902SRodney W. Grimes 				(void)printf("No Plan.\n");
85d17485b6SDag-Erling Smørgrav 			(void)show_text(pn->dir,
86a6115eb2SDag-Erling Smørgrav 			    _PATH_PUBKEY, "Public key");
879b50d902SRodney W. Grimes 		}
889b50d902SRodney W. Grimes 	}
899b50d902SRodney W. Grimes }
909b50d902SRodney W. Grimes 
919b50d902SRodney W. Grimes static void
lprint(PERSON * pn)92f4ac32deSDavid Malone lprint(PERSON *pn)
939b50d902SRodney W. Grimes {
944e030ba6SMark Murray 	struct tm *delta;
954e030ba6SMark Murray 	WHERE *w;
964e030ba6SMark Murray 	int cpr, len, maxlen;
979b50d902SRodney W. Grimes 	struct tm *tp;
989b50d902SRodney W. Grimes 	int oddfield;
997a88bad1SAndrey A. Chernov 	char t[80];
1009b50d902SRodney W. Grimes 
101f4d292b7SAndrey A. Chernov 	if (d_first < 0)
102f4d292b7SAndrey A. Chernov 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
1039b50d902SRodney W. Grimes 	/*
1049b50d902SRodney W. Grimes 	 * long format --
1059b50d902SRodney W. Grimes 	 *	login name
1069b50d902SRodney W. Grimes 	 *	real name
1079b50d902SRodney W. Grimes 	 *	home directory
1089b50d902SRodney W. Grimes 	 *	shell
1099b50d902SRodney W. Grimes 	 *	office, office phone, home phone if available
11086641d8fSPaul Traina 	 *	mail status
1119b50d902SRodney W. Grimes 	 */
1129b50d902SRodney W. Grimes 	(void)printf("Login: %-15s\t\t\tName: %s\nDirectory: %-25s",
1139b50d902SRodney W. Grimes 	    pn->name, pn->realname, pn->dir);
1149b50d902SRodney W. Grimes 	(void)printf("\tShell: %-s\n", *pn->shell ? pn->shell : _PATH_BSHELL);
1159b50d902SRodney W. Grimes 
1160830bd13SJonathan Mini 	if (gflag)
1170830bd13SJonathan Mini 		goto no_gecos;
1189b50d902SRodney W. Grimes 	/*
1199b50d902SRodney W. Grimes 	 * try and print office, office phone, and home phone on one line;
1209b50d902SRodney W. Grimes 	 * if that fails, do line filling so it looks nice.
1219b50d902SRodney W. Grimes 	 */
1229b50d902SRodney W. Grimes #define	OFFICE_TAG		"Office"
1239b50d902SRodney W. Grimes #define	OFFICE_PHONE_TAG	"Office Phone"
1249b50d902SRodney W. Grimes 	oddfield = 0;
1259b50d902SRodney W. Grimes 	if (pn->office && pn->officephone &&
1269b50d902SRodney W. Grimes 	    strlen(pn->office) + strlen(pn->officephone) +
1279b50d902SRodney W. Grimes 	    sizeof(OFFICE_TAG) + 2 <= 5 * TAB_LEN) {
1289b50d902SRodney W. Grimes 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s, %s",
1299b50d902SRodney W. Grimes 		    OFFICE_TAG, pn->office, prphone(pn->officephone));
1309b50d902SRodney W. Grimes 		oddfield = demi_print(tbuf, oddfield);
1319b50d902SRodney W. Grimes 	} else {
1329b50d902SRodney W. Grimes 		if (pn->office) {
1339b50d902SRodney W. Grimes 			(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
1349b50d902SRodney W. Grimes 			    OFFICE_TAG, pn->office);
1359b50d902SRodney W. Grimes 			oddfield = demi_print(tbuf, oddfield);
1369b50d902SRodney W. Grimes 		}
1379b50d902SRodney W. Grimes 		if (pn->officephone) {
1389b50d902SRodney W. Grimes 			(void)snprintf(tbuf, sizeof(tbuf), "%s: %s",
1399b50d902SRodney W. Grimes 			    OFFICE_PHONE_TAG, prphone(pn->officephone));
1409b50d902SRodney W. Grimes 			oddfield = demi_print(tbuf, oddfield);
1419b50d902SRodney W. Grimes 		}
1429b50d902SRodney W. Grimes 	}
1439b50d902SRodney W. Grimes 	if (pn->homephone) {
1449b50d902SRodney W. Grimes 		(void)snprintf(tbuf, sizeof(tbuf), "%s: %s", "Home Phone",
1459b50d902SRodney W. Grimes 		    prphone(pn->homephone));
1469b50d902SRodney W. Grimes 		oddfield = demi_print(tbuf, oddfield);
1479b50d902SRodney W. Grimes 	}
1489b50d902SRodney W. Grimes 	if (oddfield)
1499b50d902SRodney W. Grimes 		putchar('\n');
1509b50d902SRodney W. Grimes 
1510830bd13SJonathan Mini no_gecos:
1529b50d902SRodney W. Grimes 	/*
15386641d8fSPaul Traina 	 * long format con't:
15486641d8fSPaul Traina 	 * if logged in
1559b50d902SRodney W. Grimes 	 *	terminal
1569b50d902SRodney W. Grimes 	 *	idle time
1579b50d902SRodney W. Grimes 	 *	if messages allowed
1589b50d902SRodney W. Grimes 	 *	where logged in from
1599b50d902SRodney W. Grimes 	 * if not logged in
1609b50d902SRodney W. Grimes 	 *	when last logged in
1619b50d902SRodney W. Grimes 	 */
1629b50d902SRodney W. Grimes 	/* find out longest device name for this user for formatting */
1639b50d902SRodney W. Grimes 	for (w = pn->whead, maxlen = -1; w != NULL; w = w->next)
1649b50d902SRodney W. Grimes 		if ((len = strlen(w->tty)) > maxlen)
1659b50d902SRodney W. Grimes 			maxlen = len;
1669b50d902SRodney W. Grimes 	/* find rest of entries for user */
1679b50d902SRodney W. Grimes 	for (w = pn->whead; w != NULL; w = w->next) {
1681e832bf8SRuslan Ermilov 		if (w->info == LOGGEDIN) {
1699b50d902SRodney W. Grimes 			tp = localtime(&w->loginat);
170f4d292b7SAndrey A. Chernov 			strftime(t, sizeof(t),
171f4d292b7SAndrey A. Chernov 			    d_first ? "%a %e %b %R (%Z)" : "%a %b %e %R (%Z)",
172f4d292b7SAndrey A. Chernov 			    tp);
173f4d292b7SAndrey A. Chernov 			cpr = printf("On since %s on %s", t, w->tty);
1749b50d902SRodney W. Grimes 			/*
1759b50d902SRodney W. Grimes 			 * idle time is tough; if have one, print a comma,
1769b50d902SRodney W. Grimes 			 * then spaces to pad out the device name, then the
1779b50d902SRodney W. Grimes 			 * idle time.  Follow with a comma if a remote login.
1789b50d902SRodney W. Grimes 			 */
1799b50d902SRodney W. Grimes 			delta = gmtime(&w->idletime);
1807d7d4297SRobert Watson 			if (w->idletime != -1 && (delta->tm_yday ||
1817d7d4297SRobert Watson 			    delta->tm_hour || delta->tm_min)) {
1829b50d902SRodney W. Grimes 				cpr += printf("%-*s idle ",
18377dcd09dSPeter Wemm 				    maxlen - (int)strlen(w->tty) + 1, ",");
1849b50d902SRodney W. Grimes 				if (delta->tm_yday > 0) {
1859b50d902SRodney W. Grimes 					cpr += printf("%d day%s ",
1869b50d902SRodney W. Grimes 					   delta->tm_yday,
1879b50d902SRodney W. Grimes 					   delta->tm_yday == 1 ? "" : "s");
1889b50d902SRodney W. Grimes 				}
1899b50d902SRodney W. Grimes 				cpr += printf("%d:%02d",
1909b50d902SRodney W. Grimes 				    delta->tm_hour, delta->tm_min);
1919b50d902SRodney W. Grimes 				if (*w->host) {
1929b50d902SRodney W. Grimes 					putchar(',');
1939b50d902SRodney W. Grimes 					++cpr;
1949b50d902SRodney W. Grimes 				}
1959b50d902SRodney W. Grimes 			}
1969b50d902SRodney W. Grimes 			if (!w->writable)
1979b50d902SRodney W. Grimes 				cpr += printf(" (messages off)");
1981e832bf8SRuslan Ermilov 		} else if (w->loginat == 0) {
1991e832bf8SRuslan Ermilov 			cpr = printf("Never logged in.");
2001e832bf8SRuslan Ermilov 		} else {
2019b50d902SRodney W. Grimes 			tp = localtime(&w->loginat);
202f4d292b7SAndrey A. Chernov 			if (now - w->loginat > 86400 * 365 / 2) {
203f4d292b7SAndrey A. Chernov 				strftime(t, sizeof(t),
204f4d292b7SAndrey A. Chernov 					 d_first ? "%a %e %b %R %Y (%Z)" :
205f4d292b7SAndrey A. Chernov 						   "%a %b %e %R %Y (%Z)",
206f4d292b7SAndrey A. Chernov 					 tp);
207f4d292b7SAndrey A. Chernov 			} else {
208f4d292b7SAndrey A. Chernov 				strftime(t, sizeof(t),
209f4d292b7SAndrey A. Chernov 					 d_first ? "%a %e %b %R (%Z)" :
210f4d292b7SAndrey A. Chernov 						   "%a %b %e %R (%Z)",
211f4d292b7SAndrey A. Chernov 					 tp);
212f4d292b7SAndrey A. Chernov 			}
213f4d292b7SAndrey A. Chernov 			cpr = printf("Last login %s on %s", t, w->tty);
2149b50d902SRodney W. Grimes 		}
2159b50d902SRodney W. Grimes 		if (*w->host) {
2169b50d902SRodney W. Grimes 			if (LINE_LEN < (cpr + 6 + strlen(w->host)))
2179b50d902SRodney W. Grimes 				(void)printf("\n   ");
2189b50d902SRodney W. Grimes 			(void)printf(" from %s", w->host);
2199b50d902SRodney W. Grimes 		}
2209b50d902SRodney W. Grimes 		putchar('\n');
2219b50d902SRodney W. Grimes 	}
22286641d8fSPaul Traina 	if (pn->mailrecv == -1)
22386641d8fSPaul Traina 		printf("No Mail.\n");
22486641d8fSPaul Traina 	else if (pn->mailrecv > pn->mailread) {
22586641d8fSPaul Traina 		tp = localtime(&pn->mailrecv);
226f4d292b7SAndrey A. Chernov 		strftime(t, sizeof(t),
227f4d292b7SAndrey A. Chernov 			 d_first ? "%a %e %b %R %Y (%Z)" :
228f4d292b7SAndrey A. Chernov 				   "%a %b %e %R %Y (%Z)",
229f4d292b7SAndrey A. Chernov 			 tp);
230f4d292b7SAndrey A. Chernov 		printf("New mail received %s\n", t);
23186641d8fSPaul Traina 		tp = localtime(&pn->mailread);
232f4d292b7SAndrey A. Chernov 		strftime(t, sizeof(t),
233f4d292b7SAndrey A. Chernov 			 d_first ? "%a %e %b %R %Y (%Z)" :
234f4d292b7SAndrey A. Chernov 				   "%a %b %e %R %Y (%Z)",
235f4d292b7SAndrey A. Chernov 			 tp);
236f4d292b7SAndrey A. Chernov 		printf("     Unread since %s\n", t);
23786641d8fSPaul Traina 	} else {
23886641d8fSPaul Traina 		tp = localtime(&pn->mailread);
239f4d292b7SAndrey A. Chernov 		strftime(t, sizeof(t),
240f4d292b7SAndrey A. Chernov 			 d_first ? "%a %e %b %R %Y (%Z)" :
241f4d292b7SAndrey A. Chernov 				   "%a %b %e %R %Y (%Z)",
242f4d292b7SAndrey A. Chernov 			 tp);
243f4d292b7SAndrey A. Chernov 		printf("Mail last read %s\n", t);
24486641d8fSPaul Traina 	}
2459b50d902SRodney W. Grimes }
2469b50d902SRodney W. Grimes 
2479b50d902SRodney W. Grimes static int
demi_print(char * str,int oddfield)248f4ac32deSDavid Malone demi_print(char *str, int oddfield)
2499b50d902SRodney W. Grimes {
2509b50d902SRodney W. Grimes 	static int lenlast;
2519b50d902SRodney W. Grimes 	int lenthis, maxlen;
2529b50d902SRodney W. Grimes 
2539b50d902SRodney W. Grimes 	lenthis = strlen(str);
2549b50d902SRodney W. Grimes 	if (oddfield) {
2559b50d902SRodney W. Grimes 		/*
2569b50d902SRodney W. Grimes 		 * We left off on an odd number of fields.  If we haven't
2579b50d902SRodney W. Grimes 		 * crossed the midpoint of the screen, and we have room for
2589b50d902SRodney W. Grimes 		 * the next field, print it on the same line; otherwise,
2599b50d902SRodney W. Grimes 		 * print it on a new line.
2609b50d902SRodney W. Grimes 		 *
2619b50d902SRodney W. Grimes 		 * Note: we insist on having the right hand fields start
2629b50d902SRodney W. Grimes 		 * no less than 5 tabs out.
2639b50d902SRodney W. Grimes 		 */
2649b50d902SRodney W. Grimes 		maxlen = 5 * TAB_LEN;
2659b50d902SRodney W. Grimes 		if (maxlen < lenlast)
2669b50d902SRodney W. Grimes 			maxlen = lenlast;
2679b50d902SRodney W. Grimes 		if (((((maxlen / TAB_LEN) + 1) * TAB_LEN) +
2689b50d902SRodney W. Grimes 		    lenthis) <= LINE_LEN) {
2699b50d902SRodney W. Grimes 			while(lenlast < (4 * TAB_LEN)) {
2709b50d902SRodney W. Grimes 				putchar('\t');
2719b50d902SRodney W. Grimes 				lenlast += TAB_LEN;
2729b50d902SRodney W. Grimes 			}
2739b50d902SRodney W. Grimes 			(void)printf("\t%s\n", str);	/* force one tab */
2749b50d902SRodney W. Grimes 		} else {
2759b50d902SRodney W. Grimes 			(void)printf("\n%s", str);	/* go to next line */
2769b50d902SRodney W. Grimes 			oddfield = !oddfield;	/* this'll be undone below */
2779b50d902SRodney W. Grimes 		}
2789b50d902SRodney W. Grimes 	} else
2799b50d902SRodney W. Grimes 		(void)printf("%s", str);
2809b50d902SRodney W. Grimes 	oddfield = !oddfield;			/* toggle odd/even marker */
2819b50d902SRodney W. Grimes 	lenlast = lenthis;
2829b50d902SRodney W. Grimes 	return(oddfield);
2839b50d902SRodney W. Grimes }
2849b50d902SRodney W. Grimes 
285040864acSBrian Somers int
show_text(const char * directory,const char * file_name,const char * header)286f4ac32deSDavid Malone show_text(const char *directory, const char *file_name, const char *header)
2879b50d902SRodney W. Grimes {
2889b50d902SRodney W. Grimes 	struct stat sb;
2894e030ba6SMark Murray 	FILE *fp;
2904e030ba6SMark Murray 	int ch, cnt;
2914e030ba6SMark Murray 	char *p, lastc;
2929b50d902SRodney W. Grimes 	int fd, nr;
2939b50d902SRodney W. Grimes 
2941e832bf8SRuslan Ermilov 	lastc = '\0';
2951e832bf8SRuslan Ermilov 
2969b50d902SRodney W. Grimes 	(void)snprintf(tbuf, sizeof(tbuf), "%s/%s", directory, file_name);
2979b50d902SRodney W. Grimes 	if ((fd = open(tbuf, O_RDONLY)) < 0 || fstat(fd, &sb) ||
2989b50d902SRodney W. Grimes 	    sb.st_size == 0)
2999b50d902SRodney W. Grimes 		return(0);
3009b50d902SRodney W. Grimes 
3019b50d902SRodney W. Grimes 	/* If short enough, and no newlines, show it on a single line.*/
302c5d693c5SEd Schouten 	if (sb.st_size <= (off_t)(LINE_LEN - strlen(header) - 5)) {
3039b50d902SRodney W. Grimes 		nr = read(fd, tbuf, sizeof(tbuf));
3049b50d902SRodney W. Grimes 		if (nr <= 0) {
3059b50d902SRodney W. Grimes 			(void)close(fd);
3069b50d902SRodney W. Grimes 			return(0);
3079b50d902SRodney W. Grimes 		}
3089b50d902SRodney W. Grimes 		for (p = tbuf, cnt = nr; cnt--; ++p)
3099b50d902SRodney W. Grimes 			if (*p == '\n')
3109b50d902SRodney W. Grimes 				break;
3119b50d902SRodney W. Grimes 		if (cnt <= 1) {
312040864acSBrian Somers 			if (*header != '\0')
3139b50d902SRodney W. Grimes 				(void)printf("%s: ", header);
3149b50d902SRodney W. Grimes 			for (p = tbuf, cnt = nr; cnt--; ++p)
3153429a304SAndrey A. Chernov 				if (*p != '\r')
3169b50d902SRodney W. Grimes 					vputc(lastc = *p);
3179b50d902SRodney W. Grimes 			if (lastc != '\n')
3189b50d902SRodney W. Grimes 				(void)putchar('\n');
3199b50d902SRodney W. Grimes 			(void)close(fd);
3209b50d902SRodney W. Grimes 			return(1);
3219b50d902SRodney W. Grimes 		}
3229b50d902SRodney W. Grimes 		else
3239b50d902SRodney W. Grimes 			(void)lseek(fd, 0L, SEEK_SET);
3249b50d902SRodney W. Grimes 	}
3259b50d902SRodney W. Grimes 	if ((fp = fdopen(fd, "r")) == NULL)
3269b50d902SRodney W. Grimes 		return(0);
327040864acSBrian Somers 	if (*header != '\0')
3289b50d902SRodney W. Grimes 		(void)printf("%s:\n", header);
3299b50d902SRodney W. Grimes 	while ((ch = getc(fp)) != EOF)
3303429a304SAndrey A. Chernov 		if (ch != '\r')
3319b50d902SRodney W. Grimes 			vputc(lastc = ch);
3329b50d902SRodney W. Grimes 	if (lastc != '\n')
3339b50d902SRodney W. Grimes 		(void)putchar('\n');
3349b50d902SRodney W. Grimes 	(void)fclose(fp);
3359b50d902SRodney W. Grimes 	return(1);
3369b50d902SRodney W. Grimes }
3379b50d902SRodney W. Grimes 
3389b50d902SRodney W. Grimes static void
vputc(unsigned char ch)339f4ac32deSDavid Malone vputc(unsigned char ch)
3409b50d902SRodney W. Grimes {
3419b50d902SRodney W. Grimes 	int meta;
3429b50d902SRodney W. Grimes 
343d40cee33SAndrey A. Chernov 	if (!isprint(ch) && !isascii(ch)) {
3449b50d902SRodney W. Grimes 		(void)putchar('M');
3459b50d902SRodney W. Grimes 		(void)putchar('-');
3469b50d902SRodney W. Grimes 		ch = toascii(ch);
3479b50d902SRodney W. Grimes 		meta = 1;
3489b50d902SRodney W. Grimes 	} else
3499b50d902SRodney W. Grimes 		meta = 0;
3501e832bf8SRuslan Ermilov 	if (isprint(ch) || (!meta && (ch == ' ' || ch == '\t' || ch == '\n')))
3519b50d902SRodney W. Grimes 		(void)putchar(ch);
3529b50d902SRodney W. Grimes 	else {
3539b50d902SRodney W. Grimes 		(void)putchar('^');
3549b50d902SRodney W. Grimes 		(void)putchar(ch == '\177' ? '?' : ch | 0100);
3559b50d902SRodney W. Grimes 	}
3569b50d902SRodney W. Grimes }
357