Use strdup() instead of strncpy().
This commit is contained in:
parent
a044f10411
commit
ca0e042952
2 changed files with 13 additions and 2 deletions
1
advent.h
1
advent.h
|
@ -87,6 +87,7 @@ extern lcg_state lcgstate;
|
||||||
#define READ_MODE "rb"
|
#define READ_MODE "rb"
|
||||||
#define WRITE_MODE "wb"
|
#define WRITE_MODE "wb"
|
||||||
extern void* xmalloc(size_t);
|
extern void* xmalloc(size_t);
|
||||||
|
extern char* xstrdup(const char*);
|
||||||
extern void packed_to_token(long, char token[6]);
|
extern void packed_to_token(long, char token[6]);
|
||||||
extern void newspeak(char*);
|
extern void newspeak(char*);
|
||||||
extern void PSPEAK(vocab_t,int);
|
extern void PSPEAK(vocab_t,int);
|
||||||
|
|
14
misc.c
14
misc.c
|
@ -21,6 +21,17 @@ void* xmalloc(size_t size)
|
||||||
return(ptr);
|
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])
|
void packed_to_token(long packed, char token[6])
|
||||||
{
|
{
|
||||||
// Unpack and map back to ASCII.
|
// Unpack and map back to ASCII.
|
||||||
|
@ -60,8 +71,7 @@ void newspeak(char* msg)
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
// Create a copy of our string, so we can edit it.
|
// Create a copy of our string, so we can edit it.
|
||||||
char* copy = (char*) xmalloc(strlen(msg) + 1);
|
char* copy = xstrdup(msg);
|
||||||
strncpy(copy, msg, strlen(msg) + 1);
|
|
||||||
|
|
||||||
// Staging area for stringified parameters.
|
// Staging area for stringified parameters.
|
||||||
char parameters[5][100]; // FIXME: to be replaced with dynamic allocation
|
char parameters[5][100]; // FIXME: to be replaced with dynamic allocation
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue