04cfe60ec59f3c5ff18724a75dc8e3c45a37be73
[projects/chimara/chimara.git] / interpreters / git / git_unix.c
1 // $Id: git_unix.c,v 1.5 2004/01/25 18:44:51 iain Exp $
2
3 // unixstrt.c: Unix-specific code for Glulxe.
4 // Designed by Andrew Plotkin <erkyrath@eblong.com>
5 // http://www.eblong.com/zarf/glulx/index.html
6
7 #include "git.h"
8 #include <glk.h>
9 #include <glkstart.h> // This comes with the Glk library.
10
11 #ifdef USE_MMAP
12 #include <fcntl.h>
13 #include <sys/mman.h>
14 #include <sys/stat.h>
15 #include <errno.h>
16 #endif
17
18 // The only command-line argument is the filename.
19 glkunix_argumentlist_t glkunix_arguments[] =
20 {
21     { "", glkunix_arg_ValueFollows, "filename: The game file to load." },
22     { NULL, glkunix_arg_End, NULL }
23 };
24
25 #define CACHE_SIZE (256 * 1024L)
26 #define UNDO_SIZE (512 * 1024L)
27
28 void fatalError (const char * s)
29 {
30     fprintf (stderr, "*** fatal error: %s ***\n", s);
31     exit (1);
32 }
33
34 #ifdef USE_MMAP
35 // Fast loader that uses some fancy Unix features.
36
37 const char * gFilename = 0;
38
39 int glkunix_startup_code(glkunix_startup_t *data)
40 {
41     if (data->argc <= 1)
42     {
43         printf ("usage: git gamefile.ulx\n");
44         return 0;
45     }
46     gFilename = data->argv[1];
47     return 1;
48 }
49
50 void glk_main ()
51 {
52     int          file;
53     struct stat  info;
54     const char * ptr;
55     
56     file = open (gFilename, O_RDONLY);
57     if (file < 0)
58         goto error;
59
60     if (fstat (file, &info) != 0)
61         goto error;
62     
63     if (info.st_size < 256)
64     {
65         fprintf (stderr, "This is too small to be a glulx file.\n");
66         exit (1);
67     }
68
69     ptr = mmap (NULL, info.st_size, PROT_READ, MAP_PRIVATE, file, 0);
70     if (ptr == MAP_FAILED)
71         goto error;
72         
73     git (ptr, info.st_size, CACHE_SIZE, UNDO_SIZE);
74     munmap ((void*) ptr, info.st_size);
75     return;
76     
77 error:
78     perror ("git");
79     exit (errno);
80 }
81
82 #else
83 // Generic loader that should work anywhere.
84
85 strid_t gStream = 0;
86
87 int glkunix_startup_code(glkunix_startup_t *data)
88 {
89     if (data->argc <= 1)
90     {
91         printf ("usage: git gamefile.ulx\n");
92         return 0;
93     }
94     gStream = glkunix_stream_open_pathname ((char*) data->argv[1], 0, 0);
95     return 1;
96 }
97
98 void glk_main ()
99 {
100     if (gStream == NULL)
101         fatalError ("could not open game file");
102
103     gitWithStream (gStream, CACHE_SIZE, UNDO_SIZE);
104 }
105
106 #endif // USE_MMAP