Bugfix: Correctly handle EOF on stdin (Thanks woddf2)

This commit is contained in:
Michael Stapelberg 2011-07-08 23:16:33 +02:00 committed by Axel Wagner
parent 778268c9bb
commit 97827372af
1 changed files with 19 additions and 10 deletions

View File

@ -38,8 +38,12 @@ char *statusline_buffer = NULL;
void cleanup() { void cleanup() {
if (stdin_io != NULL) { if (stdin_io != NULL) {
ev_io_stop(main_loop, stdin_io); ev_io_stop(main_loop, stdin_io);
ev_child_stop(main_loop, child_sig);
FREE(stdin_io); FREE(stdin_io);
FREE(statusline_buffer);
}
if (child_sig != NULL) {
ev_child_stop(main_loop, child_sig);
FREE(child_sig); FREE(child_sig);
FREE(statusline_buffer); FREE(statusline_buffer);
} }
@ -69,18 +73,23 @@ void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (n == 0) { if (n == 0) {
if (rec == buffer_len) { if (rec != 0) {
buffer_len += STDIN_CHUNK_SIZE; /* remove trailing newline and finish up */
buffer = realloc(buffer, buffer_len); buffer[rec-1] = '\0';
} else {
if (rec != 0) {
/* remove trailing newline and finish up */
buffer[rec-1] = '\0';
}
break;
} }
/* end of file, kill the watcher */
DLOG("stdin: EOF\n");
ev_io_stop(loop, watcher);
FREE(stdin_io);
break;
} }
rec += n; rec += n;
if (rec == buffer_len) {
buffer_len += STDIN_CHUNK_SIZE;
buffer = realloc(buffer, buffer_len);
}
} }
if (*buffer == '\0') { if (*buffer == '\0') {
FREE(buffer); FREE(buffer);