brutally murdered various malloc/strcat/strcpy constructs
This commit is contained in:
parent
ad0e97e423
commit
0ebf29660f
10 changed files with 48 additions and 61 deletions
11
src/config.c
11
src/config.c
|
@ -270,20 +270,13 @@ static void config_delete_unknown_entries(void) {
|
|||
}
|
||||
|
||||
static FILE* config_open(const char *filename, const char *mode) {
|
||||
char *buf;
|
||||
FILE *out;
|
||||
char *buf = strjoin(get_config_path(), "/", filename, NULL);
|
||||
FILE *out = fopen(buf, mode);
|
||||
|
||||
buf = malloc(strlen(filename) + strlen(get_config_path()) + 2);
|
||||
strcpy(buf, get_config_path());
|
||||
strcat(buf, "/");
|
||||
strcat(buf, filename);
|
||||
|
||||
out = fopen(buf, mode);
|
||||
free(buf);
|
||||
|
||||
if(!out) {
|
||||
warnx("config_open(): couldn't open '%s'", filename);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return out;
|
||||
|
|
27
src/global.c
27
src/global.c
|
@ -179,9 +179,7 @@ void take_screenshot(void)
|
|||
timeinfo = localtime(&rawtime);
|
||||
strftime(outfile, 128, "/taisei_%Y%m%d_%H-%M-%S_%Z.png", timeinfo);
|
||||
|
||||
outpath = malloc(strlen(outfile) + strlen(get_screenshots_path()) + 1);
|
||||
strcpy(outpath, get_screenshots_path());
|
||||
strcat(outpath, outfile);
|
||||
outpath = strjoin(get_screenshots_path(), outfile, NULL);
|
||||
|
||||
printf("Saving screenshot as %s\n", outpath);
|
||||
out = fopen(outpath, "wb");
|
||||
|
@ -298,6 +296,29 @@ char* strjoin(const char *first, ...) {
|
|||
return str;
|
||||
}
|
||||
|
||||
char* strfmt(const char *fmt, ...) {
|
||||
size_t written = 0;
|
||||
size_t fmtlen = strlen(fmt);
|
||||
size_t asize = 1;
|
||||
char *out = NULL;
|
||||
|
||||
while(asize * 2 <= fmtlen)
|
||||
asize *= 2;
|
||||
|
||||
do {
|
||||
asize *= 2;
|
||||
out = realloc(out, asize);
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
written = vsnprintf(out, asize, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
} while(written >= asize);
|
||||
|
||||
return realloc(out, strlen(out) + 1);
|
||||
}
|
||||
|
||||
char* read_all(const char *filename, int *outsize) {
|
||||
char *text;
|
||||
size_t size;
|
||||
|
|
|
@ -165,6 +165,7 @@ bool strstartswith(const char *s, const char *p);
|
|||
bool strendswith_any(const char *s, const char **earray);
|
||||
void stralloc(char **dest, const char *src);
|
||||
char* strjoin(const char *first, ...);
|
||||
char* strfmt(const char *fmt, ...);
|
||||
#undef strdup
|
||||
#define strdup(s) strjoin(s, NULL)
|
||||
bool gamekeypressed(KeyIndex key);
|
||||
|
|
11
src/main.c
11
src/main.c
|
@ -41,16 +41,13 @@ void init_log(void) {
|
|||
const char *pref = get_config_path();
|
||||
char *s;
|
||||
|
||||
s = malloc(strlen(pref) + strlen("stdout.txt") + 2);
|
||||
strcpy(s, pref);
|
||||
strcat(s, "/stdout.txt");
|
||||
|
||||
s = strfmt("%s/%s", pref, "stdout.txt");
|
||||
freopen(s, "w", stdout);
|
||||
free(s);
|
||||
|
||||
strcpy(s, pref);
|
||||
strcat(s, "/stderr.txt");
|
||||
|
||||
s = strfmt("%s/%s", pref, "stderr.txt");
|
||||
freopen(s, "w", stderr);
|
||||
free(s);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
@ -39,21 +39,13 @@ const char *get_replays_path(void) {
|
|||
void init_paths(void) {
|
||||
#ifdef RELATIVE
|
||||
char *basedir = SDL_GetBasePath();
|
||||
content_path = malloc(strlen(basedir) + strlen(DATA_DIR) + 1);
|
||||
strcpy(content_path, basedir);
|
||||
strcat(content_path, DATA_DIR);
|
||||
content_path = strjoin(basedir, DATA_DIR, NULL);
|
||||
free(basedir);
|
||||
#else
|
||||
content_path = FILE_PREFIX;
|
||||
#endif
|
||||
|
||||
conf_path = SDL_GetPrefPath("", "taisei");
|
||||
|
||||
scr_path = malloc(strlen(SCR_DIR) + strlen(get_config_path()) + 1);
|
||||
strcpy(scr_path, get_config_path());
|
||||
strcat(scr_path, SCR_DIR);
|
||||
|
||||
rpy_path = malloc(strlen(RPY_DIR) + strlen(get_config_path()) + 1);
|
||||
strcpy(rpy_path, get_config_path());
|
||||
strcat(rpy_path, RPY_DIR);
|
||||
scr_path = strjoin(conf_path, SCR_DIR, NULL);
|
||||
rpy_path = strjoin(conf_path, RPY_DIR, NULL);
|
||||
}
|
||||
|
|
|
@ -35,15 +35,7 @@ const char *get_replays_path(void) {
|
|||
}
|
||||
|
||||
void init_paths(void) {
|
||||
conf_path = malloc(strlen(CFG_DIR) + strlen(getenv("HOME")) + 1);
|
||||
strcpy(conf_path, getenv("HOME"));
|
||||
strcat(conf_path, CFG_DIR);
|
||||
|
||||
scr_path = malloc(strlen(SCR_DIR) + strlen(get_config_path()) + 1);
|
||||
strcpy(scr_path, get_config_path());
|
||||
strcat(scr_path, SCR_DIR);
|
||||
|
||||
rpy_path = malloc(strlen(RPY_DIR) + strlen(get_config_path()) + 1);
|
||||
strcpy(rpy_path, get_config_path());
|
||||
strcat(rpy_path, RPY_DIR);
|
||||
conf_path = strjoin(getenv("HOME"), CFG_DIR, NULL);
|
||||
scr_path = strjoin(conf_path, SCR_DIR, NULL);
|
||||
rpy_path = strjoin(conf_path, RPY_DIR, NULL);
|
||||
}
|
||||
|
|
|
@ -14,9 +14,7 @@
|
|||
struct Fonts _fonts;
|
||||
|
||||
TTF_Font *load_font(char *name, int size) {
|
||||
char *buf = malloc(strlen(get_prefix()) + strlen(name)+1);
|
||||
strcpy(buf, get_prefix());
|
||||
strcat(buf, name);
|
||||
char *buf = strjoin(get_prefix(), name, NULL);
|
||||
|
||||
TTF_Font *f = TTF_OpenFont(buf, size);
|
||||
if(!f)
|
||||
|
|
|
@ -205,9 +205,7 @@ void resource_util_strip_ext(char *path) {
|
|||
|
||||
char* resource_util_basename(const char *prefix, const char *path) {
|
||||
const char *baseprefix = get_prefix();
|
||||
char fullprefix[strlen(baseprefix) + strlen(prefix) + 1];
|
||||
strcpy(fullprefix, baseprefix);
|
||||
strcat(fullprefix, prefix);
|
||||
char *fullprefix = strjoin(baseprefix, prefix, NULL);
|
||||
|
||||
assert(strstartswith(path, fullprefix));
|
||||
|
||||
|
@ -215,6 +213,7 @@ char* resource_util_basename(const char *prefix, const char *path) {
|
|||
stralloc(&out, path + strlen(fullprefix));
|
||||
resource_util_strip_ext(out);
|
||||
|
||||
free(fullprefix);
|
||||
return out;
|
||||
}
|
||||
|
||||
|
|
16
src/stage.c
16
src/stage.c
|
@ -48,7 +48,7 @@ static void add_stage(uint16_t id, StageProcs *procs, StageType type, const char
|
|||
#endif
|
||||
}
|
||||
|
||||
static void end_stages() {
|
||||
static void end_stages(void) {
|
||||
add_stage(0, NULL, 0, NULL, NULL, NULL, 0, 0, 0);
|
||||
}
|
||||
|
||||
|
@ -75,18 +75,14 @@ void stage_init_array(void) {
|
|||
if(a->idmap[diff - D_Easy] >= 0) {
|
||||
uint16_t id = STAGE_SPELL_BIT | a->idmap[diff - D_Easy] | (s->id << 8);
|
||||
|
||||
char title[10];
|
||||
const char *postfix = difficulty_name(diff);
|
||||
|
||||
snprintf(title, sizeof(title), "Spell %d", ++spellnum);
|
||||
|
||||
char subtitle[strlen(postfix) + strlen(a->name) + 4];
|
||||
strcpy(subtitle, a->name);
|
||||
strcat(subtitle, " ~ ");
|
||||
strcat(subtitle, postfix);
|
||||
char *title = strfmt("Spell %d", ++spellnum);
|
||||
char *subtitle = strjoin(a->name, " ~ ", difficulty_name(diff), NULL);
|
||||
|
||||
add_stage(id, s->procs->spellpractice_procs, STAGE_SPELL, title, subtitle, a, diff, 0, 0);
|
||||
s = stages + i; // stages just got realloc'd, so we must update the pointer
|
||||
|
||||
free(title);
|
||||
free(subtitle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "global.h"
|
||||
|
||||
// void err(int eval, const char *fmt, ...);
|
||||
void errx(int eval, const char *fmt, ...) {
|
||||
|
@ -30,10 +31,7 @@ void errx(int eval, const char *fmt, ...) {
|
|||
void warnx(const char *fmt, ...) {
|
||||
va_list ap;
|
||||
|
||||
char *buf = malloc(14+strlen(fmt));
|
||||
strcpy(buf, "!- WARNING: ");
|
||||
strcat(buf, fmt);
|
||||
strcat(buf, "\n");
|
||||
char *buf = strjoin("!- WARNING: ", fmt, "\n", NULL);
|
||||
|
||||
va_start(ap, fmt);
|
||||
vfprintf(stderr, buf, ap);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue