X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=misc.c;h=3960347f36bc77bcc10d30a86f8d307569f1611d;hb=c366ddb7334cf85dba1f0dc75a68a8a094960aa3;hp=dde006340525739807e6e4c8f9362ce02083726c;hpb=32b04f190ec31b841d0628af2346a914adce7c3e;p=open-adventure.git diff --git a/misc.c b/misc.c index dde0063..3960347 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 @@ -128,7 +138,7 @@ void newspeak(char* msg) // Render the final string. char rendered[2000]; // FIXME: to be replaced with dynamic allocation - sprintf((char *)&rendered, copy, parameters[1], parameters[2], parameters[3], parameters[4]); // FIXME: to be replaced with vsprintf() + sprintf(rendered, copy, parameters[1], parameters[2], parameters[3], parameters[4]); // FIXME: to be replaced with vsprintf() // Print the message. printf("%s\n", rendered); @@ -630,14 +640,14 @@ bool MAPLIN(FILE *fp) * and is not changed thereafter unless the routines on this page choose * to do so. */ - if (!oldstyle && !isatty(1)) + if (prompt) fputs("> ", stdout); do { - if (oldstyle) { + if (!editline) { IGNORE(fgets(rawbuf,sizeof(rawbuf)-1,fp)); eof = (feof(fp)); } else { - char *cp = linenoise("> "); + char *cp = linenoise(prompt ? "> ": ""); eof = (cp == NULL); if (!eof) { strncpy(rawbuf, cp, sizeof(rawbuf)-1); @@ -698,7 +708,4 @@ void DATIME(long* d, long* t) *t = (long) tv.tv_usec; } -long MOD(long n, long m) -{ - return(n%m); -} +/* end */