POSIX doesn’t define free
to set errno
(although POSIX doesn’t currently forbid it, so an implementation might do so – refer to @ArjunShankar’s answer for more details). But that’s not really relevant to your concern.
The way you’re checking for errors is incorrect. You should check the return value of fputs
, and check if it’s smaller than 0
. If it is, then you can check errno
to find out what caused the failure, but that’s optional (and should be done before calling any further functions).
So, something like this should do the trick :
int result = fputs(buf, somefile);
/* optionally read errno here if result < 0 (before the free call) */
free(buf);
return (result < 0) ? -1 : 0;