1 /* expand - expand tabs to spaces Author: Terrence W. Holm */
3 /* Usage: expand [ -tab1,tab2,tab3,... ] [ file ... ] */
12 int column
= 0; /* Current column, retained between files */
14 int main(int argc
, char **argv
);
15 void Expand(FILE *f
, int tab_index
, int tabs
[]);
22 int tab_index
= 0; /* Default one tab */
26 tabs
[0] = 8; /* Default tab stop */
28 if (argc
> 1 && argv
[1][0] == '-') {
30 int last_tab_stop
= 0;
32 for (tab_index
= 0; tab_index
< MAX_TABS
; ++tab_index
) {
33 if ((tabs
[tab_index
] = atoi(p
+ 1)) <= last_tab_stop
) {
34 fprintf(stderr
, "Bad tab stop spec\n");
37 last_tab_stop
= tabs
[tab_index
];
39 if ((p
= strchr(p
+ 1, ',')) == NULL
) break;
46 Expand(stdin
, tab_index
, tabs
);
48 for (i
= 1; i
< argc
; ++i
) {
49 if ((f
= fopen(argv
[i
], "r")) == NULL
) {
53 Expand(f
, tab_index
, tabs
);
61 void Expand(f
, tab_index
, tabs
)
70 while ((c
= getc(f
)) != EOF
) {
73 next
= (column
/ tabs
[0] + 1) * tabs
[0];
75 for (i
= 0; i
<= tab_index
&& tabs
[i
] <= column
; ++i
);
86 } while (column
< next
);
91 column
= column
> 0 ? column
- 1 : 0;
92 else if (c
== '\n' || c
== '\r')