Only call fwrite() and fclose() when fp is not NULL

Spotted by Hiltjo.
This commit is contained in:
sin 2014-07-02 12:03:01 +01:00
parent 3669fa4117
commit c9609ea5ff

12
login.c
View File

@ -89,12 +89,14 @@ main(int argc, char *argv[])
usr.ut_tv.tv_sec = time(NULL); usr.ut_tv.tv_sec = time(NULL);
fp = fopen(UTMP_PATH, "a"); fp = fopen(UTMP_PATH, "a");
if (!fp) if (fp) {
if (fwrite(&usr, sizeof(usr), 1, fp) != 1)
if (ferror(fp))
weprintf("%s: write error:", UTMP_PATH);
fclose(fp);
} else {
weprintf("fopen %s:", UTMP_PATH); weprintf("fopen %s:", UTMP_PATH);
if (fwrite(&usr, sizeof(usr), 1, fp) != 1) }
if (ferror(fp))
weprintf("%s: write error:", UTMP_PATH);
fclose(fp);
return dologin(pw, pflag); return dologin(pw, pflag);
} }