"Module":"noerr",
"Title":"noerr - routines for cleaning up without blatting errno",
"Author":"dinesh",
"Dependencies":[
]
"Description":[
{
"str":""
},
{
"str":"It is a good idea to follow the standard C convention of setting errno in"
},
{
"str":"your own helper functions.  Unfortunately, care must be taken in the error"
},
{
"str":"paths as most standard functions can (and do) overwrite errno, even if they"
},
{
"str":"succeed."
},
{
"str":""
},
{
"str":"Example:"
},
{
"str":"	#include <sys/types.h>"
},
{
"str":"	#include <sys/stat.h>"
},
{
"str":"	#include <fcntl.h>"
},
{
"str":""
},
{
"str":"	bool write_string_to_file(const char *file, const char *string)"
},
{
"str":"	{"
},
{
"str":"		int ret, fd = open(file, O_WRONLY|O_CREAT|O_EXCL, 0600);"
},
{
"str":"		if (fd < 0)"
},
{
"str":"			return false;"
},
{
"str":"		ret = write(fd, string, strlen(string));"
},
{
"str":"		if (ret < 0) {"
},
{
"str":"			// Preserve errno from write above."
},
{
"str":"			close_noerr(fd);"
},
{
"str":"			unlink_noerr(file);"
},
{
"str":"			return false;"
},
{
"str":"		}"
},
{
"str":"		if (close(fd) != 0) {"
},
{
"str":"			// Again, preserve errno."
},
{
"str":"			unlink_noerr(file);"
},
{
"str":"			return false;"
},
{
"str":"		}"
},
{
"str":"		// A short write means out of space."
},
{
"str":"		if (ret < strlen(string)) {"
},
{
"str":"			unlink(file);"
},
{
"str":"			errno = ENOSPC;"
},
{
"str":"			return false;"
},
{
"str":"		}"
},
{
"str":"		return true;"
},
{
"str":"	}"
},
]
