-
Notifications
You must be signed in to change notification settings - Fork 0
/
UNTAB.C
74 lines (57 loc) · 1.37 KB
/
UNTAB.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
71
72
73
74
#include <stdio.h>
void untabify_str(char *str, FILE *out)
{
/* like fputs, but convert tabs to spaces */
int col = 0;
char *s;
s = str;
while (s[0]) {
switch(s[0]) {
case '\t':
fputc(' ', out);
col++;
while (col%8) {
fputc(' ', out);
col++;
}
break;
default:
fputc(s[0], out);
col++;
}
s++;
}
/* fputc('\n', out); /* newline at end */
}
void untabify_file(FILE *in, FILE *out)
{
char str[80];
/* very simple function to read strings with fgets and print
them with untabify_str */
while (fgets(str, 80, in)) {
untabify_str(str, out);
}
}
int main(int argc, char **argv)
{
int i;
FILE *pfile;
/* untabify each file on the command line */
for (i = 1; i < argc; i++) {
pfile = fopen(argv[i], "r");
if (pfile == NULL) {
fputs("cannot open file: ", stderr);
fputs(argv[i], stderr);
fputc('\n', stderr);
}
else {
untabify_file(pfile, stdout);
fclose(pfile);
}
}
/* if no files, read from stdin */
if (argc == 1) {
untabify_file(stdin, stdout);
}
return 0;
}