X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=misc.c;h=752bc8ee2cd3f483f5994abf0e5f750a24331ea6;hb=8a46a60c8c976073ef958cee0d919bc911a8f944;hp=9da70dc8e447d0d9f04455c4659115bd465b0348;hpb=bcfecca1a138d2b53238e213365d16b9e008881b;p=open-adventure.git diff --git a/misc.c b/misc.c index 9da70dc..752bc8e 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 @@ -78,7 +88,7 @@ void newspeak(char* msg) if (msg[i + 1] == 'd') { copy[i + 1] = 's'; - sprintf(parameters[pi], "%d", PARMS[pi]); + sprintf(parameters[pi], "%ld", PARMS[pi]); } // Unmodified string specifier. @@ -128,7 +138,7 @@ void newspeak(char* msg) // Render the final string. char rendered[2000]; // FIXME: to be replaced with dynamic allocation - sprintf(&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,10 +640,10 @@ bool MAPLIN(FILE *fp) * and is not changed thereafter unless the routines on this page choose * to do so. */ - if (!oldstyle && !isatty(1)) - fputs("> ", stdout); do { - if (oldstyle) { + if (!editline) { + if (prompt) + fputs("> ", stdout); IGNORE(fgets(rawbuf,sizeof(rawbuf)-1,fp)); eof = (feof(fp)); } else { @@ -653,10 +663,17 @@ bool MAPLIN(FILE *fp) fclose(logfp); return false; } else { + FILE *efp = NULL; if (logfp && fp == stdin) - IGNORE(fputs(rawbuf, logfp)); + efp = logfp; else if (!isatty(0)) - IGNORE(fputs(rawbuf, stdout)); + efp = stdout; + if (efp != NULL) + { + if (prompt) + fputs("> ", efp); + IGNORE(fputs(rawbuf, efp)); + } strcpy(INLINE+1, rawbuf); LNLENG=0; for (i=1; i<=(long)sizeof(INLINE) && INLINE[i]!=0; i++) { @@ -698,7 +715,4 @@ void DATIME(long* d, long* t) *t = (long) tv.tv_usec; } -long MOD(long n, long m) -{ - return(n%m); -} +/* end */