blob: 457d465b6fc5bf16e90759af4e9c75097e80a67e (
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
|
#include <stdio.h>
/* The C Programming Language: 2nd Edition
*
* Exercise 5-11: Modify the programs entab and detab (written as exercises in
* Chapter 1) to accept a list of tab stops as arguments. Use the default tab
* settings if there are no arguments.
*/
#define TABWIDTH 8
int main(int argc, char *argv[]) {
int column, c, tabnum, stop;
column = 0;
if (argc > 1) {
tabnum = 1;
stop = atoi(argv[tabnum]);
} else {
tabnum = 0;
}
while ((c = getchar()) != EOF) {
if (c == '\t') {
if (argc > 1) {
// advance the argument if we're ahead of the last one
if (column > stop && tabnum < (argc - 1)) {
stop = atoi(argv[++tabnum]);
}
// insert our spaces up to the tabstop
while (column <= stop) {
putchar(' ');
column++;
}
// advance the argument (again) if needed.
if (tabnum < (argc - 1)) {
stop = atoi(argv[++tabnum]);
}
} else {
// default tabstopping
while (column % TABWIDTH != 1) {
putchar(' ');
column++;
}
}
} else {
// reset counters and the arglist
if (c == '\n') {
column = 0;
if (tabnum > 0) {
tabnum = 1;
stop = atoi(argv[tabnum]);
}
}
putchar(c);
column++;
}
}
return 0;
}
|