adding a os module for os-specific functions (mkdir in this case).

This commit is contained in:
Enno Rehling 2011-02-26 22:55:27 -08:00
parent 665d4aec4d
commit 611fe8e2a1
10 changed files with 67 additions and 21 deletions

View file

@ -19,11 +19,12 @@
#include <util/goodies.c>
#include <util/language.c>
#include <util/lists.c>
#include <util/quicklist.c>
#include <util/log.c>
#include <util/message.c>
#include <util/nrmessage.c>
#include <util/os.c>
#include <util/parser.c>
#include <util/quicklist.c>
#include <util/rand.c>
#include <util/resolve.c>
#include <util/sql.c>

View file

@ -589,7 +589,7 @@ learn_cmd(unit * u, order * ord)
struct building * b = inside_building(u);
const struct building_type * btype = b?b->type:NULL;
if (btype == bt_find("academy")) {
if (btype && btype == bt_find("academy")) {
studycost = MAX(50, studycost * 2);
}
}

View file

@ -59,6 +59,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/cvector.h>
#include <util/language.h>
#include <util/log.h>
#include <util/os.h>
#include <util/parser.h>
#include <util/rand.h>
#include <util/rng.h>
@ -3575,7 +3576,7 @@ make_battle(region * r)
char zText[MAX_PATH];
char zFilename[MAX_PATH];
sprintf(zText, "%s/battles", basepath());
makedir(zText, 0700);
os_mkdir(zText, 0700);
sprintf(zFilename, "%s/battle-%d-%s.log", zText, obs_count, simplename(r));
bdebug = fopen(zFilename, "w");
if (!bdebug) log_error(("battles cannot be debugged\n"));

View file

@ -51,6 +51,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/language.h>
#include <util/lists.h>
#include <util/log.h>
#include <util/os.h>
#include <util/quicklist.h>
/* libc includes */
@ -1491,7 +1492,7 @@ init_reports(void)
if (stat(reportpath(), &st)==0) return 0;
}
#endif
if (makedir(reportpath(), 0700)!=0) {
if (os_mkdir(reportpath(), 0700)!=0) {
if (errno!=EEXIST) {
perror("could not create reportpath");
return -1;

View file

@ -61,6 +61,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/language.h>
#include <util/lists.h>
#include <util/log.h>
#include <util/os.h>
#include <util/parser.h>
#include <util/quicklist.h>
#include <util/rand.h>
@ -1683,7 +1684,11 @@ writegame(const char *filename, int mode)
store->encoding = enc_gamedata;
if (store->open(store, path, IO_WRITE)!=0) {
return -1;
int err = os_mkdir(datapath(), 0700);
if (err) return err;
if (store->open(store, path, IO_WRITE)!=0) {
return -1;
}
}
/* globale Variablen */

View file

@ -120,19 +120,12 @@ typedef struct stat stat_type;
# define HAVE_SNPRINTF
#ifdef _POSIX_SOURCE /* MINGW doesn't seem to have these */
# define HAVE_EXECINFO
# define HAVE_MKDIR_WITH_PERMISSION
# define HAVE_SIGACTION
# define HAVE_LINK
# define HAVE_SLEEP
#endif
#endif
/* egcpp 4 dos */
#ifdef MSDOS
# include <dir.h>
# define HAVE_MKDIR_WITH_PERMISSION
#endif
/* TinyCC */
#ifdef TINYCC
# undef HAVE_INLINE
@ -211,15 +204,6 @@ typedef struct _stat stat_type;
# endif
#endif
#ifdef HAVE_MKDIR_WITH_PERMISSION
# define makedir(d, p) mkdir(d, p)
#elif defined(HAVE_MKDIR_WITHOUT_PERMISSION)
# define makedir(d, p) mkdir(d)
#elif defined(HAVE__MKDIR_WITHOUT_PERMISSION)
_CRTIMP int __cdecl _mkdir(const char *);
# define makedir(d, p) _mkdir(d)
#endif
#ifndef HAVE_STRDUP
extern char * strdup(const char *s);
#endif

View file

@ -107,6 +107,7 @@
<ClInclude Include="util\message.h" />
<ClInclude Include="util\nrmessage.h" />
<ClInclude Include="util\nrmessage_struct.h" />
<ClInclude Include="util\os.h" />
<ClInclude Include="util\parser.h" />
<ClInclude Include="util\patricia.h" />
<ClInclude Include="util\quicklist.h" />
@ -142,6 +143,7 @@
<ClCompile Include="util\log.c" />
<ClCompile Include="util\message.c" />
<ClCompile Include="util\nrmessage.c" />
<ClCompile Include="util\os.c" />
<ClCompile Include="util\parser.c" />
<ClCompile Include="util\patricia.c" />
<ClCompile Include="util\quicklist.c" />

View file

@ -110,6 +110,9 @@
<ClInclude Include="util\quicklist.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="util\os.h">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="util\argstack.c">
@ -208,5 +211,8 @@
<ClCompile Include="util\quicklist_test.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="util\os.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

17
src/util/os.c Normal file
View file

@ -0,0 +1,17 @@
#include "os.h"
#if defined(WIN32)
#include <direct.h>
#elif defined(__GCC__)
#include <sys/stat.h>
#endif
int os_mkdir(const char * path, int mode)
{
#ifdef WIN32
mode = mode;
return _mkdir(path);
#else /* POSIX is our last hope */
return mkdir(path, (mode_t)mode);
#endif
}

29
src/util/os.h Normal file
View file

@ -0,0 +1,29 @@
/*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
#ifndef _OS_H
#define _OS_H
#ifdef __cplusplus
extern "C" {
#endif
extern int os_mkdir(const char * path, int mode);
#ifdef __cplusplus
}
#endif
#endif