BSD 4_1c_2 release
[unix-history] / usr / src / games / mille / save.c
CommitLineData
e804469b
C
1#ifndef lint
2static char sccsid[] = "@(#)save.c 4.1 12/24/82";
3#endif
4
8dd2ca95
C
5#include "mille.h"
6#include "unctrl.h"
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <time.h>
10
11typedef struct stat STAT;
12typedef struct tm TIME;
13
14char *ctime();
15
16int read(), write();
17
18/*
19 * This routine saves the current game for use at a later date
20 */
21extern int errno;
22extern char *sys_errlist[];
23
24save() {
25
26 reg char *sp;
27 reg int outf;
28 reg TIME *tp;
29 char buf[80];
30 TIME tme;
31 STAT junk;
32
33 tp = &tme;
34 if (Fromfile && getyn("Same file? "))
35 strcpy(buf, Fromfile);
36 else {
37over:
38 mvaddstr(MOVE_Y, MOVE_X, "file: ");
39 clrtoeol();
40 leaveok(Board, FALSE);
41 refresh();
42 sp = buf;
43 while ((*sp = getch()) != '\n') {
44 if (*sp == _tty.sg_kill)
45 goto over;
46 else if (*sp == _tty.sg_erase) {
47 if (--sp < buf)
48 sp = buf;
49 else {
50 addch('\b');
51 /*
52 * if the previous char was a control
53 * char, cover up two characters.
54 */
55 if (*sp < ' ')
56 addch('\b');
57 clrtoeol();
58 }
59 }
60 else
61 addstr(unctrl(*sp++));
62 refresh();
63 }
64 *sp = '\0';
65 leaveok(Board, TRUE);
66 }
67
68 /*
69 * check for existing files, and confirm overwrite if needed
70 */
71
72 if (sp == buf || (!Fromfile && stat(buf, &junk) > -1
73 && getyn("Overwrite File? ") == FALSE))
74 return FALSE;
75
76 if ((outf = creat(buf, 0644)) < 0) {
77 error(sys_errlist[errno]);
78 return FALSE;
79 }
80 mvwaddstr(Score, ERR_Y, ERR_X, buf);
81 wrefresh(Score);
82 time(tp); /* get current time */
83 strcpy(buf, ctime(tp));
84 for (sp = buf; *sp != '\n'; sp++)
85 continue;
86 *sp = '\0';
87 varpush(outf, write);
88 close(outf);
89 wprintw(Score, " [%s]", buf);
90 wclrtoeol(Score);
91 wrefresh(Score);
92 return TRUE;
93}
94
95/*
96 * This does the actual restoring. It returns TRUE if the
97 * backup was made on exiting, in which case certain things must
98 * be cleaned up before the game starts.
99 */
100rest_f(file)
101reg char *file; {
102
103 reg char *sp;
104 reg int inf;
105 char buf[80];
106 STAT sbuf;
107
108 if ((inf = open(file, 0)) < 0) {
109 perror(file);
110 exit(1);
111 }
112 if (fstat(inf, &sbuf) < 0) { /* get file stats */
113 perror(file);
114 exit(1);
115 }
116 varpush(inf, read);
117 close(inf);
118 strcpy(buf, ctime(&sbuf.st_mtime));
119 for (sp = buf; *sp != '\n'; sp++)
120 continue;
121 *sp = '\0';
122 /*
123 * initialize some necessary values
124 */
125 sprintf(Initstr, "%s [%s]\n", file, buf);
126 Fromfile = file;
127 return !On_exit;
128}