wrapper.c: add and use fopen_or_warn()

When fopen() returns NULL, it could be because the given path does not
exist, but it could also be some other errors and the caller has to
check. Add a wrapper so we don't have to repeat the same error check
everywhere.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Nguyễn Thái Ngọc Duy
2017-05-03 17:16:50 +07:00
committed by Junio C Hamano
parent 11dc1fcb3f
commit e9d983f116
15 changed files with 43 additions and 26 deletions

View File

@@ -72,12 +72,10 @@ static int add_mailname_host(struct strbuf *buf)
FILE *mailname;
struct strbuf mailnamebuf = STRBUF_INIT;
mailname = fopen("/etc/mailname", "r");
if (!mailname) {
if (errno != ENOENT)
warning_errno("cannot open /etc/mailname");
mailname = fopen_or_warn("/etc/mailname", "r");
if (!mailname)
return -1;
}
if (strbuf_getline(&mailnamebuf, mailname) == EOF) {
if (ferror(mailname))
warning_errno("cannot read /etc/mailname");