X-Git-Url: https://jxself.org/git/?p=open-adventure.git;a=blobdiff_plain;f=misc.c;h=6dd24e0c89f6ced0e9b67c7a162b3caf6e9b82e6;hp=d89b4058fbcfd003329b3a470b8fed765b7ba93e;hb=ca0e04295298397a0b4cfe41a8801f0226cea5d4;hpb=a044f10411cac5c530a19a66b7252110d5d1ca0f diff --git a/misc.c b/misc.c index d89b405..6dd24e0 100644 --- a/misc.c +++ b/misc.c @@ -21,6 +21,17 @@ void* xmalloc(size_t size) return(ptr); } +char* xstrdup(const char* s) +{ + char* ptr = strdup(s); + if (ptr == NULL) + { + fprintf(stderr, "Out of memory!\n"); + exit(EXIT_FAILURE); + } + return(ptr); +} + void packed_to_token(long packed, char token[6]) { // Unpack and map back to ASCII. @@ -60,8 +71,7 @@ void newspeak(char* msg) printf("\n"); // Create a copy of our string, so we can edit it. - char* copy = (char*) xmalloc(strlen(msg) + 1); - strncpy(copy, msg, strlen(msg) + 1); + char* copy = xstrdup(msg); // Staging area for stringified parameters. char parameters[5][100]; // FIXME: to be replaced with dynamic allocation