1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8 #ifndef lint
9 static char sccsid[] = "@(#)save.c 8.1 (Berkeley) 05/31/93";
10 #endif /* not lint */
11
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <string.h>
15 #include <termios.h>
16 #include "mille.h"
17
18 #ifndef unctrl
19 #include "unctrl.h"
20 #endif
21
22 # ifdef attron
23 # include <term.h>
24 # define _tty cur_term->Nttyb
25 # endif attron
26
27 /*
28 * @(#)save.c 1.2 (Berkeley) 3/28/83
29 */
30
31 typedef struct stat STAT;
32
33 char *ctime();
34
35 int read(), write();
36
37 /*
38 * This routine saves the current game for use at a later date
39 */
40
save()41 save() {
42
43 extern int errno;
44 reg char *sp;
45 reg int outf;
46 reg time_t *tp;
47 char buf[80];
48 time_t tme;
49 STAT junk;
50
51 tp = &tme;
52 if (Fromfile && getyn(SAMEFILEPROMPT))
53 strcpy(buf, Fromfile);
54 else {
55 over:
56 prompt(FILEPROMPT);
57 leaveok(Board, FALSE);
58 refresh();
59 sp = buf;
60 while ((*sp = readch()) != '\n') {
61 if (*sp == killchar())
62 goto over;
63 else if (*sp == erasechar()) {
64 if (--sp < buf)
65 sp = buf;
66 else {
67 addch('\b');
68 /*
69 * if the previous char was a control
70 * char, cover up two characters.
71 */
72 if (*sp < ' ')
73 addch('\b');
74 clrtoeol();
75 }
76 }
77 else {
78 addstr(unctrl(*sp));
79 ++sp;
80 }
81 refresh();
82 }
83 *sp = '\0';
84 leaveok(Board, TRUE);
85 }
86
87 /*
88 * check for existing files, and confirm overwrite if needed
89 */
90
91 if (sp == buf || (!Fromfile && stat(buf, &junk) > -1
92 && getyn(OVERWRITEFILEPROMPT) == FALSE))
93 return FALSE;
94
95 if ((outf = creat(buf, 0644)) < 0) {
96 error(strerror(errno));
97 return FALSE;
98 }
99 mvwaddstr(Score, ERR_Y, ERR_X, buf);
100 wrefresh(Score);
101 time(tp); /* get current time */
102 strcpy(buf, ctime(tp));
103 for (sp = buf; *sp != '\n'; sp++)
104 continue;
105 *sp = '\0';
106 varpush(outf, write);
107 close(outf);
108 wprintw(Score, " [%s]", buf);
109 wclrtoeol(Score);
110 wrefresh(Score);
111 return TRUE;
112 }
113
114 /*
115 * This does the actual restoring. It returns TRUE if the
116 * backup was made on exiting, in which case certain things must
117 * be cleaned up before the game starts.
118 */
rest_f(file)119 rest_f(file)
120 reg char *file; {
121
122 reg char *sp;
123 reg int inf;
124 char buf[80];
125 STAT sbuf;
126
127 if ((inf = open(file, 0)) < 0) {
128 perror(file);
129 exit(1);
130 }
131 if (fstat(inf, &sbuf) < 0) { /* get file stats */
132 perror(file);
133 exit(1);
134 }
135 varpush(inf, read);
136 close(inf);
137 strcpy(buf, ctime(&sbuf.st_mtime));
138 for (sp = buf; *sp != '\n'; sp++)
139 continue;
140 *sp = '\0';
141 /*
142 * initialize some necessary values
143 */
144 (void)sprintf(Initstr, "%s [%s]\n", file, buf);
145 Fromfile = file;
146 return !On_exit;
147 }
148
149