stripspace.con commit Merge with gitk. (638ccfd)
   1#include <stdio.h>
   2#include <string.h>
   3#include <ctype.h>
   4
   5/*
   6 * Remove empty lines from the beginning and end.
   7 *
   8 * Turn multiple consecutive empty lines into just one
   9 * empty line.
  10 */
  11static void cleanup(char *line)
  12{
  13        int len = strlen(line);
  14
  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        }
  25}
  26
  27int main(int argc, char **argv)
  28{
  29        int empties = -1;
  30        char line[1024];
  31
  32        while (fgets(line, sizeof(line), stdin)) {
  33                cleanup(line);
  34
  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;
  42                }
  43                if (empties < 0)
  44                        continue;
  45                empties++;
  46        }
  47        return 0;
  48}