1 /*	$OpenBSD: save.c,v 1.11 2014/11/16 04:49:48 guenther Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <ctype.h>
33 #include <limits.h>
34 #include <errno.h>
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 -s ";
45 static const char rec[] = "\" to recover your game.\n\n";
46 static const char cantrec[] = "Can't recover file:  ";
47 
48 void
49 save(n)
50 	int     n;
51 {
52 	int     fdesc;
53 	char   *fs;
54 	char    fname[PATH_MAX];
55 	int     r, c, i;
56 
57 	if (n) {
58 		move(20, 0);
59 		clrtobot();
60 		addstr(confirm);
61 		if (!yorn(0))
62 			return;
63 	}
64 	cflag = 1;
65 	for (;;) {
66 		addstr(prompt);
67 		fs = fname;
68 		while ((i = readc()) != '\n') {
69 			if (i == KEY_BACKSPACE || i == 0177) {
70 				if (fs > fname) {
71 					fs--;
72 					getyx(stdscr, r, c);
73 					move(r, c - 1);
74 				} else
75 					beep();
76 				continue;
77 			}
78 			if (fs - fname < sizeof(fname) - 1) {
79 				if (isascii(i)) {
80 					*fs = i;
81 					addch(*fs++);
82 				} else
83 					beep();
84 			} else
85 				beep();
86 		}
87 		*fs = '\0';
88 		if ((fdesc = open(fname, O_RDWR)) == -1 && errno == ENOENT) {
89 			if ((fdesc = creat(fname, 0600)) != -1)
90 				break;
91 		}
92 		if (fdesc != -1) {
93 			move(18, 0);
94 			clrtobot();
95 			printw("%s%s%s", exist1, fname, exist2);
96 			cflag = 0;
97 			close(fdesc);
98 			if (yorn(0)) {
99 				unlink(fname);
100 				fdesc = creat(fname, 0600);
101 				break;
102 			} else {
103 				cflag = 1;
104 				continue;
105 			}
106 		}
107 		printw("%s%s.\n", cantuse, fname);
108 		cflag = 1;
109 	}
110 	write(fdesc, board, sizeof(board));
111 	write(fdesc, off, sizeof(off));
112 	write(fdesc, in, sizeof(in));
113 	write(fdesc, dice, sizeof(dice));
114 	write(fdesc, &cturn, sizeof(cturn));
115 	write(fdesc, &dlast, sizeof(dlast));
116 	write(fdesc, &pnum, sizeof(pnum));
117 	write(fdesc, &rscore, sizeof(rscore));
118 	write(fdesc, &wscore, sizeof(wscore));
119 	write(fdesc, &gvalue, sizeof(gvalue));
120 	write(fdesc, &raflag, sizeof(raflag));
121 	close(fdesc);
122 	move(18, 0);
123 	printw("%s%s%s%s%s", saved, fname, type, fname, rec);
124 	clrtobot();
125 	getout(0);
126 }
127 
128 void
129 recover(s)
130 	const char   *s;
131 {
132 	int     fdesc;
133 
134 	if ((fdesc = open(s, O_RDONLY)) == -1)
135 		norec(s);
136 	read(fdesc, board, sizeof(board));
137 	read(fdesc, off, sizeof(off));
138 	read(fdesc, in, sizeof(in));
139 	read(fdesc, dice, sizeof(dice));
140 	read(fdesc, &cturn, sizeof(cturn));
141 	read(fdesc, &dlast, sizeof(dlast));
142 	read(fdesc, &pnum, sizeof(pnum));
143 	read(fdesc, &rscore, sizeof(rscore));
144 	read(fdesc, &wscore, sizeof(wscore));
145 	read(fdesc, &gvalue, sizeof(gvalue));
146 	read(fdesc, &raflag, sizeof(raflag));
147 	close(fdesc);
148 	rflag = 1;
149 }
150 
151 void
152 norec(s)
153 	const char   *s;
154 {
155 	const char   *c;
156 
157 	addstr(cantrec);
158 	c = s;
159 	while (*c != '\0')
160 		addch(*c++);
161 	getout(0);
162 }
163