Commit 807640e4 authored by Hisham's avatar Hisham
Browse files

Shorten the code using the err() function.

parent 5c593fae
...@@ -5,22 +5,24 @@ ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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;
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment