2 * save and restore routines
4 * @(#)save.c 3.9 (Berkeley) 6/16/81
6 * Rogue: Exploring the Dungeons of Doom
7 * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
10 * See the file LICENSE.TXT for full copyright and licensing information.
15 #include <sys/types.h>
24 typedef struct stat STAT
;
26 extern char version
[], encstr
[];
40 if (file_name
[0] != '\0')
42 msg("Save file (%s)? ", file_name
);
46 } while (c
!= 'n' && c
!= 'N' && c
!= 'y' && c
!= 'Y');
48 if (c
== 'y' || c
== 'Y')
50 msg("File name: %s", file_name
);
60 if (get_str(buf
, cw
) == QUIT
)
65 strcpy(file_name
, buf
);
67 if ((savef
= fopen(file_name
, "w")) == NULL
)
68 msg(strerror(errno
)); /* fake perror() */
69 } while (savef
== NULL
);
72 * write out encrpyted file (after a stat)
73 * The fwrite is to force allocation of the buffer before the write
75 if (save_file(savef
) != 0)
77 msg("Save game failed!");
84 * automatically save a file. This is used if a HUP signal is
93 for (i
= 0; i
< NSIG
; i
++)
95 if (file_name
[0] != '\0' && (savef
= fopen(file_name
, "w")) != NULL
)
102 * write the saved game on the file
105 register FILE *savef
;
110 wmove(cw
, LINES
-1, 0);
116 encwrite(buf
,80,savef
);
118 strcpy(buf
,"R36 2\n");
119 encwrite(buf
,80,savef
);
121 sprintf(buf
,"%d x %d\n", LINES
, COLS
);
122 encwrite(buf
,80,savef
);
124 ret
= rs_save_file(savef
);
136 extern char **environ
;
139 int rogue_version
= 0, savefile_version
= 0;
141 if (strcmp(file
, "-r") == 0)
143 if ((inf
= open(file
, 0)) < 0)
150 encread(buf
, 80, inf
);
152 if (strcmp(buf
, version
) != 0)
154 printf("Sorry, saved game is out of date.\n");
158 encread(buf
, 80, inf
);
159 sscanf(buf
, "R%d %d\n", &rogue_version
, &savefile_version
);
161 if ((rogue_version
!= 36) && (savefile_version
!= 2))
163 printf("Sorry, saved game format is out of date.\n");
168 sscanf(buf
,"%d x %d\n",&slines
, &scols
);
171 * we do not close the file so that we will have a hold of the
172 * inode for as long as possible
180 printf("Sorry, original game was played on a screen with %d lines.\n",slines
);
181 printf("Current screen only has %d lines. Unable to restore game\n",LINES
);
188 printf("Sorry, original game was played on a screen with %d columns.\n",scols
);
189 printf("Current screen only has %d columns. Unable to restore game\n",COLS
);
193 cw
= newwin(LINES
, COLS
, 0, 0);
194 mw
= newwin(LINES
, COLS
, 0, 0);
195 hw
= newwin(LINES
, COLS
, 0, 0);
199 mvwprintw(cw
, 0, 0, "%s", file
);
201 if (rs_restore_file(inf
) != 0)
204 printf("Cannot restore file\n");
208 if (!wizard
&& (md_unlink_open_file(file
, inf
) < 0))
211 printf("Cannot unlink file\n");
216 strcpy(file_name
, file
);
218 clearok(curscr
, TRUE
);
228 * perform an encrypted write
230 encwrite(starta
, size
, outf
)
231 register void *starta
;
236 register char *start
= starta
;
237 unsigned int o_size
= size
;
242 if (putc(*start
++ ^ *ep
++, outf
) == EOF
)
243 return(o_size
- size
);
249 return(o_size
- size
);
253 * perform an encrypted read
255 encread(starta
, size
, inf
)
256 register void *starta
;
261 register int read_size
;
262 register char *start
= starta
;
264 if ((read_size
= read(inf
, start
, size
)) == -1 || read_size
== 0)