|
|
1.1 root 1: /*
2: * Copyright (c) 1983 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
6: * provided that: (1) source distributions retain this entire copyright
7: * notice and comment, and (2) distributions including binaries display
8: * the following acknowledgement: ``This product includes software
9: * developed by the University of California, Berkeley and its contributors''
10: * in the documentation or other materials provided with the distribution
11: * and in all advertising materials mentioning features or use of this
12: * software. Neither the name of the University nor the names of its
13: * contributors may be used to endorse or promote products derived
14: * from this software without specific prior written permission.
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18: */
19:
20: #ifndef lint
21: static char sccsid[] = "@(#)save.c 5.6 (Berkeley) 6/1/90";
22: #endif /* not lint */
23:
24: #include "mille.h"
25: #include <sys/types.h>
26: #include <sys/stat.h>
27: #include <string.h>
28: #ifndef unctrl
29: #include "unctrl.h"
30: #endif
31:
32: # ifdef attron
33: # include <term.h>
34: # define _tty cur_term->Nttyb
35: # endif attron
36:
37: /*
38: * @(#)save.c 1.2 (Berkeley) 3/28/83
39: */
40:
41: typedef struct stat STAT;
42:
43: char *ctime();
44:
45: int read(), write();
46:
47: /*
48: * This routine saves the current game for use at a later date
49: */
50:
51: save() {
52:
53: extern int errno;
54: reg char *sp;
55: reg int outf;
56: reg time_t *tp;
57: char buf[80];
58: time_t tme;
59: STAT junk;
60:
61: tp = &tme;
62: if (Fromfile && getyn(SAMEFILEPROMPT))
63: strcpy(buf, Fromfile);
64: else {
65: over:
66: prompt(FILEPROMPT);
67: leaveok(Board, FALSE);
68: refresh();
69: sp = buf;
70: while ((*sp = readch()) != '\n') {
71: if (*sp == killchar())
72: goto over;
73: else if (*sp == erasechar()) {
74: if (--sp < buf)
75: sp = buf;
76: else {
77: addch('\b');
78: /*
79: * if the previous char was a control
80: * char, cover up two characters.
81: */
82: if (*sp < ' ')
83: addch('\b');
84: clrtoeol();
85: }
86: }
87: else {
88: addstr(unctrl(*sp));
89: ++sp;
90: }
91: refresh();
92: }
93: *sp = '\0';
94: leaveok(Board, TRUE);
95: }
96:
97: /*
98: * check for existing files, and confirm overwrite if needed
99: */
100:
101: if (sp == buf || (!Fromfile && stat(buf, &junk) > -1
102: && getyn(OVERWRITEFILEPROMPT) == FALSE))
103: return FALSE;
104:
105: if ((outf = creat(buf, 0644)) < 0) {
106: error(strerror(errno));
107: return FALSE;
108: }
109: mvwaddstr(Score, ERR_Y, ERR_X, buf);
110: wrefresh(Score);
111: time(tp); /* get current time */
112: strcpy(buf, ctime(tp));
113: for (sp = buf; *sp != '\n'; sp++)
114: continue;
115: *sp = '\0';
116: varpush(outf, write);
117: close(outf);
118: wprintw(Score, " [%s]", buf);
119: wclrtoeol(Score);
120: wrefresh(Score);
121: return TRUE;
122: }
123:
124: /*
125: * This does the actual restoring. It returns TRUE if the
126: * backup was made on exiting, in which case certain things must
127: * be cleaned up before the game starts.
128: */
129: rest_f(file)
130: reg char *file; {
131:
132: reg char *sp;
133: reg int inf;
134: char buf[80];
135: STAT sbuf;
136:
137: if ((inf = open(file, 0)) < 0) {
138: perror(file);
139: exit(1);
140: }
141: if (fstat(inf, &sbuf) < 0) { /* get file stats */
142: perror(file);
143: exit(1);
144: }
145: varpush(inf, read);
146: close(inf);
147: strcpy(buf, ctime(&sbuf.st_mtime));
148: for (sp = buf; *sp != '\n'; sp++)
149: continue;
150: *sp = '\0';
151: /*
152: * initialize some necessary values
153: */
154: (void)sprintf(Initstr, "%s [%s]\n", file, buf);
155: Fromfile = file;
156: return !On_exit;
157: }
158:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.