blob: d551f668a4e854b1732808c5600f9478e47b58a5 (
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
|
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
/* The C Programming Language: 2nd Edition
*
* Exercise 5-12: Extend `entab` and `detab` to accept the shorthand
* `entab -m +n` to mean tab stops every n columns, starting at column m.
* Choose convenient (for the user) default behavior.
*/
#define DEFAULTTAB 8
int main(int argc, char *argv[]) {
int column, c, spaces;
int tabstart = (-1);
int tabevery = (-1);
char arg[4];
spaces = column = 0;
if (argc > 1) {
int i = 1;
int j = 0;
while (--argc > 0) {
c = argv[i][j++];
if (c == '-' || c == '+') {
while (isdigit(argv[i][j])) {
arg[j - 1] = argv[i][j];
j++;
}
arg[3] = '\0';
if (c == '-') {
tabstart = atoi(arg);
} else {
tabevery = atoi(arg);
}
j = 0;
} else {
printf("entab: unrecognized argument %s\n", *argv);
}
i++;
}
}
/* Clean up the mess if either argument hasn't been set to something sane */
if (tabstart == -1) {
tabstart = 0;
}
if (tabevery == -1) {
tabevery = DEFAULTTAB;
}
while ((c = getchar()) != EOF) {
column++;
if (c == ' ') {
/* No point in starting our tabs until we're after the offset */
if (column > tabstart) {
spaces++;
if ((column - tabstart) % tabevery == 0) {
if (spaces > 1) {
putchar('\t');
spaces = 0;
}
}
}
} else {
/* output all extra spaces first */
while (spaces > 0) {
putchar(' ');
spaces--;
}
/* reset the counts and argument position on a newline */
if (c == '\n') {
column = 0;
spaces = 0;
}
putchar(c);
}
}
return 0;
}
|