Permalink
Browse files

Fixed a bug in hush

  • Loading branch information...
1 parent f462d5f commit d7ba53b3b12b27fc8cc685bd9be3576152ba739f @thequux thequux committed May 26, 2012
Showing with 17 additions and 7 deletions.
  1. +17 −7 lib/hush.c
View
24 lib/hush.c
@@ -1,17 +1,24 @@
+// -*- c-basic-offset: 8; tab-width: 8 -*-
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
+#include <string.h>
int main (int argc, char** argv) {
// Argv[1] is the message
// argv[2] is the name of the child
// the rest are passed to the child
if (argc < 3) return 1;
printf ("\x1b[1;32m*\x1b[0m %s...", argv[1]);
- char cbuf[4096];
+
+ char *cbuf = malloc(4096);
+ size_t cbuf_cap = 4096;
+ size_t cbuf_len = 0;
+ char cbuf2[4096];
+
argc-=2;
argv+=1;
for (int i = 0; i < argc; i++)
@@ -40,24 +47,27 @@ int main (int argc, char** argv) {
if (cols1) { cols = atoi(cols1); } else { cols = 80; }
close(fd[0]);
int delta = 1;
- int ct = 0;
while (delta != 0) {
- delta = read (fd[1], cbuf+ct, 4096-ct);
- ct+= delta;
+ delta = read (fd[1], cbuf2, 4096);
+ while ((cbuf_len + delta) >= cbuf_cap)
+ cbuf = realloc(cbuf, cbuf_cap *= 2);
+ memcpy(cbuf + cbuf_len, cbuf2, delta);
+ cbuf_len += delta;
}
- cbuf[ct] = 0;
+
int status;
wait(&status);
fflush (NULL);
if (status) {
fprintf (stderr, "\x1b[%dG\x1b[1;34m[\x1b[1;31m!!\x1b[1;34m]\x1b[0m\n", cols-4);
- } else if (ct) {
+ } else if (cbuf_len) {
fprintf (stderr, "\x1b[%dG\x1b[1;34m[\x1b[0;33mWW\x1b[1;34m]\x1b[0m\n", cols-4);
} else {
fprintf (stderr, "\x1b[%dG\x1b[1;34m[\x1b[1;32mOK\x1b[1;34m]\x1b[0m\n", cols-4);
}
fflush (NULL);
- printf ("%s", cbuf);
+ write(2, cbuf, cbuf_len);
+ free(cbuf);
fflush (NULL);
return WEXITSTATUS(status);
}

0 comments on commit d7ba53b

Please sign in to comment.