[tyndur-devel] [PATCH] Shell: Beim cat nicht nur auf EOF sonder auch Fehler pruefen
Antoine Kaufmann ·
! Shell: Beim cat nicht nur auf EOF sonder auch Fehler pruefen Signed-off-by: Antoine Kaufmann <toni at tyndur.org> --- src/modules/c/shell/cmds/cat.c | 12 ++++++++++-- 1 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/modules/c/shell/cmds/cat.c b/src/modules/c/shell/cmds/cat.c index ffa70ce..a8320b4 100644 --- a/src/modules/c/shell/cmds/cat.c +++ b/src/modules/c/shell/cmds/cat.c @@ -49,6 +49,8 @@ void cat_display_usage(void); int main(int argc, char* argv[]) #endif { + int result = EXIT_SUCCESS; + if (argc != 2) { cat_display_usage(); return -1; @@ -59,22 +61,28 @@ void cat_display_usage(void); if (file == NULL) { printf("Konnte '%s' nicht zum lesen oeffnen!\n", path); + result = EXIT_FAILURE; } else { char buffer[BLOCK_SIZE + 1]; size_t size; - while (feof(file) != EOF) { + while (!feof(file) && !ferror(file)) { size = fread(buffer, 1, BLOCK_SIZE, file); buffer[size] = 0; printf("%s", buffer); } + + if (ferror(file)) { + puts("Beim Lesen ist ein Fehler aufgetreten!\n"); + result = EXIT_FAILURE; + } fclose(file); printf("\n"); } - return EXIT_SUCCESS; + return result; } void cat_display_usage() -- 1.6.4.4