Mercurial > hg > early-roguelike
comparison arogue7/save.c @ 279:d3968e9cb98d
Use C stdio functions for score files and save files.
Switching from Unix file descriptor operations to C standard FILE*
functions will reduce portability problems.
| author | John "Elwin" Edwards |
|---|---|
| date | Fri, 15 Sep 2017 19:57:54 -0400 |
| parents | 3d4252fa2ed3 |
| children | 70aa5808c782 |
comparison
equal
deleted
inserted
replaced
| 278:c222f9d56776 | 279:d3968e9cb98d |
|---|---|
| 32 #include "sys/window.h" | 32 #include "sys/window.h" |
| 33 extern struct uwdata wdata; | 33 extern struct uwdata wdata; |
| 34 #endif | 34 #endif |
| 35 | 35 |
| 36 #if u370 || uts | 36 #if u370 || uts |
| 37 #define ENCREAD(b,n,fd) read(fd,b,n) | 37 #define ENCREAD(b,n,f) read(md_fileno(f),b,n) |
| 38 #define ENCWRITE(b,n,fd) write(fd,b,n) | 38 #define ENCWRITE(b,n,f) write(md_fileno(f),b,n) |
| 39 #endif | 39 #endif |
| 40 #ifndef ENCREAD | 40 #ifndef ENCREAD |
| 41 #define ENCREAD encread | 41 #define ENCREAD encread |
| 42 #define ENCWRITE encwrite | 42 #define ENCWRITE encwrite |
| 43 #endif | 43 #endif |
| 44 | 44 |
| 45 bool save_file(int savefd); | 45 bool save_file(FILE *savef); |
| 46 | 46 |
| 47 typedef struct stat STAT; | 47 typedef struct stat STAT; |
| 48 | 48 |
| 49 extern char version[], encstr[]; | 49 extern char version[], encstr[]; |
| 50 /* extern bool _endwin; */ | 50 /* extern bool _endwin; */ |
| 53 STAT sbuf; | 53 STAT sbuf; |
| 54 | 54 |
| 55 bool | 55 bool |
| 56 save_game(void) | 56 save_game(void) |
| 57 { | 57 { |
| 58 register int savefd; | 58 FILE *savefi = NULL; |
| 59 register int c; | 59 register int c; |
| 60 char buf[LINELEN]; | 60 char buf[LINELEN]; |
| 61 | 61 |
| 62 /* | 62 /* |
| 63 * get file name | 63 * get file name |
| 98 msg(""); | 98 msg(""); |
| 99 return FALSE; | 99 return FALSE; |
| 100 } | 100 } |
| 101 strcpy(file_name, buf); | 101 strcpy(file_name, buf); |
| 102 gotfile: | 102 gotfile: |
| 103 if ((savefd = open(file_name, O_WRONLY|O_CREAT|O_TRUNC,0666)) < 0) | 103 if ((savefi = fopen(file_name, "w")) == NULL) |
| 104 { | 104 { |
| 105 msg(strerror(errno)); /* fake perror() */ | 105 msg(strerror(errno)); /* fake perror() */ |
| 106 if (use_savedir) | 106 if (use_savedir) |
| 107 return FALSE; | 107 return FALSE; |
| 108 } | 108 } |
| 109 } while (savefd < 0); | 109 } while (savefi == NULL); |
| 110 | 110 |
| 111 /* | 111 /* |
| 112 * write out encrpyted file (after a stat) | 112 * write out encrpyted file (after a stat) |
| 113 */ | 113 */ |
| 114 if (save_file(savefd) == FALSE) { | 114 if (save_file(savefi) == FALSE) { |
| 115 msg("Cannot create save file."); | 115 msg("Cannot create save file."); |
| 116 md_unlink(file_name); | 116 md_unlink(file_name); |
| 117 return(FALSE); | 117 return(FALSE); |
| 118 } | 118 } |
| 119 else return(TRUE); | 119 else return(TRUE); |
| 124 * recieved | 124 * recieved |
| 125 */ | 125 */ |
| 126 void | 126 void |
| 127 auto_save(int sig) | 127 auto_save(int sig) |
| 128 { | 128 { |
| 129 register int savefd; | 129 FILE *savefi; |
| 130 register int i; | 130 register int i; |
| 131 | 131 |
| 132 for (i = 0; i < NSIG; i++) | 132 for (i = 0; i < NSIG; i++) |
| 133 signal(i, SIG_IGN); | 133 signal(i, SIG_IGN); |
| 134 if (file_name[0] != '\0' && | 134 if (file_name[0] != '\0' && |
| 135 pstats.s_hpt > 0 && | 135 pstats.s_hpt > 0 && |
| 136 (savefd = open(file_name, O_WRONLY|O_CREAT|O_TRUNC, 0600)) >= 0) | 136 (savefi = fopen(file_name, "w")) != NULL) |
| 137 save_file(savefd); | 137 save_file(savefi); |
| 138 endwin(); | 138 endwin(); |
| 139 #ifdef PC7300 | 139 #ifdef PC7300 |
| 140 endhardwin(); | 140 endhardwin(); |
| 141 #endif | 141 #endif |
| 142 exit(1); | 142 exit(1); |
| 144 | 144 |
| 145 /* | 145 /* |
| 146 * write the saved game on the file | 146 * write the saved game on the file |
| 147 */ | 147 */ |
| 148 bool | 148 bool |
| 149 save_file(int savefd) | 149 save_file(FILE *savef) |
| 150 { | 150 { |
| 151 register unsigned num_to_write, num_written; | 151 register unsigned num_to_write, num_written; |
| 152 FILE *savef; | |
| 153 int ret; | 152 int ret; |
| 154 | 153 |
| 155 wmove(cw, lines-1, 0); | 154 wmove(cw, lines-1, 0); |
| 156 draw(cw); | 155 draw(cw); |
| 157 lseek(savefd, 0L, 0); | 156 fseek(savef, 0L, SEEK_SET); |
| 158 fstat(savefd, &sbuf); | 157 stat(file_name, &sbuf); |
| 159 num_to_write = strlen(version) + 1; | 158 num_to_write = strlen(version) + 1; |
| 160 num_written = ENCWRITE(version, num_to_write, savefd); | 159 num_written = ENCWRITE(version, num_to_write, savef); |
| 161 sprintf(prbuf,"%d x %d\n", LINES, COLS); | 160 sprintf(prbuf,"%d x %d\n", LINES, COLS); |
| 162 ENCWRITE(prbuf,80,savefd); | 161 ENCWRITE(prbuf,80,savef); |
| 163 savef = (FILE *) md_fdopen(savefd,"wb"); | |
| 164 ret = rs_save_file(savef); | 162 ret = rs_save_file(savef); |
| 165 fclose(savef); | 163 fclose(savef); |
| 166 if (num_to_write == num_written && ret == 0) return(TRUE); | 164 if (num_to_write == num_written && ret == 0) return(TRUE); |
| 167 else return(FALSE); | 165 else return(FALSE); |
| 168 } | 166 } |
| 169 | 167 |
| 170 bool | 168 bool |
| 171 restore(char *file, char *envp[]) | 169 restore(char *file, char *envp[]) |
| 172 { | 170 { |
| 173 register int inf; | 171 FILE *inf; |
| 174 extern char **environ; | 172 extern char **environ; |
| 175 char buf[LINELEN]; | 173 char buf[LINELEN]; |
| 176 STAT sbuf2; | 174 STAT sbuf2; |
| 177 int oldcol, oldline; /* Old number of columns and lines */ | 175 int oldcol, oldline; /* Old number of columns and lines */ |
| 178 | 176 |
| 179 if (strcmp(file, "-r") == 0) | 177 if (strcmp(file, "-r") == 0) |
| 180 file = file_name; | 178 file = file_name; |
| 181 if ((inf = open(file, 0)) < 0) | 179 if ((inf = fopen(file, "r")) == NULL) |
| 182 { | 180 { |
| 183 if (use_savedir && errno == ENOENT) | 181 if (use_savedir && errno == ENOENT) |
| 184 { | 182 { |
| 185 return TRUE; | 183 return TRUE; |
| 186 } | 184 } |
| 200 * Get the lines and columns from the previous game | 198 * Get the lines and columns from the previous game |
| 201 */ | 199 */ |
| 202 | 200 |
| 203 ENCREAD(buf, 80, inf); | 201 ENCREAD(buf, 80, inf); |
| 204 sscanf(buf, "%d x %d\n", &oldline, &oldcol); | 202 sscanf(buf, "%d x %d\n", &oldline, &oldcol); |
| 205 fstat(inf, &sbuf2); | 203 stat(file, &sbuf2); |
| 206 fflush(stdout); | 204 fflush(stdout); |
| 207 | 205 |
| 208 initscr(); | 206 initscr(); |
| 209 | 207 |
| 210 if (COLS < oldcol || LINES < oldline) { | 208 if (COLS < oldcol || LINES < oldline) { |
| 226 keypad(msgw,1); | 224 keypad(msgw,1); |
| 227 | 225 |
| 228 if (rs_restore_file(inf) != 0) | 226 if (rs_restore_file(inf) != 0) |
| 229 { | 227 { |
| 230 printf("Cannot restore file\n"); | 228 printf("Cannot restore file\n"); |
| 231 close(inf); | 229 fclose(inf); |
| 232 return(FALSE); | 230 return(FALSE); |
| 233 } | 231 } |
| 234 | 232 |
| 235 cols = COLS; | 233 cols = COLS; |
| 236 lines = LINES; | 234 lines = LINES; |
| 263 #define ENCWBSIZ 1024 | 261 #define ENCWBSIZ 1024 |
| 264 /* | 262 /* |
| 265 * perform an encrypted write | 263 * perform an encrypted write |
| 266 */ | 264 */ |
| 267 int | 265 int |
| 268 encwrite(char *start, unsigned int size, int outf) | 266 encwrite(char *start, unsigned int size, FILE *outf) |
| 269 { | 267 { |
| 270 register char *ep; | 268 register char *ep; |
| 271 register int i = 0; | 269 register int i = 0; |
| 272 int num_written = 0; | 270 int num_written = 0; |
| 273 auto char buf[ENCWBSIZ]; | 271 auto char buf[ENCWBSIZ]; |
| 279 buf[i++] = *start++ ^ *ep++ ; | 277 buf[i++] = *start++ ^ *ep++ ; |
| 280 if (*ep == '\0') | 278 if (*ep == '\0') |
| 281 ep = encstr; | 279 ep = encstr; |
| 282 | 280 |
| 283 if (i == ENCWBSIZ || size == 0) { | 281 if (i == ENCWBSIZ || size == 0) { |
| 284 if (write(outf, buf, (unsigned)i) < i) | 282 if (fwrite(buf, 1, (unsigned)i, outf) < i) |
| 285 return(num_written); | 283 return(num_written); |
| 286 else { | 284 else { |
| 287 num_written += i; | 285 num_written += i; |
| 288 i = 0; | 286 i = 0; |
| 289 } | 287 } |
| 294 | 292 |
| 295 /* | 293 /* |
| 296 * perform an encrypted read | 294 * perform an encrypted read |
| 297 */ | 295 */ |
| 298 int | 296 int |
| 299 encread(char *start, unsigned int size, int inf) | 297 encread(char *start, unsigned int size, FILE *inf) |
| 300 { | 298 { |
| 301 register char *ep; | 299 register char *ep; |
| 302 register int read_size; | 300 register int read_size; |
| 303 | 301 |
| 304 if ((read_size = read(inf, start, size)) == -1 || read_size == 0) | 302 if ((read_size = fread(start, 1, size, inf)) == 0) |
| 305 return read_size; | 303 return read_size; |
| 306 | 304 |
| 307 ep = encstr; | 305 ep = encstr; |
| 308 | 306 |
| 309 size = read_size; | 307 size = read_size; |
