diff options
author | Hiltjo Posthuma <[email protected]> | 2022-03-19 12:22:43 +0100 |
---|---|---|
committer | Hiltjo Posthuma <[email protected]> | 2022-03-19 12:51:40 +0100 |
commit | d0e36eb6abce72c587dd53dcabc35120c3cf3a81 (patch) | |
tree | f09f4f56b9851a61de612ab84813e1deebcad7c4 /stagit-index.c | |
parent | 7c419a8bac26e491206953bf2646ac634296b160 (diff) | |
download | stagit-d0e36eb6abce72c587dd53dcabc35120c3cf3a81.tar.gz stagit-d0e36eb6abce72c587dd53dcabc35120c3cf3a81.zip |
improve stream read and write error handling
Diffstat (limited to 'stagit-index.c')
-rw-r--r-- | stagit-index.c | 16 |
1 files changed, 15 insertions, 1 deletions
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, "<stdout>", 'w'); + return ret; } |