Displaying a progress bar is causing an ioctl error when standard out is From: Philip Soltero redirected. Disable the progress bar if this error occurs. --- utils/guest_creator/main.c | 47 +++++++++++++++++++++++++++----------------- 1 files changed, 29 insertions(+), 18 deletions(-) diff --git a/utils/guest_creator/main.c b/utils/guest_creator/main.c index 15b85ce..ced8dd8 100644 --- a/utils/guest_creator/main.c +++ b/utils/guest_creator/main.c @@ -1,6 +1,8 @@ #include "ezxml.h" +#include #include #include +#include #include #include #include @@ -19,6 +21,7 @@ struct file_info { int num_files = 0; struct file_info files[MAX_FILES]; +int silent = 0; #define STDIO_FD 1 @@ -232,63 +235,69 @@ int copy_file(int file_index, FILE * data_file) { int prog_len = 0; double ratio = 100; int num_dots = 256; + int progress_bar = 1; printf("Copying [%d] -- %s \n", file_index, filename); - if (ioctl(STDIO_FD, TIOCGWINSZ, &wsz) == -1) { - printf("ioctl error on STDIO\n"); - return -1; + if (errno == ENOTTY) { + progress_bar = 0; + } else { + printf("ioctl error on STDIO\n"); + return -1; + } } - memset(cons_line, 0, 256); snprintf(cons_line, 256, "\r(%s) [", files[file_index].id); prog_len = wsz.ws_col - (strlen(cons_line) + 11); - - in_file = fopen(filename, "r"); while (bytes_to_read > 0) { struct winsize tmp_wsz; int tmp_dots = 0; - if (ioctl(STDIO_FD, TIOCGWINSZ, &tmp_wsz) == -1) { - printf("ioctl error on STDIO\n"); - return -1; - } - ratio = (double)bytes_read / (double)(files[file_index].size); + if (progress_bar) { + if (ioctl(STDIO_FD, TIOCGWINSZ, &tmp_wsz) == -1) { + printf("ioctl error on STDIO\n"); + return -1; + } + } + + if (progress_bar) { + ratio = (double)bytes_read / (double)(files[file_index].size); tmp_dots = (int)(ratio * (double)prog_len); if ((tmp_dots != num_dots) || (tmp_wsz.ws_col != wsz.ws_col)) { int i = 0; int num_blanks = 0; - + wsz = tmp_wsz; num_dots = tmp_dots; - + num_blanks = prog_len - num_dots; memset(cons_line, 0, 256); snprintf(cons_line, 256, "\r(%s) [", files[file_index].id); - + for (i = 0; i <= num_dots; i++) { strcat(cons_line, "="); } - + for (i = 0; i < num_blanks - 1; i++) { strcat(cons_line, " "); } - + strcat(cons_line, "] "); // printf("console width = %d\n", wsz.ws_col); write(STDIO_FD, cons_line, wsz.ws_col); } + } @@ -311,8 +320,10 @@ int copy_file(int file_index, FILE * data_file) { bytes_to_read -= xfer_len; } - strcat(cons_line, "Done\n"); - write(STDIO_FD, cons_line, wsz.ws_col); + if (progress_bar) { + strcat(cons_line, "Done\n"); + write(STDIO_FD, cons_line, wsz.ws_col); + } fclose(in_file);