PATH_MAX and MAX_PATH are crazy.

This commit is contained in:
Enno Rehling 2017-12-29 11:44:14 +01:00
parent f98eab56c4
commit 50173d5be3
19 changed files with 99 additions and 65 deletions

View File

@ -7,9 +7,11 @@
#include <kernel/config.h>
#include <kernel/jsonconf.h>
#include <util/bsdstring.h>
#include <util/nrmessage.h>
#include <util/log.h>
#include <util/language.h>
#include <util/nrmessage.h>
#include <util/path.h>
#include <cJSON.h>
#include <string.h>
#include <stdio.h>
@ -58,12 +60,12 @@ int config_parse(const char *json)
int config_read(const char *filename, const char * relpath)
{
char name[MAX_PATH];
char name[PATH_MAX];
FILE *F;
json_relpath = relpath;
if (relpath) {
join_path(relpath, filename, name, sizeof(name));
path_join(relpath, filename, name, sizeof(name));
F = fopen(name, "r");
}
else {

View File

@ -52,6 +52,7 @@
#include <util/lists.h>
#include <util/log.h>
#include <util/macros.h>
#include <util/path.h>
#include <util/rng.h>
#include <util/unicode.h>
@ -879,7 +880,7 @@ static void select_regions(state * st, int selectmode)
}
static void loaddata(state *st) {
char datafile[MAX_PATH];
char datafile[PATH_MAX];
askstring(st->wnd_status->handle, "load from:", datafile, sizeof(datafile));
if (strlen(datafile) > 0) {
@ -889,7 +890,7 @@ static void loaddata(state *st) {
}
static void savedata(state *st) {
char datafile[MAX_PATH];
char datafile[PATH_MAX];
askstring(st->wnd_status->handle, "save as:", datafile, sizeof(datafile));
if (strlen(datafile) > 0) {
@ -1391,7 +1392,7 @@ void run_mapper(void)
char sbuffer[512];
if (!new_players) {
join_path(basepath(), "newfactions", sbuffer, sizeof(sbuffer));
path_join(basepath(), "newfactions", sbuffer, sizeof(sbuffer));
new_players = read_newfactions(sbuffer);
}

View File

@ -2,9 +2,11 @@
#include "items.h"
#include "alchemy.h"
#include "skill.h"
#include "keyword.h"
#include "direction.h"
#include "study.h"
#include "economy.h"
#include "move.h"
#include "magic.h"
#include <attributes/fleechance.h>
@ -14,17 +16,16 @@
#include <spells/regioncurse.h>
#include <kernel/curse.h>
#include <kernel/building.h>
#include <kernel/faction.h>
#include <kernel/item.h>
#include <kernel/messages.h>
#include <kernel/order.h>
#include <kernel/plane.h>
#include <kernel/pool.h>
#include <kernel/race.h>
#include <kernel/region.h>
#include <kernel/ship.h>
#include <kernel/spell.h>
#include <kernel/skills.h>
#include <kernel/unit.h>
/* triggers includes */
@ -33,13 +34,13 @@
#include <util/attrib.h>
#include <util/event.h>
#include <util/log.h>
#include <util/macros.h>
#include <util/parser.h>
#include <util/rand.h>
#include <util/rng.h>
#include <assert.h>
#include <stddef.h>
#include <limits.h>
/* BEGIN studypotion */

View File

@ -63,6 +63,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/lists.h>
#include <util/macros.h>
#include <util/parser.h>
#include <util/path.h>
#include <util/rand.h>
#include <util/rng.h>
#include <util/strings.h>
@ -520,31 +521,13 @@ void set_basepath(const char *path)
g_basedir = path;
}
#ifdef WIN32
#define PATH_DELIM '\\'
#else
#define PATH_DELIM '/'
#endif
char * join_path(const char *p1, const char *p2, char *dst, size_t len) {
size_t sz;
assert(p1 && p2);
assert(p2 != dst);
if (dst == p1) {
sz = strlen(p1);
}
else {
sz = strlcpy(dst, p1, len);
}
assert(sz < len);
dst[sz++] = PATH_DELIM;
strlcpy(dst + sz, p2, len - sz);
return dst;
return path_join(p1, p2, dst, len);
}
static const char * relpath(char *buf, size_t sz, const char *path) {
if (g_basedir) {
join_path(g_basedir, path, buf, sz);
path_join(g_basedir, path, buf, sz);
}
else {
strlcpy(buf, path, sz);

View File

@ -48,6 +48,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/log.h>
#include <util/parser.h>
#include <util/password.h>
#include <util/path.h>
#include <util/resolve.h>
#include <util/rng.h>
#include <util/strings.h>
@ -854,7 +855,7 @@ int writepasswd(void)
FILE *F;
char zText[128];
join_path(basepath(), "passwd", zText, sizeof(zText));
path_join(basepath(), "passwd", zText, sizeof(zText));
F = fopen(zText, "w");
if (!F) {
perror(zText);

View File

@ -45,6 +45,7 @@ without prior permission by the authors of Eressea.
#include <util/log.h>
#include <util/message.h>
#include <util/nrmessage.h>
#include <util/path.h>
#include <util/strings.h>
#include <util/xml.h>
@ -897,8 +898,8 @@ static void json_include(cJSON *json) {
for (child = json->child; child; child = child->next) {
FILE *F;
if (json_relpath) {
char name[MAX_PATH];
join_path(json_relpath, child->valuestring, name, sizeof(name));
char name[PATH_MAX];
path_join(json_relpath, child->valuestring, name, sizeof(name));
F = fopen(name, "r");
}
else {

View File

@ -70,6 +70,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/log.h>
#include <util/parser.h>
#include <util/password.h>
#include <util/path.h>
#include <util/rand.h>
#include <util/resolve.h>
#include <util/rng.h>
@ -292,11 +293,11 @@ static void write_owner(gamedata *data, region_owner * owner)
int current_turn(void)
{
char zText[MAX_PATH];
char zText[PATH_MAX];
int cturn = 0;
FILE *F;
join_path(basepath(), "turn", zText, sizeof(zText));
path_join(basepath(), "turn", zText, sizeof(zText));
F = fopen(zText, "r");
if (!F) {
perror(zText);
@ -1151,7 +1152,7 @@ static int cb_sb_maxlevel(spellbook_entry *sbe, void *cbdata) {
int readgame(const char *filename)
{
int n, stream_version;
char path[MAX_PATH];
char path[PATH_MAX];
gamedata gdata = { 0 };
storage store;
stream strm;
@ -1159,7 +1160,7 @@ int readgame(const char *filename)
size_t sz;
log_debug("- reading game data from %s", filename);
join_path(datapath(), filename, path, sizeof(path));
path_join(datapath(), filename, path, sizeof(path));
F = fopen(path, "rb");
if (!F) {
@ -1562,14 +1563,14 @@ static void clear_npc_orders(faction *f)
int writegame(const char *filename)
{
int n;
char path[MAX_PATH];
char path[PATH_MAX];
gamedata gdata;
storage store;
stream strm;
FILE *F;
create_directories();
join_path(datapath(), filename, path, sizeof(path));
path_join(datapath(), filename, path, sizeof(path));
/* make sure we don't overwrite an existing file (hard links) */
if (remove(path) != 0) {
if (errno == ENOENT) {

View File

@ -22,6 +22,7 @@
#include <util/event.h>
#include <util/gamedata.h>
#include <util/password.h>
#include <util/path.h>
#include <util/strings.h>
#include <storage.h>
@ -36,11 +37,11 @@
static void test_readwrite_data(CuTest * tc)
{
const char *filename = "test.dat";
char path[MAX_PATH];
char path[PATH_MAX];
test_setup();
CuAssertIntEquals(tc, 0, writegame(filename));
CuAssertIntEquals(tc, 0, readgame(filename));
join_path(datapath(), filename, path, sizeof(path));
path_join(datapath(), filename, path, sizeof(path));
CuAssertIntEquals(tc, 0, remove(path));
test_teardown();
}

View File

@ -89,6 +89,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/message.h>
#include <util/parser.h>
#include <util/password.h>
#include <util/path.h>
#include <util/rand.h>
#include <util/rng.h>
#include <util/strings.h>
@ -4260,7 +4261,7 @@ void update_subscriptions(void)
FILE *F;
char zText[4096];
join_path(basepath(), "subscriptions", zText, sizeof(zText));
path_join(basepath(), "subscriptions", zText, sizeof(zText));
F = fopen(zText, "r");
if (F == NULL) {
log_warning(0, "could not open %s.\n", zText);

View File

@ -20,14 +20,17 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <platform.h>
#endif
#include <util/log.h>
#include <kernel/config.h>
#include <kernel/database.h>
#include <kernel/version.h>
#include <kernel/save.h>
#include <util/filereader.h>
#include <util/language.h>
#include <util/log.h>
#include <util/macros.h>
#include <util/path.h>
#include "eressea.h"
#ifdef USE_CURSES
#include "gmtool.h"
@ -98,12 +101,12 @@ static const char * valid_keys[] = {
static dictionary *parse_config(const char *filename)
{
char path[MAX_PATH];
char path[PATH_MAX];
dictionary *d;
const char *str, *cfgpath = config_get("config.path");
if (cfgpath) {
join_path(cfgpath, filename, path, sizeof(path));
path_join(cfgpath, filename, path, sizeof(path));
log_debug("reading from configuration file %s\n", path);
d = iniparser_load(path);
}

View File

@ -35,6 +35,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* util includes */
#include <util/base36.h>
#include <util/language.h>
#include <util/path.h>
/* libc includes */
#include <assert.h>
@ -152,7 +153,7 @@ void score(void)
allscores = 1;
}
join_path(basepath(), "score", path, sizeof(path));
path_join(basepath(), "score", path, sizeof(path));
scoreFP = fopen(path, "w");
if (scoreFP) {
const unsigned char utf8_bom[4] = { 0xef, 0xbb, 0xbf, 0 };
@ -177,7 +178,7 @@ void score(void)
alliance *a;
const item_type *token = it_find("conquesttoken");
join_path(basepath(), "score.alliances", path, sizeof(path));
path_join(basepath(), "score.alliances", path, sizeof(path));
scoreFP = fopen(path, "w");
if (scoreFP) {
const unsigned char utf8_bom[4] = { 0xef, 0xbb, 0xbf, 0 };

View File

@ -5,6 +5,8 @@
#endif
#ifdef _MSC_VER
/* @see https://developercommunity.visualstudio.com/content/problem/69874/warning-c4001-in-standard-library-stringh-header.html */
#if _MSC_VER >= 1900
#pragma warning(disable: 4710 4820 4001)
#pragma warning(disable: 4100) // unreferenced formal parameter
@ -13,6 +15,10 @@
#pragma warning(disable: 4459) // declaration hides global
#pragma warning(disable: 4224) // formal parameter was previously defined as a type
#endif
/* @see https://insanecoding.blogspot.no/2007/11/pathmax-simply-isnt.html */
#define PATH_MAX 260
#else /* assume gcc */
#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
# define va_copy(a,b) __va_copy(a,b)
@ -20,13 +26,5 @@
#endif
/* #define _POSIX_C_SOURCE 200809L
*/
#ifndef MAX_PATH
# define MAX_PATH 4096
#endif
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define TOLUA_CAST (char*)

View File

@ -68,6 +68,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/lists.h>
#include <util/log.h>
#include <util/macros.h>
#include <util/path.h>
#include <util/strings.h>
#include <util/translation.h>
#include <stream.h>
@ -1567,7 +1568,7 @@ int write_reports(faction * f, time_t ltime)
char path[4096];
sprintf(filename, "%d-%s.%s", turn, itoa36(f->no),
rtype->extension);
join_path(reportpath(), filename, path, sizeof(path));
path_join(reportpath(), filename, path, sizeof(path));
errno = 0;
if (rtype->write(path, &ctx, (const char *)utf8_bom) == 0) {
gotit = true;
@ -1645,7 +1646,7 @@ int reports(void)
report_donations();
remove_empty_units();
join_path(rpath, "reports.txt", path, sizeof(path));
path_join(rpath, "reports.txt", path, sizeof(path));
mailit = fopen(path, "w");
if (mailit == NULL) {
log_error("%s could not be opened!\n", path);

View File

@ -30,6 +30,7 @@
#include <util/language.h>
#include <util/lists.h>
#include <util/log.h>
#include <util/path.h>
#include <assert.h>
#include <string.h>
@ -165,7 +166,7 @@ static void writeturn(void)
char zText[4096];
FILE *f;
join_path(basepath(), "datum", zText, sizeof(zText));
path_join(basepath(), "datum", zText, sizeof(zText));
f = fopen(zText, "w");
if (!f) {
perror(zText);
@ -173,7 +174,7 @@ static void writeturn(void)
}
fputs(gamedate2(default_locale), f);
fclose(f);
join_path(basepath(), "turn", zText, sizeof(zText));
path_join(basepath(), "turn", zText, sizeof(zText));
f = fopen(zText, "w");
if (!f) {
perror(zText);
@ -192,10 +193,10 @@ void report_summary(summary * s, summary * o, bool full)
int timeout = NMRTimeout();
if (full) {
join_path(basepath(), "parteien.full", zText, sizeof(zText));
path_join(basepath(), "parteien.full", zText, sizeof(zText));
}
else {
join_path(basepath(), "parteien", zText, sizeof(zText));
path_join(basepath(), "parteien", zText, sizeof(zText));
}
F = fopen(zText, "w");
if (!F) {

View File

@ -50,6 +50,7 @@ mt19937ar.c
nrmessage.c
parser.c
password.c
path.c
rand.c
resolve.c
strings.c

View File

@ -10,8 +10,10 @@ This program may not be used, modified or distributed
without prior permission by the authors of Eressea.
*/
#include <platform.h>
#include "log.h"
#include "bsdstring.h"
#include "log.h"
#include "path.h"
#include "unicode.h"
#include <assert.h>
@ -92,7 +94,7 @@ cp_convert(const char *format, unsigned char *buffer, size_t length, int codepag
void log_rotate(const char *filename, int maxindex)
{
char buffer[2][MAX_PATH];
char buffer[2][PATH_MAX];
int dst = 1;
assert(strlen(filename) < sizeof(buffer[0]) - 4);

View File

@ -1,3 +1,2 @@
#define UNUSED_ARG(x) (void)(x)
#define TOLUA_CAST (char*)

26
src/util/path.c Normal file
View File

@ -0,0 +1,26 @@
#ifdef _MSC_VER
#include <platform.h>
#endif
#include "path.h"
#include "bsdstring.h"
#include <assert.h>
#include <string.h>
char * path_join(const char *p1, const char *p2, char *dst, size_t len) {
size_t sz;
assert(p1 && p2);
assert(p2 != dst);
if (dst == p1) {
sz = strlen(p1);
}
else {
sz = strlcpy(dst, p1, len);
}
assert(sz < len);
dst[sz++] = PATH_DELIM;
strlcpy(dst + sz, p2, len - sz);
return dst;
}

10
src/util/path.h Normal file
View File

@ -0,0 +1,10 @@
#include <stddef.h>
#ifdef WIN32
#define PATH_DELIM '\\'
#else
#define PATH_DELIM '/'
#endif
char * path_join(const char *p1, const char *p2, char *dst, size_t len);