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