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