1 /* unexpand - convert spaces to tabs Author: Terrence W. Holm */
3 /* Usage: unexpand [ -a ] [ file ... ] */
12 int column
= 0; /* Current column, retained between files */
13 int spaces
= 0; /* Spaces since last tab stop */
14 int leading_blank
= 1; /* Only unexpand leading blanks, */
15 /* Overruled by -a option */
17 _PROTOTYPE(int main
, (int argc
, char **argv
));
18 _PROTOTYPE(void Unexpand
, (FILE *f
, int all
));
25 int all
= 0; /* -a flag means unexpand all spaces */
29 if (argc
> 1 && argv
[1][0] == '-') {
30 if (strcmp(argv
[1], "-a") == 0)
33 fprintf(stderr
, "Usage: unexpand [ -a ] [ file ... ]\n");
43 for (i
= 1; i
< argc
; ++i
) {
44 if ((f
= fopen(argv
[i
], "r")) == NULL
) {
53 /* If there are pending spaces print them. */
70 while ((c
= getc(f
)) != EOF
) {
71 if (c
== ' ' && (all
|| leading_blank
)) {
75 /* If we have white space up to a tab stop, then output */
76 /* A tab. If only one space is required, use a ' '. */
78 if (column
% TAB
== 0) {
89 /* If a tab character is encountered in the input then */
90 /* Simply echo it. Any accumulated spaces can only be */
91 /* Since the last tab stop, so ignore them. */
93 column
= (column
/ TAB
+ 1) * TAB
;
99 /* A non-space character is to be printed. If there */
100 /* Are pending spaces, then print them. There will be */
101 /* At most TAB-1 spaces to print. */
107 if (c
== '\n' || c
== '\r') {
114 column
= column
> 0 ? column
- 1 : 0;