00001
00013 #include <string.h>
00014 #include <ctype.h>
00015 #include <stdlib.h>
00016 #include <time.h>
00017 #include <stdio.h>
00018 #include <errno.h>
00019 #include "advent.h"
00020 #include "advdec.h"
00021
00022 #ifndef TEXTDIR
00023 #define TEXTDIR ""
00024 #endif
00025
00026 char textdir[] = TEXTDIR;
00027
00028
00029 _PROTOTYPE(int main, (int, char **));
00030 _PROTOTYPE(static void opentxt, (void));
00031 _PROTOTYPE(static void file_error, (char *));
00032
00033 int main(argc, argv)
00034 int argc;
00035 char **argv;
00036 {
00037 opentxt();
00038 initialize();
00039 rspeak(325);
00040 if (argc == 2)
00041 restore(argv[1]);
00042 else {
00043 g.hinted[3] = yes(65, 1, 0);
00044 g.limit = (g.hinted[3] ? 800 : 550);
00045 }
00046 gaveup = FALSE;
00047 srand((unsigned) time(NULL));
00048 while (!gaveup)
00049 turn();
00050 fclose(fd1);
00051 fclose(fd2);
00052 fclose(fd3);
00053 fclose(fd4);
00054 return (EXIT_SUCCESS);
00055 }
00056
00057
00058
00059
00060 static void opentxt()
00061 {
00062 static char filename[sizeof(textdir) + 16];
00063 static FILE **fp[] = {0, &fd1, &fd2, &fd3, &fd4};
00064 int i;
00065 for (i = 1; i <= 4; i++) {
00066 sprintf(filename, "%sadvent%d.dat", textdir, i);
00067 *fp[i] = fopen(filename, "r");
00068 if (!*fp[i])
00069 file_error(filename);
00070 }
00071 }
00072
00073
00074
00075
00076 void saveadv(username)
00077 char *username;
00078 {
00079 int cnt;
00080 FILE *savefd;
00081
00082 savefd = fopen(username, "wb");
00083 if (savefd == NULL) {
00084 perror(username);
00085 return;
00086 }
00087 cnt = fwrite((void *) &g, 1, sizeof(struct playinfo), savefd);
00088 if (cnt != sizeof(struct playinfo)) {
00089 fprintf(stderr, "wrote %d of %u bytes\n",
00090 cnt, (unsigned) sizeof(struct playinfo));
00091 if (ferror(savefd)) {
00092 fprintf(stderr, "errno is: 0x%.4x\n", errno);
00093 perror(username);
00094 }
00095 }
00096 if (fclose(savefd) == -1) {
00097 perror(username);
00098 }
00099 printf("Saved in %s.\n", username);
00100 return;
00101 }
00102
00103
00104
00105
00106 void restore(username)
00107 char *username;
00108 {
00109 int cnt;
00110 FILE *restfd;
00111
00112 restfd = fopen(username, "rb");
00113 if (restfd == NULL)
00114 file_error(username);
00115 cnt = fread((void *) &g, 1, sizeof(struct playinfo), restfd);
00116 if (cnt != sizeof(struct playinfo)) {
00117 fprintf(stderr, "read %d bytes, expected %u\n",
00118 cnt, (unsigned) sizeof(struct playinfo));
00119 if (ferror(restfd)) {
00120 fprintf(stderr, "errno is: 0x%.4x\n", errno);
00121 perror(username);
00122 }
00123 }
00124 if (fclose(restfd) == -1) {
00125 perror(username);
00126 }
00127 printf("Restored from %s.\n", username);
00128 return;
00129 }
00130
00131 static void file_error(filename)
00132 char *filename;
00133 {
00134 perror(filename);
00135 exit(EXIT_FAILURE);
00136 }