Whether there's sufficient space left is normally not a concern of the directory but of the whole file system. There are things like quotas, which are normally supported by default by any distributor-made standard kernel but which have to be activated first.
So, I guess a call to statvfs() should do it.
[...]On success, the number of bytes written are returned (zero indicates nothing was written). On error, -1 is returned, and errno is set appropriately. If count is zero and the file descriptor refers to a regular file, 0 may be returned, or an error could be detected. For a special file, the results are not portable.
An attempt was made to write a file that exceeds the implementation-defined maximum file size or the process' file size limit, or to write at a position past the maximum allowed offset.
The device containing the file referred to by fd has no room for the data.