1 static char sccsid[] = "	save.c	1.1	82/05/11	";
2 
3 #include "back.h"
4 
5 extern int	errno;
6 
7 int	code[] = {
8 	0000413, 0000000, 0004000, 0000000, 0002000, 0000000, 0002620, 0000000,
9 	0001100, 0000000, 0000000, 0000000, 0000000, 0000000, 0000000, 0000000,
10 	0000000, 0000000, 0000000, 0000000, 0000000, 0000000, 0000000, 0000000,
11 /* *, */
12 	0000000, 0004302, 0150136, 0004256, 0117156, 0006256, 0150120, 0127120,
13 	0152404, 0011200, 0150774, 0137120, 0014404, 0152402, 0150160, 0127120,
14 	0150010, 0167520, 0003730, 0000000, 0001773, 0006757, 0000000, 0156400,
15 	0175520, 0167401, 0000134, 0000000, 0000674, 0000000, 0000000, 0000335,
16 	0136335, 0157410, 0156357, 0000007, 0157400, 0145757, 0000007, 0157400,
17 	0127757, 0000007, 0175400, 0167405, 0000004, 0000000, 0000004, 0000000,
18 	0000000, 0126237, 0156410, 0002254, 0001373, 0000757, 0000000, 0002000,
19 	0000000, 0167735, 0003610, 0000000, 0126335, 0156410, 0002254, 0001773,
20 	0001757, 0000000, 0002000, 0000000, 0000000, 0035674, 0167427, 0000016,
21 	0000000, 0000000, 0000000, 0000373, 0106757, 0000001, 0136000, 0000001,
22 	0050320, 0160757, 0000010, 0147000, 0050001, 0000004, 0007400, 0002302,
23 	0150136, 0004254, 0160133, 0125401, 0050014, 0000716, 0002120, 0054724,
24 	0054720, 0030530, 0000207, 0126366, 0126404, 0150377, 0054001, 0054335,
25 	0126737, 0114377, 0006653, 0175576, 0167403, 0002312, 0000000, 0050320,
26 	0010531, 0114152, 0056357, 0000007, 0077000, 0000773, 0141757, 0000001,
27 	0152400, 0011520, 0104005, 0100217, 0006253, 0167736, 0004220, 0000000,
28 	0004253, 0167736, 0004210, 0000000, 0002253, 0003741, 0006253, 0150102,
29 	0004253, 0173132, 0002254, 0002273, 0125726, 0140404, 0000217, 0000004,
30 	0055000, 0150520, 0002253, 0014120, 0150406, 0002254, 0011012, 0141617,
31 	0125532, 0050004, 0050320, 0156530, 0156520, 0114132, 0006653, 0175576,
32 	0167403, 0002142, 0000000, 0050320, 0150131, 0125532, 0152004, 0010553,
33 	0160556, 0125402, 0001414, 0066461, 0150377, 0004253, 0011132, 0150463,
34 	0107533, 0004070, 0000000, 0001422, 0074061, 0156777, 0000217, 0000004,
35 	0175400, 0167401, 0000630, 0000000, 0050320, 0150132, 0125532, 0011010,
36 	0104006, 0125404, 0010414, 0104203, 0125410, 0152014, 0150131, 0054131,
37 	0020021, 0055303, 0002253, 0150131, 0054131, 0013025, 0055320, 0002253,
38 	0054735, 0055335, 0125630, 0077015, 0001773, 0174757, 0000003, 0150000,
39 	0054520, 0107462, 0001777, 0173153, 0002254, 0150212, 0125532, 0150404,
40 	0054530, 0003423, 0020210, 0006253, 0167461, 0150376, 0002254, 0002120,
41 	0007000, 0126320, 0055404, 0125630, 0050014, 0107712, 0177771, 0177777,
42 	0150520, 0001120, 0040022, 0125720, 0055010, 0035023, 0055303, 0002253,
43 	0012531, 0150063, 0125532, 0111404, 0102217, 0006253, 0002023, 0050324,
44 	0002421, 0107462, 0002000, 0150120, 0065520, 0054735, 0055335, 0125630,
45 	0077015, 0001773, 0107757, 0000003, 0150400, 0054520, 0004023, 0020210,
46 	0006253, 0000716, 0002120, 0050324, 0000004, 0004000, 0167736, 0002766,
47 	0000000, 0010533, 0156414, 0175533, 0167401, 0000016, 0000000, 0010300,
48 	0150533, 0167533, 0003440, 0000000, 0165431, 0000004, 0006000, 0126320,
49 	0055404, 0000716, 0111532, 0125403, 0011414, 0160074, 0125406, 0033414,
50 	0055735, 0000773, 0063757, 0177777, 0150377, 0055120, 0125630, 0077015,
51 	0000773, 0026757, 0000000, 0152400, 0014120, 0147003, 0055001, 0001741,
52 	0006253, 0156412, 0004253, 0000773, 0167757, 0000001, 0111400, 0106217,
53 	0006253, 0001423, 0125724, 0105010, 0177617, 0006253, 0065724, 0055320,
54 	0002120, 0000000, 0000000, 0003274, 0003036, 0167427, 0176760, 0177777,
55 	0050324, 0000004, 0000000, 0004302, 0157536, 0175255, 0126335, 0175404,
56 	0167402, 0000044, 0000000, 0050325, 0001430, 0050324, 0150004, 0050001,
57 	0000004, 0000000, 0000000, 0126335, 0156410, 0004617, 0000164, 0156400,
58 	0002254, 0001773, 0013757, 0000000, 0002000, 0000000, 0126335, 0156410,
59 	0004217, 0000164, 0156400, 0002254, 0001773, 0000757, 0000000, 0002000,
60 	0000000, 0033274, 0003036, 0167427, 0176624, 0177777, 0000004, 0000000,
61 	0007000, 0167725, 0003124, 0000000, 0027022, 0000711, 0070217, 0000011,
62 	0167400, 0003106, 0000000, 0000711, 0066217, 0000011, 0167400, 0003076,
63 	0000000, 0167736, 0003070, 0000000, 0035757, 0000006, 0157000, 0024757,
64 	0000006, 0167400, 0003054, 0000000, 0002335, 0003701, 0002254, 0175576,
65 	0167402, 0001052, 0000000, 0050320, 0150131, 0012757, 0000006, 0055400,
66 	0167724, 0003032, 0000000, 0065751, 0010405, 0150044, 0065552, 0065720,
67 	0164532, 0173552, 0001170, 0050131, 0055700, 0150520, 0050132, 0007431,
68 	0001170, 0050131, 0055700, 0150520, 0055520, 0001431, 0134461, 0150000,
69 	0055133, 0000713, 0055553, 0055721, 0012132, 0150713, 0167532, 0002726,
70 	0000000, 0004422, 0055721, 0066217, 0000011, 0011400, 0152003, 0002120,
71 	0000761, 0167401, 0002710, 0000000, 0177654, 0107701, 0000400, 0000000,
72 	0050131, 0107706, 0000400, 0000000, 0074120, 0050010, 0127757, 0000005,
73 	0156400, 0175400, 0167401, 0000542, 0000000, 0050320, 0074132, 0167402,
74 	0002634, 0000000, 0140120, 0050132, 0050321, 0014532, 0074300, 0167402,
75 	0002614, 0000000, 0175576, 0167401, 0000500, 0000000, 0050320, 0150532,
76 	0107532, 0177777, 0177777, 0122423, 0055320, 0064777, 0000005, 0140400,
77 	0167404, 0002542, 0000000, 0150520, 0050132, 0003423, 0000710, 0052777,
78 	0000005, 0074000, 0167402, 0002526, 0000000, 0140120, 0050132, 0002303,
79 	0065120, 0065320, 0037757, 0000005, 0144400, 0107401, 0004554, 0000000,
80 	0031777, 0000005, 0030400, 0177430, 0001170, 0050131, 0055701, 0167520,
81 	0002440, 0000000, 0055321, 0014757, 0000005, 0012400, 0150022, 0010777,
82 	0000005, 0167400, 0002424, 0000000, 0065720, 0002777, 0000005, 0144400,
83 	0167401, 0002376, 0000000, 0140553, 0055404, 0002120, 0006000, 0126320,
84 	0055404, 0055720, 0141132, 0055004, 0055320, 0162757, 0000004, 0145400,
85 	0065001, 0150120, 0065120, 0000004, 0007000, 0006302, 0150136, 0002254,
86 	0164533, 0176253, 0156411, 0175533, 0167401, 0177716, 0177777, 0055703,
87 	0176253, 0143520, 0050004, 0172255, 0126335, 0175410, 0167401, 0177116,
88 	0177777, 0050320, 0011532, 0150405, 0055532, 0002022, 0055320, 0002120,
89 	0055720, 0176255, 0055320, 0174255, 0002335, 0001701, 0004254, 0175576,
90 	0167402, 0000232, 0000000, 0050320, 0150531, 0126531, 0017364, 0150031,
91 	0126531, 0010764, 0150023, 0176255, 0150120, 0174255, 0150121, 0060540,
92 	0002300, 0176255, 0002300, 0174255, 0126720, 0050364, 0126727, 0152764,
93 	0011120, 0150742, 0055532, 0132030, 0001170, 0050131, 0055300, 0150520,
94 	0055520, 0124031, 0001170, 0050131, 0055300, 0141120, 0050133, 0002306,
95 	0150120, 0042757, 0000004, 0040000, 0010552, 0000221, 0000000, 0167701,
96 	0002100, 0000000, 0002254, 0156576, 0150001, 0051534, 0057320, 0136134,
97 	0017021, 0013406, 0043757, 0177773, 0150377, 0022757, 0000004, 0050000,
98 	0121700, 0167404, 0002034, 0000000, 0000004, 0136000, 0017021, 0013406,
99 	0025757, 0177773, 0150377, 0002254, 0003757, 0000004, 0152000, 0002120,
100 	0000000, 0126320, 0050004, 0126320, 0051010, 0002423, 0051321, 0012401,
101 	0152010, 0075521, 0050122, 0051120, 0011404, 0150413, 0051120, 0001436,
102 	0050324, 0150004, 0050001, 0000004, 0000000, 0002274, 0003036, 0167427,
103 	0175354, 0177777, 0000004, 0000000, 0000000, 0000000, 0000000, 0000000,
104 	0000000, 0000000, 0000000, 0000000, 0000000, 0000000, 0000000, 0000000,
105 /* *, */
106 	0000000, 0000000, 0072457, 0071163, 0063457, 0066541, 0071545, 0061057,
107 	0061541, 0063553, 0066541, 0067555, 0000156, 0060542, 0065543, 0060547,
108 	0066555, 0067157, 0071400, 0000000, 0000000, 0000000, 0006620, 0000000,
109 	0006620, 0000000, 0000001, 0000000, 0000000, 0000000, 0000000, 0000000,
110 	0000000, 0000000, 0000402, 0000000, 0000000, 0000000, 0000000, 0000000,
111 	0000000, 0000000, 0001006, 0000000, 0000000, 0000000, 0000000, 0000000,
112 	0000000, 0000000, 0000000, 0000000, 0000000, 0000000, 0000000, 0000000,
113 /* *, */
114 	0000000, 0000000, 0000000, 0000000, 0004550, 0000000, 0000000, 0000000,
115 	0000000, 0000000, 0000000, 0000000, 0000000, 0000000, 0000000, 0000000,
116 	0000000, 0000000, 0010620, 0000000, 0000000, 0000000, 0000000, 0000000,
117 	0000000, 0000000, 0000000, 0000000, 0000000, 0000000, 0000000, 0000000,
118 /* *, */
119 	0000004, 0000000, 0000004, 0000013, 0000000, 0000000, 0000013, 0000000,
120 	0000002, 0000000, 0000001, 0000000, 0000020, 0000000, 0000004, 0000000,
121 	0000074, 0000000, 0000030, 0000000, 0000004, 0000000, 0000140, 0000000,
122 	0000040, 0000000, 0000004, 0000005, 0000160, 0000000, 0000050, 0000000,
123 	0000004, 0000007, 0000210, 0000000, 0000061, 0000000, 0000004, 0000001,
124 	0000224, 0000000, 0000070, 0000000, 0000004, 0000013, 0000240, 0000000,
125 	0000101, 0000000, 0000004, 0000013, 0000254, 0000000, 0000112, 0000000,
126 	0000004, 0000001, 0001244, 0000000, 0000121, 0000000, 0000004, 0000007,
127 	0001244, 0000000, 0000131, 0000000, 0000004, 0000001, 0001264, 0000000,
128 	0000142, 0000000, 0000004, 0000002, 0001324, 0000000, 0000151, 0000000,
129 	0000004, 0000002, 0001400, 0000000, 0000161, 0000000, 0000004, 0000001,
130 	0001420, 0000000, 0000172, 0000000, 0000004, 0000004, 0002474, 0000000,
131 	0000201, 0000000, 0000004, 0000005, 0002600, 0000000, 0000210, 0000000,
132 	0000004, 0000000, 0002650, 0000000, 0000220, 0000000, 0000005, 0001640,
133 	0000224, 0000000, 0000226, 0000000, 0000005, 0002416, 0000000, 0000000,
134 	0000234, 0000000, 0000005, 0001576, 0000074, 0000000, 0000242, 0000000,
135 	0000007, 0002562, 0004000, 0000000, 0000253, 0000000, 0000005, 0003572,
136 	0000140, 0000000, 0000262, 0000000, 0000005, 0003604, 0000160, 0000000,
137 	0000271, 0000000, 0000005, 0003317, 0000210, 0000000, 0000301, 0000000,
138 	0000005, 0004034, 0000240, 0000000, 0000310, 0000000, 0000005, 0001626,
139 	0001052, 0000000, 0000322, 0000000, 0000011, 0003627, 0004610, 0000000,
140 	0000331, 0000000, 0000005, 0001146, 0000254, 0000000, 0000342, 0000000,
141 	0000005, 0004225, 0002650, 0000000, 0000351, 0000000, 0000007, 0001456,
142 	0004050, 0000000, 0000357, 0000000, 0000005, 0003463, 0001264, 0000000,
143 	0000367, 0000000, 0000011, 0003012, 0004620, 0000000, 0000377, 0000000,
144 	0000005, 0003253, 0001420, 0000000, 0000407, 0000000, 0000005, 0003114,
145 	0000720, 0000000, 0000417, 0000000, 0000005, 0003001, 0001114, 0000000,
146 	0000427, 0000000, 0000007, 0002664, 0004550, 0000000, 0000441, 0000000,
147 	0000005, 0003473, 0001244, 0000000, 0000450, 0000000, 0000005, 0001571,
148 	0002174, 0000000, 0000456, 0000000, 0000011, 0002732, 0006620, 0000000,
149 	0000466, 0000000, 0000005, 0001673, 0001352, 0000000, 0000474, 0000000,
150 	0000005, 0002033, 0001324, 0000000, 0000502, 0000000, 0000005, 0003614,
151 	0001400, 0000000, 0000511, 0000000, 0000005, 0003200, 0002600, 0000000,
152 	0000516, 0000000, 0000005, 0001701, 0002474, 0000000, 0000524, 0000000,
153 	0000005, 0003067, 0002230, 0000000, 0000535, 0000000, 0000011, 0002714,
154 	0010620, 0000000, 0000542, 0000000, 0000005, 0002717, 0002551, 0000000,
155 	0000547, 0000000, 0071143, 0030164, 0067456, 0062400, 0064570, 0000164,
156 	0074145, 0061545, 0027154, 0000157, 0074145, 0061545, 0027154, 0000157,
157 	0074145, 0061545, 0027166, 0000157, 0074145, 0061545, 0062566, 0067456,
158 	0062400, 0064570, 0027164, 0000157, 0062543, 0071162, 0071157, 0067456,
159 	0063000, 0071554, 0072542, 0027146, 0000157, 0060544, 0060564, 0067456,
160 	0061400, 0067554, 0062563, 0067456, 0064400, 0060563, 0072164, 0027171,
161 	0000157, 0072163, 0074564, 0067456, 0064400, 0061557, 0066164, 0067456,
162 	0066400, 0066141, 0067554, 0027143, 0000157, 0061163, 0065562, 0067456,
163 	0072400, 0064544, 0027166, 0000157, 0071167, 0072151, 0027145, 0000157,
164 	0062537, 0064570, 0000164, 0072163, 0071141, 0000164, 0066537, 0064541,
165 	0000156, 0062537, 0073156, 0071151, 0067157, 0057400, 0074145, 0061545,
166 	0000154, 0062537, 0062570, 0073143, 0057400, 0074145, 0061545, 0062566,
167 	0061400, 0071145, 0067562, 0000162, 0057537, 0066143, 0060545, 0072556,
168 	0000160, 0062537, 0071162, 0067556, 0057400, 0063137, 0071554, 0072542,
169 	0000146, 0073537, 0064562, 0062564, 0057400, 0064537, 0061157, 0057400,
170 	0071551, 0072141, 0074564, 0057400, 0071537, 0061157, 0063165, 0057400,
171 	0060555, 0066154, 0061557, 0057400, 0063146, 0072554, 0064163, 0057400,
172 	0061546, 0067554, 0062563, 0057400, 0066137, 0071541, 0061164, 0063165,
173 	0057400, 0066143, 0071557, 0000145, 0063137, 0062562, 0000145, 0057537,
174 	0064563, 0072542, 0000146, 0063537, 0072164, 0000171, 0071537, 0072164,
175 	0000171, 0064537, 0061557, 0066164, 0072400, 0064544, 0000166, 0071537,
176 	0071142, 0000153, 0071137, 0060545, 0066154, 0061557, 0057400, 0067145,
177 	0000144, 0061137, 0065562, 0000000,
178 };
179 
180 static char	confirm[] = "Are you sure you want to leave now?";
181 static char	prompt[] = "Enter a file name:  ";
182 static char	exist1[] = "The file '";
183 static char	exist2[] =
184 	"' already exists.\nAre you sure you want to use this file?";
185 static char	cantuse[] = "\nCan't use ";
186 static char	saved[] = "This file has been saved on the file '";
187 static char	type[] = "'.\nType '";
188 static char	rec[] = "' to recover your game.\n\n";
189 static char	cantrec[] = "Can't recover file:  ";
190 
191 save (n)
192 register int	n;
193 
194 {
195 	register int	fdesc;
196 	register char	*fs;
197 	char		fname[50];
198 
199 	if (n)  {
200 		if (tflag)  {
201 			curmove (20,0);
202 			clend();
203 		} else
204 			writec ('\n');
205 		writel (confirm);
206 		if (! yorn(0))
207 			return;
208 	}
209 	cflag = 1;
210 	for (;;)  {
211 		writel (prompt);
212 		fs = fname;
213 		while ((*fs = readc()) != '\n')  {
214 			if (*fs == tty.sg_erase)  {
215 				if (fs > fname)  {
216 					fs--;
217 					if (tflag)
218 						curmove (curr,curc-1);
219 					else
220 						writec (*fs);
221 				} else
222 					writec ('\007');
223 				continue;
224 			}
225 			writec (*fs++);
226 		}
227 		*fs = '\0';
228 		if ((fdesc = open(fname,2)) == -1 && errno == 2)  {
229 			if ((fdesc = creat (fname,0700)) != -1)
230 			break;
231 		}
232 		if (fdesc != -1)  {
233 			if (tflag)  {
234 				curmove (18,0);
235 				clend();
236 			} else
237 				writec ('\n');
238 			writel (exist1);
239 			writel (fname);
240 			writel (exist2);
241 			cflag = 0;
242 			close (fdesc);
243 			if (yorn (0))  {
244 				unlink (fname);
245 				fdesc = creat (fname,0700);
246 				break;
247 			} else  {
248 				cflag = 1;
249 				continue;
250 			}
251 		}
252 		writel (cantuse);
253 		writel (fname);
254 		writel (".\n");
255 		close (fdesc);
256 		cflag = 1;
257 	}
258 	write (fdesc,code,sizeof code);
259 	write (fdesc,board,52);
260 	write (fdesc,off,4);
261 	write (fdesc,in,4);
262 	write (fdesc,dice,4);
263 	write (fdesc,&cturn,2);
264 	write (fdesc,&dlast,2);
265 	write (fdesc,&pnum,2);
266 	write (fdesc,&rscore,2);
267 	write (fdesc,&wscore,2);
268 	write (fdesc,&gvalue,2);
269 	write (fdesc,&raflag,2);
270 	close (fdesc);
271 	if (tflag)
272 		curmove (18,0);
273 	writel (saved);
274 	writel (fname);
275 	writel (type);
276 	writel (fname);
277 	writel (rec);
278 	if (tflag)
279 		clend();
280 	getout ();
281 }
282 
283 recover (s)
284 char	*s;
285 
286 {
287 	register int	i;
288 	int		fdesc;
289 
290 	if ((fdesc = open (s,0)) == -1)
291 		norec (s);
292 	read (fdesc,board,52);
293 	for (i = 0; i < 26; i++)
294 		if (board[i] != code[i])
295 			norec (s);
296 	if (lseek (fdesc,(long) sizeof code,0) == -1)
297 		norec (s);
298 	read (fdesc,board,52);
299 	read (fdesc,off,4);
300 	read (fdesc,in,4);
301 	read (fdesc,dice,4);
302 	read (fdesc,&cturn,2);
303 	read (fdesc,&dlast,2);
304 	read (fdesc,&pnum,2);
305 	read (fdesc,&rscore,2);
306 	read (fdesc,&wscore,2);
307 	read (fdesc,&gvalue,2);
308 	read (fdesc,&raflag,2);
309 	close (fdesc);
310 	rflag = 1;
311 }
312 
313 norec (s)
314 register char	*s;
315 
316 {
317 	register char	*c;
318 
319 	tflag = 0;
320 	writel (cantrec);
321 	c = s;
322 	while (*c != '\0')
323 		writec (*c++);
324 	getout ();
325 }
326