-
Notifications
You must be signed in to change notification settings - Fork 0
/
reprint.c
81 lines (71 loc) · 1.63 KB
/
reprint.c
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
/* Exercise 7-2. Write a program that will print arbitrary input in a sensible
* way. As a minimum, it should print non-graphic characters in octal or
* hexadecimal according to local custom, and break long text lines. */
#include "stdio.h"
#include "ctype.h"
#define BUF_INPUT 10000
#define LINE_STOP 80
struct {
int enable_hex : 1;
int enable_oct : 1;
} option;
void scanargs(int argc, char **argv);
void warning(const char *msg);
int main(int argc, char **argv) {
char input[BUF_INPUT];
int c, i, j;
scanargs(argc, argv);
i = 0;
/* get input */
while ((c = getchar()) != EOF && i < BUF_INPUT)
input[i++] = (char) c;
if (i == BUF_INPUT) {
warning("buffer for input is not enough.");
input[--i] = '\0';
} else
input[i] = '\0';
j = 0;
for (i = 0; input[i] != '\0'; i++) {
if (j == LINE_STOP) {
putchar('\n');
j = 0;
}
if (!isgraph(input[i])) {
if (j <= LINE_STOP - 2) {
printf(option.enable_hex ? "%x" : "%o", input[i]);
j += 2;
} else {
putchar('\n');
printf(option.enable_hex ? "%x" : "%o", input[i]);
j = 4;
}
}
else {
putchar(input[i]);
j++;
}
}
putchar('\n');
return 0;
}
void scanargs(int argc, char **argv) {
option.enable_hex = 1;
while (--argc)
if (**++argv == '-')
while (*++*argv != '\0')
switch (**argv) {
case 'o':
option.enable_hex = 0;
option.enable_oct = 1;
break;
case 'x':
option.enable_hex = 1;
option.enable_oct = 0;
break;
default:
break;
}
}
void warning(const char *msg) {
printf("warning: %s\n", msg);
}