Intel Pro/1000 driver written by Niek Linnenbank.
[minix.git] / commands / advent / advent.c
blob34145ef94ac692d9ca2fef27db6b3dba902a6fe3
1 /** Adventure translated from Fortran to "C"
2 and ported to Minix by:
3 Robert R. Hall
4 San Diego, Calif 92115
5 hall@crash.cts.com
6 */
8 /** program ADVENT.C *
9 * "advent.c" allocates GLOBAL storage space by *
10 * #defining EXTERN before #including "advdec.h". */
13 #include <string.h>
14 #include <ctype.h>
15 #include <stdlib.h>
16 #include <time.h>
17 #include <stdio.h>
18 #include <errno.h>
19 #include "advent.h" /* #define preprocessor equates */
20 #include "advdec.h"
22 #ifndef TEXTDIR
23 #define TEXTDIR ""
24 #endif
26 char textdir[] = TEXTDIR; /* directory where text files
27 live. */
29 _PROTOTYPE(int main, (int, char **));
30 _PROTOTYPE(static void opentxt, (void));
31 _PROTOTYPE(static void file_error, (char *));
33 int main(argc, argv)
34 int argc;
35 char **argv;
37 opentxt();
38 initialize();
39 rspeak(325);
40 if (argc == 2)
41 restore(argv[1]);
42 else {
43 g.hinted[3] = yes(65, 1, 0);
44 g.limit = (g.hinted[3] ? 800 : 550);
46 gaveup = FALSE;
47 srand((unsigned) time(NULL)); /* seed random */
48 while (!gaveup)
49 turn();
50 fclose(fd1);
51 fclose(fd2);
52 fclose(fd3);
53 fclose(fd4);
54 return (EXIT_SUCCESS); /* exit = ok */
55 } /* main */
58 Open advent?.txt files
60 static void opentxt()
62 static char filename[sizeof(textdir) + 16];
63 static FILE **fp[] = {0, &fd1, &fd2, &fd3, &fd4};
64 int i;
65 for (i = 1; i <= 4; i++) {
66 sprintf(filename, "%sadvent%d.dat", textdir, i);
67 *fp[i] = fopen(filename, "r");
68 if (!*fp[i])
69 file_error(filename);
74 save adventure game
76 void saveadv(username)
77 char *username;
79 int cnt;
80 FILE *savefd;
82 savefd = fopen(username, "wb");
83 if (savefd == NULL) {
84 perror(username);
85 return;
87 cnt = fwrite((void *) &g, 1, sizeof(struct playinfo), savefd);
88 if (cnt != sizeof(struct playinfo)) {
89 fprintf(stderr, "wrote %d of %u bytes\n",
90 cnt, (unsigned) sizeof(struct playinfo));
91 if (ferror(savefd)) {
92 fprintf(stderr, "errno is: 0x%.4x\n", errno);
93 perror(username);
96 if (fclose(savefd) == -1) {
97 perror(username);
99 printf("Saved in %s.\n", username);
100 return;
104 restore saved game handler
106 void restore(username)
107 char *username;
109 int cnt;
110 FILE *restfd;
112 restfd = fopen(username, "rb");
113 if (restfd == NULL)
114 file_error(username);
115 cnt = fread((void *) &g, 1, sizeof(struct playinfo), restfd);
116 if (cnt != sizeof(struct playinfo)) {
117 fprintf(stderr, "read %d bytes, expected %u\n",
118 cnt, (unsigned) sizeof(struct playinfo));
119 if (ferror(restfd)) {
120 fprintf(stderr, "errno is: 0x%.4x\n", errno);
121 perror(username);
124 if (fclose(restfd) == -1) {
125 perror(username);
127 printf("Restored from %s.\n", username);
128 return;
131 static void file_error(filename)
132 char *filename;
134 perror(filename);
135 exit(EXIT_FAILURE);