Shorten the code using the err() function.

This commit is contained in:
Hisham 2016-02-03 12:42:25 +00:00
parent 5c593fae42
commit 807640e49b
1 changed files with 11 additions and 18 deletions

View File

@ -5,22 +5,24 @@
#ifndef _GNU_SOURCE #ifndef _GNU_SOURCE
#define _GNU_SOURCE #define _GNU_SOURCE
#endif #endif
#include <unistd.h> #include <err.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
/*{ /*{
#include <stdlib.h> #include <stdlib.h>
}*/ }*/
static char oomMessage[] = "Out of memory!\n"; static inline void fail() {
curs_set(1);
endwin();
err(1, NULL);
}
void* xMalloc(size_t size) { void* xMalloc(size_t size) {
void* data = malloc(size); void* data = malloc(size);
if (!data && size > 0) { if (!data && size > 0) {
curs_set(1); fail();
endwin();
write(2, oomMessage, sizeof oomMessage - 1);
exit(1);
} }
return data; return data;
} }
@ -28,10 +30,7 @@ void* xMalloc(size_t size) {
void* xCalloc(size_t nmemb, size_t size) { void* xCalloc(size_t nmemb, size_t size) {
void* data = calloc(nmemb, size); void* data = calloc(nmemb, size);
if (!data) { if (!data) {
curs_set(1); fail();
endwin();
write(2, oomMessage, sizeof oomMessage - 1);
exit(1);
} }
return data; return data;
} }
@ -39,10 +38,7 @@ void* xCalloc(size_t nmemb, size_t size) {
void* xRealloc(void* ptr, size_t size) { void* xRealloc(void* ptr, size_t size) {
void* data = realloc(ptr, size); void* data = realloc(ptr, size);
if (!data && size > 0) { if (!data && size > 0) {
curs_set(1); fail();
endwin();
write(2, oomMessage, sizeof oomMessage - 1);
exit(1);
} }
return data; return data;
} }
@ -50,10 +46,7 @@ void* xRealloc(void* ptr, size_t size) {
char* xStrdup(const char* str) { char* xStrdup(const char* str) {
char* data = strdup(str); char* data = strdup(str);
if (!data && str) { if (!data && str) {
curs_set(1); fail();
endwin();
write(2, oomMessage, sizeof oomMessage - 1);
exit(1);
} }
return data; return data;
} }