Generically speaking, cover functions
I'm tired of writing this:
ptr = malloc (sizeof (*ptr)); if (ptr == NULL) { fprintf (stderr, "%s: unable to malloc() %d bytes, errno %d (%s)\n", progname, sizeof (*ptr), errno, strerror (errno)); // and optionally writing it to syslog() too exit (EXIT_FAILURE); }
Over, and over, and over again. So, I wrote a little macro cover function:
#define emalloc(n) emallocWORKER (n,__LINE__,__FILE__); extern void *emallocWORKER (size_t size, int lineNumber, const char *sourceFile);
And then the actual function itself:
extern char *progname; /* defined by user */ void * emallocWORKER (size_t size, int lineNumber, const char *sourceFile) { void *ptr; fatal (!(ptr = malloc (size)), "unable to allocate %d bytes (module %s, line %d)\n", progname, size, sourceFile, lineNumber); return (ptr); }
It's damn useful to put in the __FILE__ and __LINE__ manifests!
Others
I do the same thing for ecalloc(), erealloc(), eopen(), efopen(), etc. Whenever I find myself doing a check against a success and then printing a message and exiting is a good candidate for this.