t5500: test fix
[git/git-svn.git] / stripspace.c
blobdee1ef06edae928c6103fea6b21812de22140a6e
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. Return true if it is an incomplete line.
11 static int 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);
24 return 0;
26 return 1;
29 int main(int argc, char **argv)
31 int empties = -1;
32 int incomplete = 0;
33 char line[1024];
35 while (fgets(line, sizeof(line), stdin)) {
36 incomplete = cleanup(line);
38 /* Not just an empty line? */
39 if (line[0] != '\n') {
40 if (empties > 0)
41 putchar('\n');
42 empties = 0;
43 fputs(line, stdout);
44 continue;
46 if (empties < 0)
47 continue;
48 empties++;
50 if (incomplete)
51 putchar('\n');
52 return 0;