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