Merge branches 'bf/doc' and 'db/tartree'
[git/jnareb-git.git] / stripspace.c
blob96cd0a88f0ba0c06da81d4cdb83a482d82ecdd23
1 #include <stdio.h>
2 #include <string.h>
3 #include <ctype.h>
5 /*
6 * Remove empty lines from the beginning and end.
8 * Turn multiple consecutive empty lines into just one
9 * empty line.
11 static void cleanup(char *line)
13 int len = strlen(line);
15 if (len > 1 && line[len-1] == '\n') {
16 do {
17 unsigned char c = line[len-2];
18 if (!isspace(c))
19 break;
20 line[len-2] = '\n';
21 len--;
22 line[len] = 0;
23 } while (len > 1);
27 int main(int argc, char **argv)
29 int empties = -1;
30 char line[1024];
32 while (fgets(line, sizeof(line), stdin)) {
33 cleanup(line);
35 /* Not just an empty line? */
36 if (line[0] != '\n') {
37 if (empties > 0)
38 putchar('\n');
39 empties = 0;
40 fputs(line, stdout);
41 continue;
43 if (empties < 0)
44 continue;
45 empties++;
47 return 0;