From 4595ada2f69b42670c85a63c7d2344af63f2afe7 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Sun, 29 Dec 2019 01:35:09 -0500 Subject: Minor fixes: * size_t in place of int * Moved some variables closer to their execution scope * Add some error checks --- src/shell.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/shell.c') diff --git a/src/shell.c b/src/shell.c index 6ce3ad2..6b28e64 100644 --- a/src/shell.c +++ b/src/shell.c @@ -32,9 +32,6 @@ void shell(Process **proc_info, u_int64_t option, const char *fmt, ...) { va_list args; va_start(args, fmt); - size_t bytes_read = 0; - size_t i = 0; - size_t new_buf_size = 0; clockid_t clkid = CLOCK_REALTIME; FILE *proc = NULL; @@ -68,6 +65,8 @@ void shell(Process **proc_info, u_int64_t option, const char *fmt, ...) { proc = popen(cmd, "r"); if (!proc) { + free(outbuf); + va_end(args); return; } @@ -81,10 +80,12 @@ void shell(Process **proc_info, u_int64_t option, const char *fmt, ...) { } if (option & SHELL_OUTPUT) { + size_t bytes_read = 0; + size_t i = 0; + size_t new_buf_size; (*proc_info)->output = (char *)calloc(BUFSIZ, sizeof(char)); while ((*outbuf = fgetc(proc)) != EOF) { - if (i >= BUFSIZ) { new_buf_size = BUFSIZ + (i + bytes_read); (*proc_info)->output = (char *)realloc((*proc_info)->output, new_buf_size); -- cgit