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