summaryrefslogtreecommitdiff
path: root/dviware/beebe/utils/hd.c
blob: a4bc2a43598bb7d6bb8869f57f5fdbe43972c11e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/***********************************************************************
Dump one or more files to stdout, translating unprintable characters  to
\ooo octal sequences, and  starting newlines at each  ESC, or when  line
length would exceed 72 characters.

Usage:
	hd filelist

Both 7-bit and 8-bit input files are supported, and the output  contains
the file name and its last-write date at the top of the page.

Outside of TOPS-20,  it will  be necessary to  provide (possibly  dummy)
definitions of  fileof()  and timeof(),  which  are intended  to  return
character strings containing the file name and last read or write  time,
given the file pointer.

[15-Aug-87]

***********************************************************************/

#define MAXLINE 72
#define COMMENT '!'		/* marks start of comment to end-of-line */

#include <stdio.h>

char* fileof();
char* timeof();
void  hd();

FILE* tty;


main(argc,argv)
int argc;
char* argv[];
{
    int k;

    tty = stdout;
    for (k = 1; k < argc; ++k)
        hd(argv[k]);
}

void
hd(filename)
char* filename;
{
    register int c;
    register FILE* fp;
    register int hpos;

    if ((fp = fopen(filename,"rb")) == (FILE*)NULL)
        exit(2);

    (void)fprintf(tty,"%c%s [%s]\n",
        COMMENT,fileof(fileno(fp)),timeof(fileno(fp),"w"));

    hpos = 0;
    while ((c = getc(fp)) != EOF)
    {
	if ((c < 040) || (c > 0176) || (c == '\\') || (c == COMMENT))
	{				/* use octal encoding */
	    if (c == 033)
	    {
	        putc('\n',tty);
		hpos = 0;
	    }
	    else if (hpos > (MAXLINE-4))
	    {
	        putc('\n',tty);
		putc('\t',tty);
		hpos = 8;
	    }

	    (void)fprintf(tty,"\\%03o",c);
	    hpos += 4;
	    if (c == '\n')
	    {
	        putc('\n',tty);
		hpos = 0;
	    }
	}
	else				/* printable character */
	{
	    if (hpos > (MAXLINE-1))
	    {
	        putc('\n',tty);
		putc('\t',tty);
		hpos = 8;
	    }
	    putc(c,tty);
	    hpos++;
	}
    }
    putc('\n',tty);
    putc('\f',tty);

    (void)fflush(tty);
    (void)fclose(fp);
}