save.c revision 1.1.1.2 1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static char sccsid[] = "@(#)save.c 8.1 (Berkeley) 5/31/93";
36 #endif /* not lint */
37
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <string.h>
41 #include <termios.h>
42 #include "mille.h"
43
44 #ifndef unctrl
45 #include "unctrl.h"
46 #endif
47
48 # ifdef attron
49 # include <term.h>
50 # define _tty cur_term->Nttyb
51 # endif attron
52
53 /*
54 * @(#)save.c 1.2 (Berkeley) 3/28/83
55 */
56
57 typedef struct stat STAT;
58
59 char *ctime();
60
61 int read(), write();
62
63 /*
64 * This routine saves the current game for use at a later date
65 */
66
67 save() {
68
69 extern int errno;
70 reg char *sp;
71 reg int outf;
72 reg time_t *tp;
73 char buf[80];
74 time_t tme;
75 STAT junk;
76
77 tp = &tme;
78 if (Fromfile && getyn(SAMEFILEPROMPT))
79 strcpy(buf, Fromfile);
80 else {
81 over:
82 prompt(FILEPROMPT);
83 leaveok(Board, FALSE);
84 refresh();
85 sp = buf;
86 while ((*sp = readch()) != '\n') {
87 if (*sp == killchar())
88 goto over;
89 else if (*sp == erasechar()) {
90 if (--sp < buf)
91 sp = buf;
92 else {
93 addch('\b');
94 /*
95 * if the previous char was a control
96 * char, cover up two characters.
97 */
98 if (*sp < ' ')
99 addch('\b');
100 clrtoeol();
101 }
102 }
103 else {
104 addstr(unctrl(*sp));
105 ++sp;
106 }
107 refresh();
108 }
109 *sp = '\0';
110 leaveok(Board, TRUE);
111 }
112
113 /*
114 * check for existing files, and confirm overwrite if needed
115 */
116
117 if (sp == buf || (!Fromfile && stat(buf, &junk) > -1
118 && getyn(OVERWRITEFILEPROMPT) == FALSE))
119 return FALSE;
120
121 if ((outf = creat(buf, 0644)) < 0) {
122 error(strerror(errno));
123 return FALSE;
124 }
125 mvwaddstr(Score, ERR_Y, ERR_X, buf);
126 wrefresh(Score);
127 time(tp); /* get current time */
128 strcpy(buf, ctime(tp));
129 for (sp = buf; *sp != '\n'; sp++)
130 continue;
131 *sp = '\0';
132 varpush(outf, write);
133 close(outf);
134 wprintw(Score, " [%s]", buf);
135 wclrtoeol(Score);
136 wrefresh(Score);
137 return TRUE;
138 }
139
140 /*
141 * This does the actual restoring. It returns TRUE if the
142 * backup was made on exiting, in which case certain things must
143 * be cleaned up before the game starts.
144 */
145 rest_f(file)
146 reg char *file; {
147
148 reg char *sp;
149 reg int inf;
150 char buf[80];
151 STAT sbuf;
152
153 if ((inf = open(file, 0)) < 0) {
154 perror(file);
155 exit(1);
156 }
157 if (fstat(inf, &sbuf) < 0) { /* get file stats */
158 perror(file);
159 exit(1);
160 }
161 varpush(inf, read);
162 close(inf);
163 strcpy(buf, ctime(&sbuf.st_mtime));
164 for (sp = buf; *sp != '\n'; sp++)
165 continue;
166 *sp = '\0';
167 /*
168 * initialize some necessary values
169 */
170 (void)sprintf(Initstr, "%s [%s]\n", file, buf);
171 Fromfile = file;
172 return !On_exit;
173 }
174
175