1 /* $OpenBSD: save.c,v 1.14 2015/11/30 08:19:25 tb 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
save(int n)49 save(int n)
50 {
51 int fdesc;
52 char *fs;
53 char fname[PATH_MAX];
54 int r, c, i;
55
56 if (n) {
57 move(20, 0);
58 clrtobot();
59 addstr(confirm);
60 if (!yorn(0))
61 return;
62 }
63 cflag = 1;
64 for (;;) {
65 addstr(prompt);
66 fs = fname;
67 while ((i = readc()) != '\n') {
68 if (i == KEY_BACKSPACE || i == 0177) {
69 if (fs > fname) {
70 fs--;
71 getyx(stdscr, r, c);
72 move(r, c - 1);
73 } else
74 beep();
75 continue;
76 }
77 if (fs - fname < sizeof(fname) - 1) {
78 if (isascii(i)) {
79 *fs = i;
80 addch(*fs++);
81 } else
82 beep();
83 } else
84 beep();
85 }
86 *fs = '\0';
87 if ((fdesc = open(fname, O_RDWR)) == -1 && errno == ENOENT) {
88 if ((fdesc = open(fname,
89 O_CREAT | O_TRUNC | O_WRONLY,
90 0600)) != -1)
91 break;
92 }
93 if (fdesc != -1) {
94 move(18, 0);
95 clrtobot();
96 printw("%s%s%s", exist1, fname, exist2);
97 cflag = 0;
98 close(fdesc);
99 if (yorn(0)) {
100 unlink(fname);
101 fdesc = open(fname,
102 O_CREAT | O_TRUNC | O_WRONLY,
103 0600);
104 break;
105 } else {
106 cflag = 1;
107 continue;
108 }
109 }
110 printw("%s%s.\n", cantuse, fname);
111 cflag = 1;
112 }
113 write(fdesc, board, sizeof(board));
114 write(fdesc, off, sizeof(off));
115 write(fdesc, in, sizeof(in));
116 write(fdesc, dice, sizeof(dice));
117 write(fdesc, &cturn, sizeof(cturn));
118 write(fdesc, &dflag, sizeof(dflag));
119 write(fdesc, &dlast, sizeof(dlast));
120 write(fdesc, &pnum, sizeof(pnum));
121 write(fdesc, &rscore, sizeof(rscore));
122 write(fdesc, &wscore, sizeof(wscore));
123 write(fdesc, &gvalue, sizeof(gvalue));
124 write(fdesc, &raflag, sizeof(raflag));
125 close(fdesc);
126 move(18, 0);
127 printw("%s%s%s%s%s", saved, fname, type, fname, rec);
128 clrtobot();
129 getout(0);
130 }
131
132 void
recover(const char * s)133 recover(const char *s)
134 {
135 int fdesc;
136
137 if ((fdesc = open(s, O_RDONLY)) == -1)
138 norec(s);
139 read(fdesc, board, sizeof(board));
140 read(fdesc, off, sizeof(off));
141 read(fdesc, in, sizeof(in));
142 read(fdesc, dice, sizeof(dice));
143 read(fdesc, &cturn, sizeof(cturn));
144 read(fdesc, &dflag, sizeof(dflag));
145 read(fdesc, &dlast, sizeof(dlast));
146 read(fdesc, &pnum, sizeof(pnum));
147 read(fdesc, &rscore, sizeof(rscore));
148 read(fdesc, &wscore, sizeof(wscore));
149 read(fdesc, &gvalue, sizeof(gvalue));
150 read(fdesc, &raflag, sizeof(raflag));
151 close(fdesc);
152 rflag = 1;
153 }
154
155 void
norec(const char * s)156 norec(const char *s)
157 {
158 const char *c;
159
160 addstr(cantrec);
161 c = s;
162 while (*c != '\0')
163 addch(*c++);
164 getout(0);
165 }
166