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