trail and field are really just -t, -f
[unix-history] / usr / src / games / mille / save.c
CommitLineData
f823d190
KB
1/*
2 * Copyright (c) 1983 Regents of the University of California.
6892174b
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of California at Berkeley. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
f823d190
KB
11 */
12
13#ifndef lint
6892174b
KB
14static char sccsid[] = "@(#)save.c 5.3 (Berkeley) %G%";
15#endif /* not lint */
16
f823d190
KB
17
18#include "mille.h"
19#include <sys/types.h>
20#include <sys/stat.h>
21#ifndef unctrl
22#include "unctrl.h"
23#endif
24
25# ifdef attron
26# include <term.h>
27# define _tty cur_term->Nttyb
28# endif attron
29
30/*
31 * @(#)save.c 1.2 (Berkeley) 3/28/83
32 */
33
34typedef struct stat STAT;
35
36char *ctime();
37
38int read(), write();
39
40/*
41 * This routine saves the current game for use at a later date
42 */
43extern int errno;
44extern char *sys_errlist[];
45
46save() {
47
48 reg char *sp;
49 reg int outf;
50 reg time_t *tp;
51 char buf[80];
52 time_t tme;
53 STAT junk;
54
55 tp = &tme;
56 if (Fromfile && getyn(SAMEFILEPROMPT))
57 strcpy(buf, Fromfile);
58 else {
59over:
60 prompt(FILEPROMPT);
61 leaveok(Board, FALSE);
62 refresh();
63 sp = buf;
64 while ((*sp = readch()) != '\n') {
65 if (*sp == killchar())
66 goto over;
67 else if (*sp == erasechar()) {
68 if (--sp < buf)
69 sp = buf;
70 else {
71 addch('\b');
72 /*
73 * if the previous char was a control
74 * char, cover up two characters.
75 */
76 if (*sp < ' ')
77 addch('\b');
78 clrtoeol();
79 }
80 }
6892174b
KB
81 else {
82 addstr(unctrl(*sp));
83 ++sp;
84 }
f823d190
KB
85 refresh();
86 }
87 *sp = '\0';
88 leaveok(Board, TRUE);
89 }
90
91 /*
92 * check for existing files, and confirm overwrite if needed
93 */
94
95 if (sp == buf || (!Fromfile && stat(buf, &junk) > -1
96 && getyn(OVERWRITEFILEPROMPT) == FALSE))
97 return FALSE;
98
99 if ((outf = creat(buf, 0644)) < 0) {
100 error(sys_errlist[errno]);
101 return FALSE;
102 }
103 mvwaddstr(Score, ERR_Y, ERR_X, buf);
104 wrefresh(Score);
105 time(tp); /* get current time */
106 strcpy(buf, ctime(tp));
107 for (sp = buf; *sp != '\n'; sp++)
108 continue;
109 *sp = '\0';
110 varpush(outf, write);
111 close(outf);
112 wprintw(Score, " [%s]", buf);
113 wclrtoeol(Score);
114 wrefresh(Score);
115 return TRUE;
116}
117
118/*
119 * This does the actual restoring. It returns TRUE if the
120 * backup was made on exiting, in which case certain things must
121 * be cleaned up before the game starts.
122 */
123rest_f(file)
124reg char *file; {
125
126 reg char *sp;
127 reg int inf;
128 char buf[80];
129 STAT sbuf;
130
131 if ((inf = open(file, 0)) < 0) {
132 perror(file);
133 exit(1);
134 }
135 if (fstat(inf, &sbuf) < 0) { /* get file stats */
136 perror(file);
137 exit(1);
138 }
139 varpush(inf, read);
140 close(inf);
141 strcpy(buf, ctime(&sbuf.st_mtime));
142 for (sp = buf; *sp != '\n'; sp++)
143 continue;
144 *sp = '\0';
145 /*
146 * initialize some necessary values
147 */
6f979f4f 148 (void)sprintf(Initstr, "%s [%s]\n", file, buf);
f823d190
KB
149 Fromfile = file;
150 return !On_exit;
151}
152