]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - include/c.h
build-sys: provide alternatives for err, errx, warn and warnx
[thirdparty/util-linux.git] / include / c.h
index 81546388151708bbb7cc776fbed925855282ce8f..0db8b2b59053fff229ddd99c64b1868437d885e2 100644 (file)
@@ -6,6 +6,15 @@
 #define UTIL_LINUX_C_H
 
 #include <limits.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <string.h>
+#include <errno.h>
+
+#ifdef HAVE_ERR_H
+# include <err.h>
+#endif
 
 /*
  * Compiler specific stuff
 #endif
 
 
+#ifndef HAVE_ERR_H
+static inline void
+errmsg(char doexit, int excode, char adderr, const char *fmt, ...)
+{
+       fprintf(stderr, "%s: ", program_invocation_short_name);
+       if (fmt != NULL) {
+               va_list argp;
+               va_start(argp, fmt);
+               vfprintf(stderr, fmt, argp);
+               va_end(argp);
+               if (adderr)
+                       fprintf(stderr, ": ");
+       }
+       if (adderr)
+               fprintf(stderr, "%s", strerror(errno));
+       fprintf(stderr, "\n");
+       if (doexit)
+               exit(excode);
+}
+
+#ifndef HAVE_ERR
+# define err(E, FMT...) errmsg(1, E, 1, FMT)
+#endif
+
+#ifndef HAVE_ERRX
+# define errx(E, FMT...) errmsg(1, E, 0, FMT)
+#endif
+
+#ifndef HAVE_WARN
+# define warn(FMT...) errmsg(0, 0, 1, FMT)
+#endif
+
+#ifndef HAVE_WARNX
+# define warnx(FMT...) errmsg(0, 0, 0, FMT)
+#endif
+#endif /* !HAVE_ERR_H */
+
+
 static inline __attribute__((const)) int is_power_of_2(unsigned long num)
 {
        return (num != 0 && ((num & (num - 1)) == 0));