From d0e36eb6abce72c587dd53dcabc35120c3cf3a81 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Sat, 19 Mar 2022 12:22:43 +0100 Subject: improve stream read and write error handling --- stagit-index.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'stagit-index.c') diff --git a/stagit-index.c b/stagit-index.c index 7c1f76d..26ef16d 100644 --- a/stagit-index.c +++ b/stagit-index.c @@ -16,6 +16,16 @@ static char description[255] = "Repositories"; static char *name = ""; static char owner[255]; +/* Handle read or write errors for a FILE * stream */ +void +checkfileerror(FILE *fp, const char *name, int mode) +{ + if (mode == 'r' && ferror(fp)) + errx(1, "read error: %s", name); + else if (mode == 'w' && (fflush(fp) || ferror(fp))) + errx(1, "write error: %s", name); +} + void joinpath(char *buf, size_t bufsiz, const char *path, const char *path2) { @@ -214,6 +224,7 @@ main(int argc, char *argv[]) if (fp) { if (!fgets(description, sizeof(description), fp)) description[0] = '\0'; + checkfileerror(fp, "description", 'r'); fclose(fp); } @@ -227,8 +238,9 @@ main(int argc, char *argv[]) if (fp) { if (!fgets(owner, sizeof(owner), fp)) owner[0] = '\0'; - owner[strcspn(owner, "\n")] = '\0'; + checkfileerror(fp, "owner", 'r'); fclose(fp); + owner[strcspn(owner, "\n")] = '\0'; } writelog(stdout); } @@ -238,5 +250,7 @@ main(int argc, char *argv[]) git_repository_free(repo); git_libgit2_shutdown(); + checkfileerror(stdout, "", 'w'); + return ret; } -- cgit v1.2.3