1 /*	@(#)save.c	8.1 (Berkeley) 5/31/93				*/
2 /*	$NetBSD: save.c,v 1.16 2012/10/13 19:19:39 dholland Exp $	*/
3 
4 /*
5  * Copyright (c) 1980, 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 <errno.h>
34 
35 #include "back.h"
36 
37 static const char confirm[] = "Are you sure you want to leave now?";
38 static const char prompt[] = "Enter a file name:  ";
39 static const char exist1[] = "The file '";
40 static const char exist2[] =
41 "' already exists.\nAre you sure you want to use this file?";
42 static const char cantuse[] = "\nCan't use ";
43 static const char saved[] = "This game has been saved on the file '";
44 static const char type[] = "'.\nType \"backgammon ";
45 static const char rec[] = "\" to recover your game.\n\n";
46 static const char cantrec[] = "Can't recover file:  ";
47 
48 static void norec(const char *) __dead2;
49 
50 void
51 save(struct move *mm, int n)
52 {
53 	int     fdesc;
54 	char   *fs;
55 	char    fname[50];
56 
57 	if (n) {
58 		if (tflag) {
59 			curmove(20, 0);
60 			clend();
61 		} else
62 			writec('\n');
63 		writel(confirm);
64 		if (!yorn(0))
65 			return;
66 	}
67 	cflag = 1;
68 	for (;;) {
69 		writel(prompt);
70 		fs = fname;
71 		while ((*fs = readc()) != '\n') {
72 			if (*fs == old.c_cc[VERASE]) {
73 				if (fs > fname) {
74 					fs--;
75 					if (tflag)
76 						curmove(curr, curc - 1);
77 					else
78 						writec(*fs);
79 				} else
80 					writec('\007');
81 				continue;
82 			}
83 			writec(*fs++);
84 		}
85 		*fs = '\0';
86 		if ((fdesc = open(fname, O_RDWR)) == -1 && errno == ENOENT) {
87 			if ((fdesc = creat(fname, 0600)) != -1)
88 				break;
89 		}
90 		if (fdesc != -1) {
91 			if (tflag) {
92 				curmove(18, 0);
93 				clend();
94 			} else
95 				writec('\n');
96 			writel(exist1);
97 			writel(fname);
98 			writel(exist2);
99 			cflag = 0;
100 			close(fdesc);
101 			if (yorn(0)) {
102 				unlink(fname);
103 				fdesc = creat(fname, 0600);
104 				break;
105 			} else {
106 				cflag = 1;
107 				continue;
108 			}
109 		}
110 		writel(cantuse);
111 		writel(fname);
112 		writel(".\n");
113 		cflag = 1;
114 	}
115 	write(fdesc, board, sizeof board);
116 	write(fdesc, off, sizeof off);
117 	write(fdesc, in, sizeof in);
118 	write(fdesc, mm->dice, sizeof mm->dice);
119 	write(fdesc, &cturn, sizeof cturn);
120 	write(fdesc, &dlast, sizeof dlast);
121 	write(fdesc, &pnum, sizeof pnum);
122 	write(fdesc, &rscore, sizeof rscore);
123 	write(fdesc, &wscore, sizeof wscore);
124 	write(fdesc, &gvalue, sizeof gvalue);
125 	write(fdesc, &raflag, sizeof raflag);
126 	close(fdesc);
127 	if (tflag)
128 		curmove(18, 0);
129 	writel(saved);
130 	writel(fname);
131 	writel(type);
132 	writel(fname);
133 	writel(rec);
134 	if (tflag)
135 		clend();
136 	getout(0);
137 }
138 
139 void
140 recover(struct move *mm, const char *s)
141 {
142 	int     fdesc;
143 
144 	if ((fdesc = open(s, O_RDONLY)) == -1)
145 		norec(s);
146 	read(fdesc, board, sizeof board);
147 	read(fdesc, off, sizeof off);
148 	read(fdesc, in, sizeof in);
149 	read(fdesc, mm->dice, sizeof mm->dice);
150 	read(fdesc, &cturn, sizeof cturn);
151 	read(fdesc, &dlast, sizeof dlast);
152 	read(fdesc, &pnum, sizeof pnum);
153 	read(fdesc, &rscore, sizeof rscore);
154 	read(fdesc, &wscore, sizeof wscore);
155 	read(fdesc, &gvalue, sizeof gvalue);
156 	read(fdesc, &raflag, sizeof raflag);
157 	close(fdesc);
158 	rflag = 1;
159 }
160 
161 static void
162 norec(const char *s)
163 {
164 	const char   *c;
165 
166 	tflag = 0;
167 	writel(cantrec);
168 	c = s;
169 	while (*c != '\0')
170 		writec(*c++);
171 	getout(0);
172 }
173