Skip to content

Instantly share code, notes, and snippets.

@Zorgatone
Last active September 10, 2017 10:26
Show Gist options
  • Save Zorgatone/3b0ae126d069c7300bddce8a51cf7073 to your computer and use it in GitHub Desktop.
Save Zorgatone/3b0ae126d069c7300bddce8a51cf7073 to your computer and use it in GitHub Desktop.
Die utility - terminate C program with error message and exit code
#include <stdarg.h>
#include "util.h"
void vsfdie(FILE * stream, int code, const char *format, va_list arg) {
vfprintf(stream, format, arg);
va_end(arg);
exit(code);
}
void sfdie(FILE * stream, int code, const char *format, ...) {
va_list args;
va_start(args, format);
vsfdie(stream, code, format, args);
}
void sdie(int code, const char *format, ...) {
va_list args;
va_start(args, format);
vsfdie(stderr, code, format, args);
}
void fdie(FILE *stream, const char *format, ...) {
va_list args;
va_start(args, format);
vfdie(stream, format, args);
}
void die(const char *format, ...) {
va_list args;
va_start(args, format);
vdie(format, args);
}
#pragma once
#ifndef UTIL_H
#define UTIL_H
#include <stdio.h>
#include <stdlib.h>
void vsfdie(FILE *stream, int code, const char *format, va_list arg);
void sfdie(FILE * stream, int code, const char *format, ...);
void sdie(int code, const char *format, ...);
void die(const char *format, ...);
static inline void vsdie(int code, const char *format, va_list arg) {
vsfdie(stderr, code, format, arg);
};
static inline void vfdie(FILE *stream, const char *format, va_list arg) {
vsfdie(stream, EXIT_FAILURE, format, arg);
}
static inline void vdie(const char *format, va_list arg) {
vsdie(EXIT_FAILURE, format, arg);
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment