]> asedeno.scripts.mit.edu Git - git.git/blobdiff - builtin-stripspace.c
Drop strbuf's 'eof' marker, and make read_line a first class citizen.
[git.git] / builtin-stripspace.c
index 0c970aa945a5ae615a2a7af2b2c0c75fb14f0b8e..1ce284710c77ef260d7f64f561e7acc0bed4a3ca 100644 (file)
@@ -74,19 +74,22 @@ size_t stripspace(char *buffer, size_t length, int skip_comments)
 
 int cmd_stripspace(int argc, const char **argv, const char *prefix)
 {
-       char *buffer;
-       unsigned long size;
+       struct strbuf buf;
+       int strip_comments = 0;
 
-       size = 1024;
-       buffer = xmalloc(size);
-       if (read_pipe(0, &buffer, &size))
+       if (argc > 1 && (!strcmp(argv[1], "-s") ||
+                               !strcmp(argv[1], "--strip-comments")))
+               strip_comments = 1;
+
+       strbuf_init(&buf, 0);
+       if (strbuf_read(&buf, 0, 1024) < 0)
                die("could not read the input");
 
-       size = stripspace(buffer, size, 0);
-       write_or_die(1, buffer, size);
-       if (size)
-               putc('\n', stdout);
+       strbuf_setlen(&buf, stripspace(buf.buf, buf.len, strip_comments));
+       if (buf.len)
+               strbuf_addch(&buf, '\n');
 
-       free(buffer);
+       write_or_die(1, buf.buf, buf.len);
+       strbuf_release(&buf);
        return 0;
 }