3 * general utility functions
12 #include <sys/types.h>
17 static void vdie_vprintf(const char *fmt, va_list al) {
20 vfprintf(stderr,fmt,al_copy);
21 die_vprintf_hook(fmt,al);
24 static void vdie_printf(const char *fmt, ...) {
31 void vdie(const char *fmt, int ev, va_list al) {
32 vdie_printf("%s: fatal: ", progname);
34 if (ev) vdie_printf(": %s",strerror(ev));
40 void badusage(const char *why) {
41 fprintf(stderr,"%s: bad usage: %s\n",progname,why); exit(8);
44 void die(const char *fmt, ...)
45 { va_list al; va_start(al,fmt); vdie(fmt,0,al); }
46 void diee(const char *fmt, ...)
47 { va_list al; va_start(al,fmt); vdie(fmt,errno,al); }
49 { diee("malloc failed"); }
51 void *mmalloc(size_t sz) {
59 void *mrealloc(void *o, size_t sz) {
61 if (!sz) { free(o); return 0; }
67 char *mstrdupl(const char *s, int l) {
75 char *mstrdup(const char *s) { return mstrdupl(s,strlen(s)); }
77 void mrename(const char *old, const char *new) {
79 diee("failed to rename `%s' to `%s'", old,new);
82 void mwaitpid(pid_t child, const char *what) {
86 rpid= waitpid(child,&status,0); if (rpid!=child) diee("waitpid");
87 if (WIFEXITED(status)) {
88 int st= WEXITSTATUS(status);
89 if (st) die("%s exited with nonzero status %d",what,st);
90 } else if (WIFSIGNALED(status)) {
91 die("%s died due to %s%s", what, strsignal(WTERMSIG(status)),
92 WCOREDUMP(status) ? " (core dumped)" : "");
94 die("%s failed with unexpected wait status 0x%x", what, status);
98 void mflushstdout(void) {
99 if (ferror(stdout) || fflush(stdout)) diee("write to stdout");