]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - games/backgammon/common_source/save.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / games / backgammon / common_source / save.c
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)save.c      8.1 (Berkeley) 5/31/93";
37 #endif
38 static const char rcsid[] =
39  "$FreeBSD$";
40 #endif /* not lint */
41
42 #include <fcntl.h>
43 #include <errno.h>
44 #include "back.h"
45
46 static const char       confirm[] = "Are you sure you want to leave now?";
47 static const char       prompt[] = "Enter a file name:  ";
48 static const char       exist1[] = "The file '";
49 static const char       exist2[] =
50         "' already exists.\nAre you sure you want to use this file?";
51 static const char       cantuse[] = "\nCan't use ";
52 static const char       saved[] = "This game has been saved on the file '";
53 static const char       type[] = "'.\nType \"backgammon ";
54 static const char       rec[] = "\" to recover your game.\n\n";
55 static const char       cantrec[] = "Can't recover file:  ";
56
57 save (n)
58 int     n;
59
60 {
61         int     fdesc;
62         char    *fs;
63         char            fname[50];
64
65         if (n)  {
66                 if (tflag)  {
67                         curmove (20,0);
68                         clend();
69                 } else
70                         writec ('\n');
71                 writel (confirm);
72                 if (! yorn(0))
73                         return;
74         }
75         cflag = 1;
76         for (;;)  {
77                 writel (prompt);
78                 fs = fname;
79                 while ((*fs = readc()) != '\n')  {
80                         if (*fs == tty.sg_erase)  {
81                                 if (fs > fname)  {
82                                         fs--;
83                                         if (tflag)
84                                                 curmove (curr,curc-1);
85                                         else
86                                                 writec (*fs);
87                                 } else
88                                         writec ('\007');
89                                 continue;
90                         }
91                         writec (*fs++);
92                 }
93                 *fs = '\0';
94                 if ((fdesc = open(fname,O_RDWR)) == -1 && errno == ENOENT)  {
95                         if ((fdesc = creat (fname,0600)) != -1)
96                         break;
97                 }
98                 if (fdesc != -1)  {
99                         if (tflag)  {
100                                 curmove (18,0);
101                                 clend();
102                         } else
103                                 writec ('\n');
104                         writel (exist1);
105                         writel (fname);
106                         writel (exist2);
107                         cflag = 0;
108                         close (fdesc);
109                         if (yorn (0))  {
110                                 unlink (fname);
111                                 fdesc = creat (fname,0700);
112                                 break;
113                         } else  {
114                                 cflag = 1;
115                                 continue;
116                         }
117                 }
118                 writel (cantuse);
119                 writel (fname);
120                 writel (".\n");
121                 close (fdesc);
122                 cflag = 1;
123         }
124         write (fdesc,board,sizeof board);
125         write (fdesc,off,sizeof off);
126         write (fdesc,in,sizeof in);
127         write (fdesc,dice,sizeof dice);
128         write (fdesc,&cturn,sizeof cturn);
129         write (fdesc,&dlast,sizeof dlast);
130         write (fdesc,&pnum,sizeof pnum);
131         write (fdesc,&rscore,sizeof rscore);
132         write (fdesc,&wscore,sizeof wscore);
133         write (fdesc,&gvalue,sizeof gvalue);
134         write (fdesc,&raflag,sizeof raflag);
135         close (fdesc);
136         if (tflag)
137                 curmove (18,0);
138         writel (saved);
139         writel (fname);
140         writel (type);
141         writel (fname);
142         writel (rec);
143         if (tflag)
144                 clend();
145         getout ();
146 }
147 \f
148 int
149 recover (s)
150 const char      *s;
151
152 {
153         int     i;
154         int             fdesc;
155
156         if ((fdesc = open (s,O_RDONLY)) == -1)
157                 norec (s);
158         read (fdesc,board,sizeof board);
159         read (fdesc,off,sizeof off);
160         read (fdesc,in,sizeof in);
161         read (fdesc,dice,sizeof dice);
162         read (fdesc,&cturn,sizeof cturn);
163         read (fdesc,&dlast,sizeof dlast);
164         read (fdesc,&pnum,sizeof pnum);
165         read (fdesc,&rscore,sizeof rscore);
166         read (fdesc,&wscore,sizeof wscore);
167         read (fdesc,&gvalue,sizeof gvalue);
168         read (fdesc,&raflag,sizeof raflag);
169         close (fdesc);
170         rflag = 1;
171 }
172
173 int
174 norec (s)
175 const char      *s;
176
177 {
178         const char      *c;
179
180         tflag = 0;
181         writel (cantrec);
182         c = s;
183         while (*c != '\0')
184                 writec (*c++);
185         getout ();
186 }