Home | History | Annotate | Line # | Download | only in mille
save.c revision 1.8
      1 /*	$NetBSD: save.c,v 1.8 1999/09/08 21:17:51 jsm Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 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.8 1999/09/08 21:17:51 jsm Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include "mille.h"
     46 
     47 #ifndef	unctrl
     48 #include "unctrl.h"
     49 #endif
     50 
     51 /*
     52  * @(#)save.c	1.2 (Berkeley) 3/28/83
     53  */
     54 
     55 typedef	struct stat	STAT;
     56 
     57 /*
     58  *	This routine saves the current game for use at a later date
     59  *	Returns FALSE if it couldn't be done.
     60  */
     61 bool
     62 save()
     63 {
     64 	char	*sp;
     65 	int	outf;
     66 	time_t	*tp;
     67 	char	buf[80];
     68 	time_t	tme;
     69 	STAT	junk;
     70 	bool	rv;
     71 
     72 	sp = NULL;
     73 	tp = &tme;
     74 	if (Fromfile && getyn(SAMEFILEPROMPT))
     75 		strcpy(buf, Fromfile);
     76 	else {
     77 over:
     78 		prompt(FILEPROMPT);
     79 		leaveok(Board, FALSE);
     80 		refresh();
     81 		sp = buf;
     82 		while ((*sp = readch()) != '\n') {
     83 			if (*sp == killchar())
     84 				goto over;
     85 			else if (*sp == erasechar()) {
     86 				if (--sp < buf)
     87 					sp = buf;
     88 				else {
     89 					addch('\b');
     90 					/*
     91 					 * if the previous char was a control
     92 					 * char, cover up two characters.
     93 					 */
     94 					if (*sp < ' ')
     95 						addch('\b');
     96 					clrtoeol();
     97 				}
     98 			}
     99 			else {
    100 				addstr(unctrl(*sp));
    101 				++sp;
    102 			}
    103 			refresh();
    104 		}
    105 		*sp = '\0';
    106 		leaveok(Board, TRUE);
    107 	}
    108 
    109 	/*
    110 	 * check for existing files, and confirm overwrite if needed
    111 	 */
    112 
    113 	if (sp == buf || (!Fromfile && stat(buf, &junk) > -1
    114 	    && getyn(OVERWRITEFILEPROMPT) == FALSE))
    115 		return FALSE;
    116 
    117 	if ((outf = creat(buf, 0644)) < 0) {
    118 		error(strerror(errno));
    119 		return FALSE;
    120 	}
    121 	mvwaddstr(Score, ERR_Y, ERR_X, buf);
    122 	wrefresh(Score);
    123 	time(tp);			/* get current time		*/
    124 	rv = varpush(outf, writev);
    125 	close(outf);
    126 	if (rv == FALSE) {
    127 		unlink(buf);
    128 	} else {
    129 		strcpy(buf, ctime(tp));
    130 		for (sp = buf; *sp != '\n'; sp++)
    131 			continue;
    132 		*sp = '\0';
    133 		wprintw(Score, " [%s]", buf);
    134 	}
    135 	wclrtoeol(Score);
    136 	wrefresh(Score);
    137 	return rv;
    138 }
    139 
    140 /*
    141  *	This does the actual restoring.  It returns TRUE if the
    142  * backup was made on exiting, in which case certain things must
    143  * be cleaned up before the game starts.
    144  */
    145 bool
    146 rest_f(file)
    147 	const char	*file;
    148 {
    149 
    150 	char	*sp;
    151 	int	inf;
    152 	char	buf[80];
    153 	STAT	sbuf;
    154 
    155 	if ((inf = open(file, 0)) < 0) {
    156 		warn("%s", file);
    157 		exit(1);
    158 	}
    159 	if (fstat(inf, &sbuf) < 0) {		/* get file stats	*/
    160 		warn("%s", file);
    161 		exit(1);
    162 	}
    163 	varpush(inf, readv);
    164 	close(inf);
    165 	strcpy(buf, ctime(&sbuf.st_mtime));
    166 	for (sp = buf; *sp != '\n'; sp++)
    167 		continue;
    168 	*sp = '\0';
    169 	/*
    170 	 * initialize some necessary values
    171 	 */
    172 	(void)sprintf(Initstr, "%s [%s]\n", file, buf);
    173 	Fromfile = file;
    174 	return !On_exit;
    175 }
    176