X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=interpreters%2Fgit%2Fgit_unix.c;fp=interpreters%2Fgit%2Fgit_unix.c;h=04cfe60ec59f3c5ff18724a75dc8e3c45a37be73;hb=147a8cbf17f2b3379277bf7d37cda9866510f16c;hp=0000000000000000000000000000000000000000;hpb=7de488aa6a1709a4d5c59b5ff59862105c1748c5;p=rodin%2Fchimara.git diff --git a/interpreters/git/git_unix.c b/interpreters/git/git_unix.c new file mode 100644 index 0000000..04cfe60 --- /dev/null +++ b/interpreters/git/git_unix.c @@ -0,0 +1,106 @@ +// $Id: git_unix.c,v 1.5 2004/01/25 18:44:51 iain Exp $ + +// unixstrt.c: Unix-specific code for Glulxe. +// Designed by Andrew Plotkin +// http://www.eblong.com/zarf/glulx/index.html + +#include "git.h" +#include +#include // This comes with the Glk library. + +#ifdef USE_MMAP +#include +#include +#include +#include +#endif + +// The only command-line argument is the filename. +glkunix_argumentlist_t glkunix_arguments[] = +{ + { "", glkunix_arg_ValueFollows, "filename: The game file to load." }, + { NULL, glkunix_arg_End, NULL } +}; + +#define CACHE_SIZE (256 * 1024L) +#define UNDO_SIZE (512 * 1024L) + +void fatalError (const char * s) +{ + fprintf (stderr, "*** fatal error: %s ***\n", s); + exit (1); +} + +#ifdef USE_MMAP +// Fast loader that uses some fancy Unix features. + +const char * gFilename = 0; + +int glkunix_startup_code(glkunix_startup_t *data) +{ + if (data->argc <= 1) + { + printf ("usage: git gamefile.ulx\n"); + return 0; + } + gFilename = data->argv[1]; + return 1; +} + +void glk_main () +{ + int file; + struct stat info; + const char * ptr; + + file = open (gFilename, O_RDONLY); + if (file < 0) + goto error; + + if (fstat (file, &info) != 0) + goto error; + + if (info.st_size < 256) + { + fprintf (stderr, "This is too small to be a glulx file.\n"); + exit (1); + } + + ptr = mmap (NULL, info.st_size, PROT_READ, MAP_PRIVATE, file, 0); + if (ptr == MAP_FAILED) + goto error; + + git (ptr, info.st_size, CACHE_SIZE, UNDO_SIZE); + munmap ((void*) ptr, info.st_size); + return; + +error: + perror ("git"); + exit (errno); +} + +#else +// Generic loader that should work anywhere. + +strid_t gStream = 0; + +int glkunix_startup_code(glkunix_startup_t *data) +{ + if (data->argc <= 1) + { + printf ("usage: git gamefile.ulx\n"); + return 0; + } + gStream = glkunix_stream_open_pathname ((char*) data->argv[1], 0, 0); + return 1; +} + +void glk_main () +{ + if (gStream == NULL) + fatalError ("could not open game file"); + + gitWithStream (gStream, CACHE_SIZE, UNDO_SIZE); +} + +#endif // USE_MMAP