Home | History | Annotate | Line # | Download | only in common_source
save.c revision 1.6
      1 /*	$NetBSD: save.c,v 1.6 1998/08/29 22:53:04 hubertf 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 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)save.c	8.1 (Berkeley) 5/31/93";
     40 #else
     41 __RCSID("$NetBSD: save.c,v 1.6 1998/08/29 22:53:04 hubertf Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <errno.h>
     46 
     47 #include "back.h"
     48 
     49 static char confirm[] = "Are you sure you want to leave now?";
     50 static char prompt[] = "Enter a file name:  ";
     51 static char exist1[] = "The file '";
     52 static char exist2[] =
     53 "' already exists.\nAre you sure you want to use this file?";
     54 static char cantuse[] = "\nCan't use ";
     55 static char saved[] = "This game has been saved on the file '";
     56 static char type[] = "'.\nType \"backgammon ";
     57 static char rec[] = "\" to recover your game.\n\n";
     58 static char cantrec[] = "Can't recover file:  ";
     59 
     60 void
     61 save(n)
     62 	int     n;
     63 {
     64 	int     fdesc;
     65 	char   *fs;
     66 	char    fname[50];
     67 
     68 	if (n) {
     69 		if (tflag) {
     70 			curmove(20, 0);
     71 			clend();
     72 		} else
     73 			writec('\n');
     74 		writel(confirm);
     75 		if (!yorn(0))
     76 			return;
     77 	}
     78 	cflag = 1;
     79 	for (;;) {
     80 		writel(prompt);
     81 		fs = fname;
     82 		while ((*fs = readc()) != '\n') {
     83 			if (*fs == old.c_cc[VERASE]) {
     84 				if (fs > fname) {
     85 					fs--;
     86 					if (tflag)
     87 						curmove(curr, curc - 1);
     88 					else
     89 						writec(*fs);
     90 				} else
     91 					writec('\007');
     92 				continue;
     93 			}
     94 			writec(*fs++);
     95 		}
     96 		*fs = '\0';
     97 		if ((fdesc = open(fname, O_RDWR)) == -1 && errno == ENOENT) {
     98 			if ((fdesc = creat(fname, 0700)) != -1)
     99 				break;
    100 		}
    101 		if (fdesc != -1) {
    102 			if (tflag) {
    103 				curmove(18, 0);
    104 				clend();
    105 			} else
    106 				writec('\n');
    107 			writel(exist1);
    108 			writel(fname);
    109 			writel(exist2);
    110 			cflag = 0;
    111 			close(fdesc);
    112 			if (yorn(0)) {
    113 				unlink(fname);
    114 				fdesc = creat(fname, 0700);
    115 				break;
    116 			} else {
    117 				cflag = 1;
    118 				continue;
    119 			}
    120 		}
    121 		writel(cantuse);
    122 		writel(fname);
    123 		writel(".\n");
    124 		close(fdesc);
    125 		cflag = 1;
    126 	}
    127 	write(fdesc, board, sizeof board);
    128 	write(fdesc, off, sizeof off);
    129 	write(fdesc, in, sizeof in);
    130 	write(fdesc, dice, sizeof dice);
    131 	write(fdesc, &cturn, sizeof cturn);
    132 	write(fdesc, &dlast, sizeof dlast);
    133 	write(fdesc, &pnum, sizeof pnum);
    134 	write(fdesc, &rscore, sizeof rscore);
    135 	write(fdesc, &wscore, sizeof wscore);
    136 	write(fdesc, &gvalue, sizeof gvalue);
    137 	write(fdesc, &raflag, sizeof raflag);
    138 	close(fdesc);
    139 	if (tflag)
    140 		curmove(18, 0);
    141 	writel(saved);
    142 	writel(fname);
    143 	writel(type);
    144 	writel(fname);
    145 	writel(rec);
    146 	if (tflag)
    147 		clend();
    148 	getout(0);
    149 }
    150 
    151 void
    152 recover(s)
    153 	char   *s;
    154 {
    155 	int     fdesc;
    156 
    157 	if ((fdesc = open(s, O_RDONLY)) == -1)
    158 		norec(s);
    159 	read(fdesc, board, sizeof board);
    160 	read(fdesc, off, sizeof off);
    161 	read(fdesc, in, sizeof in);
    162 	read(fdesc, dice, sizeof dice);
    163 	read(fdesc, &cturn, sizeof cturn);
    164 	read(fdesc, &dlast, sizeof dlast);
    165 	read(fdesc, &pnum, sizeof pnum);
    166 	read(fdesc, &rscore, sizeof rscore);
    167 	read(fdesc, &wscore, sizeof wscore);
    168 	read(fdesc, &gvalue, sizeof gvalue);
    169 	read(fdesc, &raflag, sizeof raflag);
    170 	close(fdesc);
    171 	rflag = 1;
    172 }
    173 
    174 void
    175 norec(s)
    176 	char   *s;
    177 {
    178 	char   *c;
    179 
    180 	tflag = 0;
    181 	writel(cantrec);
    182 	c = s;
    183 	while (*c != '\0')
    184 		writec(*c++);
    185 	getout(0);
    186 }
    187