39 lines
818 B
C
39 lines
818 B
C
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <libgen.h>
|
|
|
|
#ifndef __DECONST
|
|
#define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var))
|
|
#endif
|
|
|
|
#ifdef MAINFILE
|
|
const char *PROGNAME = NULL;
|
|
|
|
static inline void initprogname(const char *argv0) {
|
|
PROGNAME = "grep";
|
|
if (argv0 && argv0[0]) {
|
|
char *copied_argv0 = strdup(argv0);
|
|
if (!copied_argv0) return;
|
|
PROGNAME = basename(copied_argv0);
|
|
}
|
|
}
|
|
#else
|
|
extern const char *PROGNAME;
|
|
#endif
|
|
|
|
static inline const char *getprogname() {
|
|
if (!PROGNAME) abort();
|
|
return PROGNAME;
|
|
}
|
|
|
|
static inline void warnc(int code, const char *fmt, ...) {
|
|
va_list args;
|
|
|
|
fprintf(stderr, "warning: ");
|
|
va_start(args, fmt);
|
|
vfprintf(stderr, fmt, args);
|
|
va_end(args);
|
|
fprintf(stderr, ": %s\n", strerror(code));
|
|
}
|