This repository has been archived by the owner on Jun 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
entabArgs.c
70 lines (58 loc) · 1.73 KB
/
entabArgs.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
#include <stdlib.h>
#include <stdio.h>
// It is recommended that when testing, the space character is
// replaced with something visible.
int main(int argc, char *argv[]) {
int c, i, col, count, nextTabSize, currentTabSize;
int m = 0, n = 8, cnt;
char k;
/* We must to walk through the arguments, looking for '-' */
while(--argc > 0) {
if((*++argv)[0] == '-') {
/* Count letters after it */
cnt = 1;
while((k = *++argv[0]))
switch(k) {
case 'n':
n = atoi(*(argv+cnt++));
break;
default:
;
}
}
}
count = 0;
col = 0;
while((c = getchar()) != EOF) {
if (c == ' ') {
++count;
} else {
while (count != 0) {
// We need to keep track of what size the tab
// will be when we print it - this will vary
// depending on the distance to the tab stop.
nextTabSize = (m != 0) ? m : n;
if (m != 0)
m = 0;
currentTabSize = nextTabSize - (col % nextTabSize);
if (count >= currentTabSize) {
putchar('\t');
col = 0;
count -= currentTabSize;
} else {
for (i = 0; i < count; ++i) {
putchar(' ');
count -= 1;
++col;
}
}
}
putchar(c);
count = 0;
++col;
}
if (c == '\n')
col = 0;
}
return 0;
}