1 /*-
2  * Copyright (c) 1980, 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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)save.c	8.1 (Berkeley) 5/31/93
30  * $FreeBSD: src/games/backgammon/common_source/save.c,v 1.8 1999/11/30 03:48:27 billf Exp $
31  */
32 
33 #include <fcntl.h>
34 #include <errno.h>
35 #include "back.h"
36 
37 static void	norec(const char *);
38 
39 static const char	confirm[] = "Are you sure you want to leave now?";
40 static const char	prompt[] = "Enter a file name:  ";
41 static const char	exist1[] = "The file '";
42 static const char	exist2[] =
43 	"' already exists.\nAre you sure you want to use this file?";
44 static const char	cantuse[] = "\nCan't use ";
45 static const char	saved[] = "This game has been saved on the file '";
46 static const char	type[] = "'.\nType \"backgammon ";
47 static const char	rec[] = "\" to recover your game.\n\n";
48 static const char	cantrec[] = "Can't recover file:  ";
49 
50 void
51 save(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 == tty.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, 0700);
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, dice, sizeof 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(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, dice, sizeof 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