+/* $NetBSD: save.c,v 1.16 2011/08/26 09:01:07 tron Exp $ */
+
/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#ifndef lint
-/*static char sccsid[] = "from: @(#)save.c 8.1 (Berkeley) 5/31/93";*/
-static char rcsid[] = "$Id: save.c,v 1.3 1994/05/12 17:39:41 jtc Exp $";
+#if 0
+static char sccsid[] = "@(#)save.c 8.1 (Berkeley) 5/31/93";
+#else
+__RCSID("$NetBSD: save.c,v 1.16 2011/08/26 09:01:07 tron Exp $");
+#endif
#endif /* not lint */
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <string.h>
-#include <termios.h>
+#include <time.h>
+
#include "mille.h"
#ifndef unctrl
#include "unctrl.h"
#endif
-# ifdef attron
-# include <term.h>
-# define _tty cur_term->Nttyb
-# endif attron
-
/*
* @(#)save.c 1.2 (Berkeley) 3/28/83
*/
typedef struct stat STAT;
-char *ctime();
-
-int read(), write();
-
/*
* This routine saves the current game for use at a later date
+ * Returns FALSE if it couldn't be done.
*/
-
-save() {
-
- extern int errno;
- reg char *sp;
- reg int outf;
- reg time_t *tp;
- char buf[80];
- time_t tme;
- STAT junk;
-
+bool
+save(void)
+{
+ char *sp;
+ int outfd;
+ time_t *tp;
+ char buf[80];
+ time_t tme;
+ STAT junk;
+ bool rv;
+
+ sp = NULL;
tp = &tme;
if (Fromfile && getyn(SAMEFILEPROMPT))
strcpy(buf, Fromfile);
&& getyn(OVERWRITEFILEPROMPT) == FALSE))
return FALSE;
- if ((outf = creat(buf, 0644)) < 0) {
- error(strerror(errno));
+ if ((outfd = creat(buf, 0644)) < 0) {
+ error("%s", strerror(errno));
return FALSE;
}
mvwaddstr(Score, ERR_Y, ERR_X, buf);
wrefresh(Score);
time(tp); /* get current time */
- strcpy(buf, ctime(tp));
- for (sp = buf; *sp != '\n'; sp++)
- continue;
- *sp = '\0';
- varpush(outf, write);
- close(outf);
- wprintw(Score, " [%s]", buf);
+ rv = varpush(outfd, writev);
+ close(outfd);
+ if (rv == FALSE) {
+ unlink(buf);
+ } else {
+ char *p;
+ if ((p = ctime(tp)) == NULL)
+ strcpy(buf, "?");
+ else
+ strcpy(buf, p);
+ for (sp = buf; *sp != '\n'; sp++)
+ continue;
+ *sp = '\0';
+ wprintw(Score, " [%s]", buf);
+ }
wclrtoeol(Score);
wrefresh(Score);
- return TRUE;
+ return rv;
}
/*
* backup was made on exiting, in which case certain things must
* be cleaned up before the game starts.
*/
-rest_f(file)
-reg char *file; {
+bool
+rest_f(const char *file)
+{
- reg char *sp;
- reg int inf;
- char buf[80];
- STAT sbuf;
+ char *sp, *p;
+ int inf;
+ char buf[80];
+ STAT sbuf;
- if ((inf = open(file, 0)) < 0) {
- perror(file);
+ if ((inf = open(file, O_RDONLY)) < 0) {
+ warn("%s", file);
exit(1);
}
if (fstat(inf, &sbuf) < 0) { /* get file stats */
- perror(file);
+ warn("%s", file);
exit(1);
}
- varpush(inf, read);
+ varpush(inf, readv);
close(inf);
- strcpy(buf, ctime(&sbuf.st_mtime));
+ if ((p = ctime(&sbuf.st_mtime)) == NULL)
+ strcpy(buf, "?");
+ else
+ strcpy(buf, p);
for (sp = buf; *sp != '\n'; sp++)
continue;
*sp = '\0';
/*
* initialize some necessary values
*/
- (void)sprintf(Initstr, "%s [%s]\n", file, buf);
+ (void)snprintf(Initstr, INITSTR_SIZE, "%s [%s]\n", file, buf);
Fromfile = file;
return !On_exit;
}
-