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