aboutsummaryrefslogtreecommitdiff
path: root/7.2/main.c
blob: f11256e5b97120e5b378ea4c7ef4a2d658714533 (plain) (blame)
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
#include <stdio.h>
#include <ctype.h>
#include "getch.h"

#define MAXLINE	80
#define OCTLEN	6

int inc(int col, int n);

int main(void)
{
	char c, col, next;

	col = 0;
	while ((c = getch()) != EOF) {
		if (iscntrl(c) || isblank(c)) {
			col = inc(col, OCTLEN);
			printf(" \\%03o ", c);

			if (c == '\n') {
				col = 0;
				putchar(c);
			}
		} else {
			col = inc(col, 1);
			/* ac-
			 * k */ 
			ungetch(next = getch());
			if (col == MAXLINE && isalpha(c) &&
			    isalpha(next)) {
				ungetch(c);
				c = '-';
			}

			putchar(c);
		}
	}

	return 0;
}

int inc(int col, int n)
{
	if (col + n > MAXLINE) {
		putchar('\n');
		return n;
	} else {
		return col + n;
	}
}