1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32: #include <sys/cdefs.h>
33: #ifndef lint
34: #if 0
35: static char sccsid[] = "@(#)execute.c 8.1 (Berkeley) 5/31/93";
36: #else
37: __RCSID("$NetBSD: execute.c,v 1.11 2004/01/27 20:30:30 jsm Exp $");
38: #endif
39: #endif
40:
41: #include "monop.ext"
42: #include <fcntl.h>
43: #include <stdlib.h>
44: #include <unistd.h>
45: #include <sys/types.h>
46: #include <sys/stat.h>
47: #include <sys/time.h>
48: #include <time.h>
49:
50: #define SEGSIZE 8192
51:
52: typedef struct stat STAT;
53: typedef struct tm TIME;
54:
55: static char buf[257];
56:
57: static bool new_play;
58: extern void *heapstart;
59:
60: static void show_move(void);
61:
62:
63:
64:
65: void
66: execute(com_num)
67: int com_num;
68: {
69: new_play = FALSE;
70: (*func[com_num])();
71: notify();
72: force_morg();
73: if (new_play)
74: next_play();
75: else if (num_doub)
76: printf("%s rolled doubles. Goes again\n", cur_p->name);
77: }
78:
79:
80:
81:
82: void
83: do_move()
84: {
85: int r1, r2;
86: bool was_jail;
87:
88: new_play = was_jail = FALSE;
89: printf("roll is %d, %d\n", r1=roll(1, 6), r2=roll(1, 6));
90: if (cur_p->loc == JAIL) {
91: was_jail++;
92: if (!move_jail(r1, r2)) {
93: new_play++;
94: goto ret;
95: }
96: }
97: else {
98: if (r1 == r2 && ++num_doub == 3) {
99: printf("That's 3 doubles. You go to jail\n");
100: goto_jail();
101: new_play++;
102: goto ret;
103: }
104: move(r1+r2);
105: }
106: if (r1 != r2 || was_jail)
107: new_play++;
108: ret:
109: return;
110: }
111:
112:
113:
114:
115: void
116: move(rl)
117: int rl;
118: {
119: int old_loc;
120:
121: old_loc = cur_p->loc;
122: cur_p->loc = (cur_p->loc + rl) % N_SQRS;
123: if (cur_p->loc < old_loc && rl > 0) {
124: cur_p->money += 200;
125: printf("You pass %s and get $200\n", board[0].name);
126: }
127: show_move();
128: }
129:
130:
131:
132:
133: static void
134: show_move()
135: {
136: SQUARE *sqp;
137:
138: sqp = &board[cur_p->loc];
139: printf("That puts you on %s\n", sqp->name);
140: switch (sqp->type) {
141: case SAFE:
142: printf("That is a safe place\n");
143: break;
144: case CC:
145: cc(); break;
146: case CHANCE:
147: chance(); break;
148: case INC_TAX:
149: inc_tax(); break;
150: case GOTO_J:
151: goto_jail(); break;
152: case LUX_TAX:
153: lux_tax(); break;
154: case PRPTY:
155: case RR:
156: case UTIL:
157: if (sqp->owner < 0) {
158: printf("That would cost $%d\n", sqp->cost);
159: if (getyn("Do you want to buy? ") == 0) {
160: buy(player, sqp);
161: cur_p->money -= sqp->cost;
162: }
163: else if (num_play > 2)
164: bid();
165: }
166: else if (sqp->owner == player)
167: printf("You own it.\n");
168: else
169: rent(sqp);
170: }
171: }
172:
173:
174:
175:
176: void
177: save()
178: {
179: char *sp;
180: int outf, num;
181: time_t t;
182: struct stat sb;
183: char *start, *end;
184:
185: printf("Which file do you wish to save it in? ");
186: sp = buf;
187: while ((*sp++=getchar()) != '\n')
188: continue;
189: *--sp = '\0';
190:
191:
192:
193:
194:
195: if (stat(buf, &sb) > -1
196: && getyn("File exists. Do you wish to overwrite? ") > 0)
197: return;
198:
199: if ((outf=creat(buf, 0644)) < 0) {
200: perror(buf);
201: return;
202: }
203: printf("\"%s\" ", buf);
204: time(&t);
205: strcpy(buf, ctime(&t));
206: for (sp = buf; *sp != '\n'; sp++)
207: continue;
208: *sp = '\0';
209: start = heapstart;
210: end = sbrk(0);
211: while (start < end) {
212: num = start + 16 * 1024 > end ? end - start : 16 * 1024;
213: write(outf, start, num);
214: start += num;
215: }
216: close(outf);
217: printf("[%s]\n", buf);
218: }
219:
220:
221:
222:
223: void
224: restore()
225: {
226: char *sp;
227:
228: printf("Which file do you wish to restore from? ");
229: for (sp = buf; (*sp=getchar()) != '\n'; sp++)
230: continue;
231: *sp = '\0';
232: rest_f(buf);
233: }
234:
235:
236:
237:
238:
239: int
240: rest_f(file)
241: const char *file;
242: {
243: char *sp;
244: int inf, num;
245: char buf[80];
246: char *start, *end;
247: STAT sbuf;
248:
249: if ((inf=open(file, O_RDONLY)) < 0) {
250: perror(file);
251: return FALSE;
252: }
253: printf("\"%s\" ", file);
254: if (fstat(inf, &sbuf) < 0) {
255: perror(file);
256: exit(1);
257: }
258: start = heapstart;
259: brk(end = start + sbuf.st_size);
260: while (start < end) {
261: num = start + 16 * 1024 > end ? end - start : 16 * 1024;
262: read(inf, start, num);
263: start += num;
264: }
265: close(inf);
266: strcpy(buf, ctime(&sbuf.st_mtime));
267: for (sp = buf; *sp != '\n'; sp++)
268: continue;
269: *sp = '\0';
270: printf("[%s]\n", buf);
271: return TRUE;
272: }