diff --git a/bin/Makefile b/bin/Makefile index 51fa3b3..261a6bd 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -38,18 +38,6 @@ ifdef LDATA_DEF CFLAGS += -DLDATA_DEF=$(LDATA_DEF) endif -# OS Stuff. - -# Linux Stuff. -ifeq ($(OS),LINUX) -CFLAGS += -D_POSIX_SOURCE -endif - -# FreeBSD stuff. -ifeq ($(OS),FREEBSD) - CFLAGS += -D_POSIX_SOURCE -D__BSD_VISIBLE -endif - # Debug stuff. ifdef DEBUG CFLAGS += -W -Wall -Wextra -Wunused -Wshadow -Wpointer-arith -Wmissing-prototypes \ @@ -67,11 +55,6 @@ ifeq ($(OS), LINUX) LDFLAGS += -rdynamic endif # Linux. -# FreeBSD stuff. -ifeq ($(OS), FREEBSD) - LDFLAGS += -rdynamic -endif - else # DEBUG CFLAGS += -O2 -funroll-loops -pipe -std=c99 endif diff --git a/src/lcompat.h b/src/lcompat.h new file mode 100644 index 0000000..132fd2d --- /dev/null +++ b/src/lcompat.h @@ -0,0 +1,11 @@ +#pragma once + +/* System specific. */ +#define HAS_LINUX (defined(linux) || defined(__linux) || defined(__linux__)) +#define HAS_FREEBSD (defined(__FreeBSD__)) +#define HAS_HAS_WIN32 (defined(__WIN32)) + +/* Standard specific. */ +#define HAS_POSIX HAS_UNIX /* (defined(_POSIX_VERSION) && (_POSIX_VERSION >= 200112L)) */ +#define HAS_UNIX (defined(__unix__) || defined(__unix)) + diff --git a/src/lephisto.c b/src/lephisto.c index 6d53c2c..da763c1 100644 --- a/src/lephisto.c +++ b/src/lephisto.c @@ -11,14 +11,16 @@ #include +#include "lcompat.h" + /* Global. */ #include -#if defined(LINUX) && !defined(DEBUGGING) +#if HAS_LINUX && defined(DEBUG) /* DEBUGGING isn't defined yet. */ #include #include #include #include -#endif /* defined(LINUX) && !defined(DEBUGGING) */ +#endif /* HAS_LINUX && defined(DEBUG) */ /* Local. */ #include "lephisto.h" @@ -606,7 +608,7 @@ static void print_SDLversion(void) { WARN("SDL is older than compiled version."); } -#if defined(LINUX) && !defined(DEBUGGING) +#if HAS_LINUX && defined(DEBUGGING) /** * @brief Get the string related to the signal code. * @param sig Signal to which code belongs. @@ -667,13 +669,13 @@ static void debug_sigHandler(int sig, siginfo_t* info, void* unused) { exit(1); } -#endif /* defined(LINUX) && !defined(DEBUG) */ +#endif /* HAS_LINUX && defined(DEBUGGING) */ /** * @brief Set up the SignalHandler for Linux. */ static void debug_sigInit(void) { -#if defined(LINUX) && !defined(DEBUGGING) +#if HAS_LINUX && defined(DEBUGGING) struct sigaction sa, so; /* Set up handler. */ @@ -692,6 +694,6 @@ static void debug_sigInit(void) { sigaction(SIGABRT, &sa, &so); if(so.sa_handler == SIG_IGN) DEBUG("Unable to get set up SIGABRT signal handler."); -#endif /* #if defined(LINUX) && !defined(DEBUGGING) */ +#endif /* #if HAS_LINUX && defined(DEBUGGING) */ } diff --git a/src/lfile.c b/src/lfile.c index e3237b1..7783b65 100644 --- a/src/lfile.c +++ b/src/lfile.c @@ -6,17 +6,19 @@ * @todo Add support for windows and mac OS. */ +#include "lcompat.h" + #include #include #include -#ifdef _POSIX_SOURCE +#if HAS_POSIX #include #include #include #include #include #include -#endif +#endif /* HAS_POSIX */ #include "lephisto.h" #include "log.h" @@ -33,10 +35,10 @@ char* lfile_basePath(void) { char* home; if(lephisto_base[0] == '\0') { -#if defined(LINUX) || defined(FREEBSD) +#if HAS_UNIX home = getenv("HOME"); snprintf(lephisto_base, PATH_MAX, "%s/.lephisto/", home); -#else +#else /* HAS_UNIX */ FILE* f; /* Try to open the file, C89 compliant, but not as precise as stat. */ @@ -69,7 +71,7 @@ int lfile_dirMakeExist(const char* path, ...) { va_end(ap); } -#ifdef _POSIX_SOURCE +#if HAS_POSIX struct stat buf; stat(file, &buf); @@ -78,9 +80,9 @@ int lfile_dirMakeExist(const char* path, ...) { WARN("Dir '%s' does not exist and unable to create", file); return -1; } -#else -#error "Needs implentation." -#endif +#else /* HAS_POSIX */ +#error "Feature needs implementation on this Operating System for Lephisto to work." +#endif /* HAS_POSIX */ return 0; } @@ -101,14 +103,21 @@ int lfile_fileExists(const char* path, ...) { va_end(ap); } -#ifdef _POSIX_SOURCE +#if HAS_POSIX struct stat buf; if(stat(file, &buf)==0) /* Stat worked, file must exist. */ return 1; -#else -#error "Needs implementation." -#endif +#else /* HAS_POSIX */ + FILE* f; + + /* Try to open the file, C89 compliant, but not as precise as stat. */ + f = fopen(file, 'r'); + if(f != NULL) { + fclose(f); + return 1; + } +#endif /* HAS_POSIX */ return 0; } @@ -134,7 +143,7 @@ char** lfile_readDir(int* lfiles, const char* path, ...) { va_end(ap); } -#ifdef _POSIX_SOURCE +#if HAS_POSIX int i, j, k, n; DIR* d; struct dirent *dir; @@ -216,9 +225,9 @@ char** lfile_readDir(int* lfiles, const char* path, ...) { free(tfiles); free(tt); -#else -#error "Needs implementation." -#endif +#else /* HAS_POSIX */ +#error "Feature needs implementation on this Operating System for Lephito to work." +#endif /* HAS_POSIX /* What if we find nothing? */ if((*lfiles) == 0) { diff --git a/src/pack.c b/src/pack.c index f8b4005..ad2185c 100644 --- a/src/pack.c +++ b/src/pack.c @@ -1,5 +1,10 @@ #include -#include /* S_IRUSR */ +#include /* creat() etc. */ +#include /* uint32_t */ +#if HAS_POSIX +#include /* ssize_t */ +#include /* S_IRUSR */ +#endif /* HAS_POSIX */ #include #include #include @@ -36,11 +41,11 @@ * @brief Abstracts around packfiles. */ struct Packfile_s { -#ifdef _POSIX_SOURCE +#if HAS_POSIX int fd; /**< File descriptor. */ #else FILE* fp; /**< For non-posix. */ -#endif +#endif /* HAS_POSIX */ uint32_t pos; /**< Cursor position. */ uint32_t start; /**< File start. */ uint32_t end; /**< File end. */ @@ -52,11 +57,11 @@ struct Packfile_s { * @brief Allows much faster creation of packfiles. */ struct Packcache_s { -#ifdef _POSIX_SOURCE +#if HAS_POSIX int fd; /**< File descriptor. */ #else FILE* fp; /* For non-posix. */ -#endif +#endif /* HAS_POSIX */ char** index; /**< Cached index for faster lookups. */ uint32_t* start; /**< Cached index starts. */ @@ -64,7 +69,7 @@ struct Packcache_s { }; /* Helper defines. */ -#ifdef _POSIX_SOURCE +#if HAS_POSIX #define READ(f, b, n) if(read((f)->fd, (b), (n)) != (n)) { \ ERR("Fewer bytes read then expected"); \ return NULL; } @@ -72,7 +77,7 @@ struct Packcache_s { #define READ(f, b, n) if(fread((b), 1, (n),(f)->fp) != (n)) { \ ERR("Fewer bytes read then expected"); \ return NULL; } -#endif +#endif /* HAS_POSIX */ #undef DEBUG /* This will be spammy. */ #define DEBUG(str, args...) do{;} while(0) /**< Hack to disable debugging. */ @@ -112,13 +117,13 @@ Packcache_t* pack_openCache(const char* packfile) { } /* Open file. */ -#ifdef _POSIX_SOURCE +#if HAS_POSIX cache->fd = open(packfile, O_RDONLY); if(cache->fd == -1) { #else cache->fp = fopen(packfile, "rb"); if(cache->fp == NULL) { -#endif +#endif /* HAS_POSIX */ ERR("Error opening %s: %s", packfile, strerror(errno)); return NULL; } @@ -157,11 +162,11 @@ void pack_closeCache(Packcache_t* cache) { uint32_t i; /* Close file. */ -#ifdef _POSIX_SOURCE +#if HAS_POSIX close(cache->fd); #else fclose(cache->fp); -#endif +#endif /* HAS_POSIX */ /* Free memory. */ if(cache->nindex > 0) { @@ -188,7 +193,7 @@ Packfile_t* pack_openFromCache(Packcache_t* cache, const char* filename) { for(i = 0; i < cache->nindex; i++) { if(strcmp(cache->index[i], filename)==0) { /* Copy file. */ -#ifdef _POSIX_SOURCE +#if HAS_POSIX file->fd = dup(cache->fd); #else file->fp = cache->fp; @@ -200,12 +205,12 @@ Packfile_t* pack_openFromCache(Packcache_t* cache, const char* filename) { /* Seek. */ if(file->start) { /* Go to the beginning of the file. */ -#ifdef _POSIX_SOURCE +#if HAS_POSIX if((uint32_t)lseek(file->fd, file->start, SEEK_SET) != file->start) { #else fseek(file->fp, file->start, SEEK_SET); if(errno) { -#endif +#endif /* HAS_POSIX */ ERR("Failure to seek to file start: %s", strerror(errno)); return NULL; } @@ -231,7 +236,7 @@ Packfile_t* pack_openFromCache(Packcache_t* cache, const char* filename) { * @return The size of the file. */ static off_t getfilesize(const char* filename) { -#ifdef _POSIX_SOURCE +#if HAS_POSIX struct stat file; if(!stat(filename, &file)) @@ -250,7 +255,7 @@ static off_t getfilesize(const char* filename) { fclose(fp); return size; -#endif +#endif /* HAS_POSIX */ } /** @@ -266,7 +271,7 @@ int pack_check(const char* filename) { buf = malloc(sizeof(magic)); -#ifdef _POSIX_SOURCE +#if HAS_POSIX int fd = open(filename, O_RDONLY); if(fd == -1) { ERR("Error opening %s: %s", filename, strerror(errno)); @@ -296,13 +301,13 @@ int pack_check(const char* filename) { ret = (memcmp(buf, &magic, sizeof(magic))==0) ? 0 : 1; fclose(file); -#endif +#endif /* HAS_POSIX */ free(buf); return ret; } -#ifdef _POSIX_SOURCE +#if HAS_POSIX #define WRITE(b,n) if(write(outfd, b, n)==-1) { \ ERR("Error writing to file: %s", strerror(errno)); \ free(buf); return -1; } /**< Macro to help check for errors. @@ -310,7 +315,7 @@ int pack_check(const char* filename) { #define WRITE(b,n) if(fwrite(b, 1, n, outf)==0) { \ ERR("Error writing to file: %s", strerror(errno)); \ free(buf); return -1; } /**< Macro to help check for errors. */ -#endif +#endif /* HAS_POSIX */ /** * @brief Packages files into a packfile. @@ -321,12 +326,12 @@ int pack_check(const char* filename) { */ int pack_files(const char* outfile, const char** infiles, const uint32_t nfiles) { void* buf; -#ifdef _POSIX_SOURCE +#if HAS_POSIX struct stat file; int outfd, infd; #else FILE* outf, *inf; -#endif +#endif /* HAS_POSIX */ uint32_t i; int namesize; uint32_t indexsize, pointer; @@ -335,11 +340,11 @@ int pack_files(const char* outfile, const char** infiles, const uint32_t nfiles) for(namesize = 0, i = 0; i < nfiles; i++) { /* Make sure file exists before writing. */ -#ifdef _POSIX_SOURCE +#if HAS_POSIX if(stat(infiles[i], &file)) { #else if(getfilesize(infiles[i]) == 0) { -#endif +#endif /* HAS_POSIX */ ERR("File %s does not exist", infiles[i]); return -1; } @@ -357,13 +362,13 @@ int pack_files(const char* outfile, const char** infiles, const uint32_t nfiles) DEBUG("Index size is %d", indexsize); /* Create the output file. */ -#ifdef _POSIX_SOURCE +#if HAS_POSIX outfd = creat(outfile, PERMS); if(outfd == -1) { #else outf = fopen(outfile, "wb"); if(outf == NULL) { -#endif +#endif /* HAS_POSIX */ ERR("Unable to open %s for writing", outfile); return -1; } @@ -395,32 +400,32 @@ int pack_files(const char* outfile, const char** infiles, const uint32_t nfiles) WRITE(&bytes, 4); /* filesize. */ DEBUG("About to write '%s' of %d bytes", infiles[i], bytes); md5_init(&md5); -#ifdef _POSIX_SOURCE +#if HAS_POSIX infd = open(infiles[i], O_RDONLY); while((bytes = read(infd, buf, BLOCKSIZE))) { #else inf = fopen(infiles[i], "rb"); while((bytes = fread(buf, 1, BLOCKSIZE, inf))) { -#endif +#endif /* HAS_POSIX */ WRITE(buf, bytes); /* Data. */ md5_append(&md5, buf, bytes); } md5_finish(&md5, md5val); WRITE(md5val, 16); -#ifdef _POSIX_SOURCE +#if HAS_POSIX close(infd); #else fclose(inf); -#endif +#endif /* HAS_POSIX */ DEBUG("Wrote file '%s'", infiles[i]); } free(md5val); -#ifdef _POSIX_SOURCE +#if HAS_POSIX close(outfd); #else fclose(outf); -#endif +#endif /* HAS_POSIX */ free(buf); DEBUG("Packfile success\n\t%d files\n\t%d bytes", @@ -445,13 +450,13 @@ Packfile_t* pack_open(const char* packfile, const char* filename) { file = malloc(sizeof(Packfile_t)); memset(file, 0, sizeof(Packfile_t)); -#ifdef _POSIX_SOURCE +#if HAS_POSIX file->fd = open(packfile, O_RDONLY); if(file->fd == -1) { #else file->fp = fopen(packfile, "rb"); if(file->fp == NULL) { -#endif +#endif /* HAS_POSIX */ ERR("Error opening %s: %s", filename, strerror(errno)); return NULL; } @@ -476,21 +481,21 @@ Packfile_t* pack_open(const char* packfile, const char* filename) { DEBUG("'%s' found at %d", filename, file->start); break; } -#ifdef _POSIX_SOURCE +#if HAS_POSIX lseek(file->fd, 4, SEEK_CUR); /* Ignore the file location. */ #else fseek(file->fp, 4, SEEK_CUR); -#endif +#endif /* HAS_POSIX */ } if(file->start) { /* Go to the beginning of the file. */ -#ifdef _POSIX_SOURCE +#if HAS_POSIX if((uint32_t)lseek(file->fd, file->start, SEEK_SET) != file->start) { #else fseek(file->fp, file->start, SEEK_SET); if(errno) { -#endif +#endif /* HAS_POSIX */ ERR("Failure to seek to file start: %s", strerror(errno)); return NULL; } @@ -521,11 +526,11 @@ ssize_t pack_read(Packfile_t* file, void* buf, size_t count) { count = file->end - file->pos; /* Can't go past the end! */ if(count == 0) return 0; -#ifdef _POSIX_SOURCE +#if HAS_POSIX if((bytes = read(file->fd, buf, count)) == -1) { #else if((bytes = fread(buf, 1, count, file->fp)) == -1) { -#endif +#endif /* HAS_POSIX */ ERR("Error while reading file: %s", strerror(errno)); return -1; } @@ -549,7 +554,7 @@ off_t pack_seek(Packfile_t* file, off_t offset, int whence) { DEBUG("Attempting to seek offset: %d, whence: %d", offset, whence); off_t ret; switch(whence) { -#ifdef _POSIX_SOURCE +#if HAS_POSIX case SEEK_SET: if((file->start + offset) > file->end) return -1; ret = lseek(file->fd, file->start + offset, SEEK_SET); @@ -581,7 +586,7 @@ off_t pack_seek(Packfile_t* file, off_t offset, int whence) { ret = flseek(file->fd, file->end - offset - 1, SEEK_SET); if(ret != (file->end - offset)) return -1; break; -#endif +#endif /* HAS_POSIX */ default: ERR("Whence is not one of SEEK_SET, SEEK_CUR or SEEK_END"); return -1; @@ -633,11 +638,11 @@ static void* pack_readfilePack(Packfile_t* file, md5_init(&md5); md5_append(&md5, buf, bytes); md5_finish(&md5, md5val); -#ifdef _POSIX_SOURCE +#if HAS_POSIX if((bytes = read(file->fd, md5fd, 16)) == -1) #else if((bytes = fread(md5fd, 1, 16, file->fp)) == -1) -#endif +#endif /* HAS_POSIX */ WARN("Failure to read MD5, continuing anyway.."); else if(memcmp(md5val, md5fd, 16)) WARN("MD5 gives different value, possible memory corruption, continuing.."); @@ -702,13 +707,13 @@ char** pack_listfiles(const char* packfile, uint32_t* nfiles) { *nfiles = 0; -#ifdef _POSIX_SOURCE +#if HAS_POSIX file.fd = open(packfile, O_RDONLY); if(file.fd == -1) { #else file.fp = fopen(packfile, "rb"); if(file.fp == NULL) { -#endif +#endif /* HAS_POSIX */ ERR("opening %s: %s", packfile, strerror(errno)); return NULL; } @@ -731,11 +736,11 @@ char** pack_listfiles(const char* packfile, uint32_t* nfiles) { } free(buf); -#ifdef _POSIX_SOURCE +#if HAS_POSIX close(file.fd); #else fclose(file.fp); -#endif +#endif /* HAS_POSIX */ return filenames; } @@ -772,14 +777,14 @@ int pack_close(Packfile_t* file) { int i; /* Close files. */ -#ifdef _POSIX_SOURCE +#if HAS_POSIX i = close(file->fd); #else if(file->flags & PACKFILE_FROMCACHE) i = 0; else i = fclose(file->fp); -#endif +#endif /* HAS_POSIX */ /* Free memory. */ free(file); diff --git a/src/pack.h b/src/pack.h index 768b118..581dbf5 100644 --- a/src/pack.h +++ b/src/pack.h @@ -1,10 +1,12 @@ #pragma once + +#include "lcompat.h" + #include -#ifndef _POSIX_SOURCE /* No posix. */ -#include -#endif #include +#if HAS_POSIX #include +#endif /* HAS_POSIX */ struct Packfile_s; typedef struct Packfile_s Packfile_t; diff --git a/src/physics.c b/src/physics.c index e0598f5..75f8b88 100644 --- a/src/physics.c +++ b/src/physics.c @@ -100,7 +100,7 @@ double vect_dot(Vec2* a, Vec2* b) { * SOLID! * ================ */ -#if defined(FREEBSD) +#if HAS_FREEBSD /** * @brief Update the solids position using a euler integration. * @@ -150,7 +150,7 @@ static void simple_update(Solid* obj, const double dt) { vect_cset(&obj->vel, vx, vy); vect_cset(&obj->pos, px, py); } -#endif /* defined(FREEBSD) */ +#endif /* HAS_FREEBSD */ /** * @brief Runge-Kutta method of updating a solid based on it's @@ -175,7 +175,7 @@ static void simple_update(Solid* obj, const double dt) { * a tiny staight line. */ -#if !defined(FREEBSD) +#if !HAS_FREEBSD #define RK4_MIN_H 0.01 /* Minimal pass we want. */ static void rk4_update(Solid* obj, const double dt) { int i, N; /* For iteration and pass calculation. */ @@ -230,7 +230,7 @@ static void rk4_update(Solid* obj, const double dt) { } vect_cset(&obj->pos, px, py); } -#endif /* !defined(FREEBSD) */ +#endif /* !HAS_FREEBSD */ /* Initialize a new solid. */ void solid_init(Solid* dest, const double mass, const double dir, @@ -260,11 +260,11 @@ void solid_init(Solid* dest, const double mass, const double dir, * FreeBSD seems to have a bug with optimizations in rk4_update causing it to * eventually become NaN. */ -#if defined(FREEBSD) +#if HAS_FREEBSD dest->update = simple_update; #else dest->update = rk4_update; -#endif +#endif /* HAS_FREEBSD */ } /* Create a new solid. */ diff --git a/src/rng.c b/src/rng.c index 46f50db..3439742 100644 --- a/src/rng.c +++ b/src/rng.c @@ -6,15 +6,17 @@ * Random numbers are currently generated using the mersenne twister. */ +#include "lcompat.h" + #include #include #include #include #include -#if defined _POSIX_SOURCE +#if HAS_POSIX #include #include -#endif +#endif /* HAS_POSIX */ #include #include "lephisto.h" @@ -40,7 +42,7 @@ void rng_init(void) { int need_init; need_init = 1; /* Initialize by default. */ -#ifdef LINUX +#if HAS_LINUX int fd; fd = open("/dev/urandom", O_RDONLY); /* /dev/urandom is better then time seed. */ if(fd != -1) { @@ -54,7 +56,7 @@ void rng_init(void) { i = rng_timeEntropy(); #else i = rng_timeEntropy(); -#endif +#endif /* HAS_LINUX */ if(need_init) mt_initArray(i); for(i = 0; i < 10; i++) @@ -71,16 +73,16 @@ void rng_init(void) { static uint32_t rng_timeEntropy(void) { int i; -#if defined(_POSIX_SOURCE) +#if HAS_POSIX struct timeval tv; gettimeofday(&tv, NULL); i = tv.tv_sec * 1000000 + tv.tv_usec; -#elif defined(WIN32) +#elif HAS_WIN32 struct _timeb tb; _ftime(&tb); i = tb.time * 1000 + tb.millitm; #else -#error "Needs Implmentation." +#error "Feature needs implementation on this Operating System for Lephisto to work." #endif return i; }