1 /* 2 * Copyright (c) 1980 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.3 (Berkeley) 06/18/88"; 20 #endif /* not lint */ 21 22 #include "back.h" 23 24 extern int errno; 25 26 static char confirm[] = "Are you sure you want to leave now?"; 27 static char prompt[] = "Enter a file name: "; 28 static char exist1[] = "The file '"; 29 static char exist2[] = 30 "' already exists.\nAre you sure you want to use this file?"; 31 static char cantuse[] = "\nCan't use "; 32 static char saved[] = "This game has been saved on the file '"; 33 static char type[] = "'.\nType \"backgammon "; 34 static char rec[] = "\" to recover your game.\n\n"; 35 static char cantrec[] = "Can't recover file: "; 36 37 save (n) 38 register int n; 39 40 { 41 register int fdesc; 42 register char *fs; 43 char fname[50]; 44 45 if (n) { 46 if (tflag) { 47 curmove (20,0); 48 clend(); 49 } else 50 writec ('\n'); 51 writel (confirm); 52 if (! yorn(0)) 53 return; 54 } 55 cflag = 1; 56 for (;;) { 57 writel (prompt); 58 fs = fname; 59 while ((*fs = readc()) != '\n') { 60 if (*fs == tty.sg_erase) { 61 if (fs > fname) { 62 fs--; 63 if (tflag) 64 curmove (curr,curc-1); 65 else 66 writec (*fs); 67 } else 68 writec ('\007'); 69 continue; 70 } 71 writec (*fs++); 72 } 73 *fs = '\0'; 74 if ((fdesc = open(fname,2)) == -1 && errno == 2) { 75 if ((fdesc = creat (fname,0700)) != -1) 76 break; 77 } 78 if (fdesc != -1) { 79 if (tflag) { 80 curmove (18,0); 81 clend(); 82 } else 83 writec ('\n'); 84 writel (exist1); 85 writel (fname); 86 writel (exist2); 87 cflag = 0; 88 close (fdesc); 89 if (yorn (0)) { 90 unlink (fname); 91 fdesc = creat (fname,0700); 92 break; 93 } else { 94 cflag = 1; 95 continue; 96 } 97 } 98 writel (cantuse); 99 writel (fname); 100 writel (".\n"); 101 close (fdesc); 102 cflag = 1; 103 } 104 write (fdesc,board,sizeof board); 105 write (fdesc,off,sizeof off); 106 write (fdesc,in,sizeof in); 107 write (fdesc,dice,sizeof dice); 108 write (fdesc,&cturn,sizeof cturn); 109 write (fdesc,&dlast,sizeof dlast); 110 write (fdesc,&pnum,sizeof pnum); 111 write (fdesc,&rscore,sizeof rscore); 112 write (fdesc,&wscore,sizeof wscore); 113 write (fdesc,&gvalue,sizeof gvalue); 114 write (fdesc,&raflag,sizeof raflag); 115 close (fdesc); 116 if (tflag) 117 curmove (18,0); 118 writel (saved); 119 writel (fname); 120 writel (type); 121 writel (fname); 122 writel (rec); 123 if (tflag) 124 clend(); 125 getout (); 126 } 127 128 recover (s) 129 char *s; 130 131 { 132 register int i; 133 int fdesc; 134 135 if ((fdesc = open (s,0)) == -1) 136 norec (s); 137 read (fdesc,board,sizeof board); 138 read (fdesc,off,sizeof off); 139 read (fdesc,in,sizeof in); 140 read (fdesc,dice,sizeof dice); 141 read (fdesc,&cturn,sizeof cturn); 142 read (fdesc,&dlast,sizeof dlast); 143 read (fdesc,&pnum,sizeof pnum); 144 read (fdesc,&rscore,sizeof rscore); 145 read (fdesc,&wscore,sizeof wscore); 146 read (fdesc,&gvalue,sizeof gvalue); 147 read (fdesc,&raflag,sizeof raflag); 148 close (fdesc); 149 rflag = 1; 150 } 151 152 norec (s) 153 register char *s; 154 155 { 156 register char *c; 157 158 tflag = 0; 159 writel (cantrec); 160 c = s; 161 while (*c != '\0') 162 writec (*c++); 163 getout (); 164 } 165