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