fix resource leak

When open returns 0 (which is valid), there's a resource leak.
Changing the comparison to include 0 fixes this leak.
Only close fds that were opened

Don't try to close a fd that failed to be opened. Also use
g_close to match up with the g_open.
Argument cannot be negative

1) sysconf returns a long so make bufsize one.
2) sysconf call can fail and return -1, handle that condition.
3) if sysconf does fail a negative value would be sent to calloc
   which expects a positive value.