1 /* $OpenBSD: save.c,v 1.7 2001/06/23 23:50:04 pjanzen Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)save.c 8.1 (Berkeley) 5/31/93"; 39 #else 40 static char rcsid[] = "$OpenBSD: save.c,v 1.7 2001/06/23 23:50:04 pjanzen Exp $"; 41 #endif 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <ctype.h> 46 #include <errno.h> 47 #include "back.h" 48 49 static const char confirm[] = "Are you sure you want to leave now?"; 50 static const char prompt[] = "Enter a file name: "; 51 static const char exist1[] = "The file '"; 52 static const char exist2[] = 53 "' already exists.\nAre you sure you want to use this file?"; 54 static const char cantuse[] = "\nCan't use "; 55 static const char saved[] = "This game has been saved on the file '"; 56 static const char type[] = "'.\nType \"backgammon -s "; 57 static const char rec[] = "\" to recover your game.\n\n"; 58 static const char cantrec[] = "Can't recover file: "; 59 60 void 61 save(n) 62 int n; 63 { 64 int fdesc; 65 char *fs; 66 char fname[MAXPATHLEN]; 67 int r, c, i; 68 69 if (n) { 70 move(20, 0); 71 clrtobot(); 72 addstr(confirm); 73 if (!yorn(0)) 74 return; 75 } 76 cflag = 1; 77 for (;;) { 78 addstr(prompt); 79 fs = fname; 80 while ((i = readc()) != '\n') { 81 if (i == KEY_BACKSPACE || i == 0177) { 82 if (fs > fname) { 83 fs--; 84 getyx(stdscr, r, c); 85 move(r, c - 1); 86 } else 87 beep(); 88 continue; 89 } 90 if (fs - fname < MAXPATHLEN - 1) { 91 if (isascii(i)) { 92 *fs = i; 93 addch(*fs++); 94 } else 95 beep(); 96 } else 97 beep(); 98 } 99 *fs = '\0'; 100 if ((fdesc = open(fname, O_RDWR)) == -1 && errno == ENOENT) { 101 if ((fdesc = creat(fname, 0600)) != -1) 102 break; 103 } 104 if (fdesc != -1) { 105 move(18, 0); 106 clrtobot(); 107 printw("%s%s%s", exist1, fname, exist2); 108 cflag = 0; 109 close(fdesc); 110 if (yorn(0)) { 111 unlink(fname); 112 fdesc = creat(fname, 0600); 113 break; 114 } else { 115 cflag = 1; 116 continue; 117 } 118 } 119 printw("%s%s.\n", cantuse, fname); 120 close(fdesc); 121 cflag = 1; 122 } 123 write(fdesc, board, sizeof(board)); 124 write(fdesc, off, sizeof(off)); 125 write(fdesc, in, sizeof(in)); 126 write(fdesc, dice, sizeof(dice)); 127 write(fdesc, &cturn, sizeof(cturn)); 128 write(fdesc, &dlast, sizeof(dlast)); 129 write(fdesc, &pnum, sizeof(pnum)); 130 write(fdesc, &rscore, sizeof(rscore)); 131 write(fdesc, &wscore, sizeof(wscore)); 132 write(fdesc, &gvalue, sizeof(gvalue)); 133 write(fdesc, &raflag, sizeof(raflag)); 134 close(fdesc); 135 move(18, 0); 136 printw("%s%s%s%s%s", saved, fname, type, fname, rec); 137 clrtobot(); 138 getout(0); 139 } 140 141 void 142 recover(s) 143 const char *s; 144 { 145 int fdesc; 146 147 if ((fdesc = open(s, O_RDONLY)) == -1) 148 norec(s); 149 read(fdesc, board, sizeof(board)); 150 read(fdesc, off, sizeof(off)); 151 read(fdesc, in, sizeof(in)); 152 read(fdesc, dice, sizeof(dice)); 153 read(fdesc, &cturn, sizeof(cturn)); 154 read(fdesc, &dlast, sizeof(dlast)); 155 read(fdesc, &pnum, sizeof(pnum)); 156 read(fdesc, &rscore, sizeof(rscore)); 157 read(fdesc, &wscore, sizeof(wscore)); 158 read(fdesc, &gvalue, sizeof(gvalue)); 159 read(fdesc, &raflag, sizeof(raflag)); 160 close(fdesc); 161 rflag = 1; 162 } 163 164 void 165 norec(s) 166 const char *s; 167 { 168 const char *c; 169 170 addstr(cantrec); 171 c = s; 172 while (*c != '\0') 173 addch(*c++); 174 getout(0); 175 } 176