|
|
1.1 root 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: (1) source distributions retain this entire copyright
7: * notice and comment, and (2) distributions including binaries display
8: * the following acknowledgement: ``This product includes software
9: * developed by the University of California, Berkeley and its contributors''
10: * in the documentation or other materials provided with the distribution
11: * and in all advertising materials mentioning features or use of this
12: * software. Neither the name of the University nor the names of its
13: * contributors may be used to endorse or promote products derived
14: * from this software without specific prior written permission.
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18: */
19:
20: #ifndef lint
21: static char sccsid[] = "@(#)misc.c 5.4 (Berkeley) 6/1/90";
22: #endif /* not lint */
23:
24: # include "monop.ext"
25: # include <ctype.h>
26: # include <signal.h>
27:
28: # define execsh(sh) execl(sh, shell_name[roll(1, num_names)-1], 0)
29:
30: static char *shell_def = "/bin/csh",
31: *shell_name[] = {
32: ".Hi Mom!",
33: ".Kick Me",
34: ".I'm really the next process down",
35: ".Hi Kids!",
36: ".This space for rent",
37: ".Singin' in the rain....",
38: ".I am but a Cog in the Wheel of Life",
39: ".Look out!!! Behind you!!!!!",
40: ".Looking for a good time, sailor?",
41: ".I don't get NO respect...",
42: ".Augghh! You peeked!"
43: };
44:
45: static int num_names = sizeof shell_name / sizeof (char *);;
46:
47: char *shell_in();
48:
49: /*
50: * This routine executes a truncated set of commands until a
51: * "yes or "no" answer is gotten.
52: */
53: getyn(prompt)
54: reg char *prompt; {
55:
56: reg int com;
57:
58: for (;;)
59: if ((com=getinp(prompt, yn)) < 2)
60: return com;
61: else
62: (*func[com-2])();
63: }
64: /*
65: * This routine tells the player if he's out of money.
66: */
67: notify() {
68:
69: if (cur_p->money < 0)
70: printf("That leaves you $%d in debt\n", -cur_p->money);
71: else if (cur_p->money == 0)
72: printf("that leaves you broke\n");
73: else if (fixing && !told_em && cur_p->money > 0) {
74: printf("-- You are now Solvent ---\n");
75: told_em = TRUE;
76: }
77: }
78: /*
79: * This routine switches to the next player
80: */
81: next_play() {
82:
83: player = ++player % num_play;
84: cur_p = &play[player];
85: num_doub = 0;
86: }
87: /*
88: * This routine gets an integer from the keyboard after the
89: * given prompt.
90: */
91: get_int(prompt)
92: reg char *prompt; {
93:
94: reg int num;
95: reg char *sp;
96: char buf[257];
97:
98: for (;;) {
99: inter:
100: printf(prompt);
101: num = 0;
102: for (sp = buf; (*sp=getchar()) != '\n'; sp++)
103: if (*sp == -1) /* check for interrupted system call */
104: goto inter;
105: if (sp == buf)
106: continue;
107: for (sp = buf; isspace(*sp); sp++)
108: continue;
109: for (; isdigit(*sp); sp++)
110: num = num * 10 + *sp - '0';
111: if (*sp == '\n')
112: return num;
113: else
114: printf("I can't understand that\n");
115: }
116: }
117: /*
118: * This routine sets the monopoly flag from the list given.
119: */
120: set_ownlist(pl)
121: int pl; {
122:
123: reg int num; /* general counter */
124: reg MON *orig; /* remember starting monop ptr */
125: reg OWN *op; /* current owned prop */
126: OWN *orig_op; /* origianl prop before loop */
127:
128: op = play[pl].own_list;
129: #ifdef DEBUG
130: printf("op [%d] = play[pl [%d] ].own_list;\n", op, pl);
131: #endif
132: while (op) {
133: #ifdef DEBUG
134: printf("op->sqr->type = %d\n", op->sqr->type);
135: #endif
136: switch (op->sqr->type) {
137: case UTIL:
138: #ifdef DEBUG
139: printf(" case UTIL:\n");
140: #endif
141: for (num = 0; op && op->sqr->type == UTIL; op = op->next)
142: num++;
143: play[pl].num_util = num;
144: #ifdef DEBUG
145: printf("play[pl].num_util = num [%d];\n", num);
146: #endif
147: break;
148: case RR:
149: #ifdef DEBUG
150: printf(" case RR:\n");
151: #endif
152: for (num = 0; op && op->sqr->type == RR; op = op->next) {
153: #ifdef DEBUG
154: printf("iter: %d\n", num);
155: printf("op = %d, op->sqr = %d, op->sqr->type = %d\n", op, op->sqr, op->sqr->type);
156: #endif
157: num++;
158: }
159: play[pl].num_rr = num;
160: #ifdef DEBUG
161: printf("play[pl].num_rr = num [%d];\n", num);
162: #endif
163: break;
164: case PRPTY:
165: #ifdef DEBUG
166: printf(" case PRPTY:\n");
167: #endif
168: orig = op->sqr->desc->mon_desc;
169: orig_op = op;
170: num = 0;
171: while (op && op->sqr->desc->mon_desc == orig) {
172: #ifdef DEBUG
173: printf("iter: %d\n", num);
174: #endif
175: num++;
176: #ifdef DEBUG
177: printf("op = op->next ");
178: #endif
179: op = op->next;
180: #ifdef DEBUG
181: printf("[%d];\n", op);
182: #endif
183: }
184: #ifdef DEBUG
185: printf("num = %d\n");
186: #endif
187: if (orig == 0) {
188: printf("panic: bad monopoly descriptor: orig = %d\n", orig);
189: printf("player # %d\n", pl+1);
190: printhold(pl);
191: printf("orig_op = %d\n", orig_op);
192: printf("orig_op->sqr->type = %d (PRPTY)\n", op->sqr->type);
193: printf("orig_op->next = %d\n", op->next);
194: printf("orig_op->sqr->desc = %d\n", op->sqr->desc);
195: printf("op = %d\n", op);
196: printf("op->sqr->type = %d (PRPTY)\n", op->sqr->type);
197: printf("op->next = %d\n", op->next);
198: printf("op->sqr->desc = %d\n", op->sqr->desc);
199: printf("num = %d\n", num);
200: }
201: #ifdef DEBUG
202: printf("orig->num_in = %d\n", orig->num_in);
203: #endif
204: if (num == orig->num_in)
205: is_monop(orig, pl);
206: else
207: isnot_monop(orig);
208: break;
209: }
210: }
211: }
212: /*
213: * This routine sets things up as if it is a new monopoly
214: */
215: is_monop(mp, pl)
216: reg MON *mp;
217: int pl; {
218:
219: reg char *sp;
220: reg int i;
221:
222: mp->owner = pl;
223: mp->num_own = mp->num_in;
224: for (i = 0; i < mp->num_in; i++)
225: mp->sq[i]->desc->monop = TRUE;
226: mp->name = mp->mon_n;
227: }
228: /*
229: * This routine sets things up as if it is no longer a monopoly
230: */
231: isnot_monop(mp)
232: reg MON *mp; {
233:
234: reg char *sp;
235: reg int i;
236:
237: mp->owner = -1;
238: for (i = 0; i < mp->num_in; i++)
239: mp->sq[i]->desc->monop = FALSE;
240: mp->name = mp->not_m;
241: }
242: /*
243: * This routine gives a list of the current player's routine
244: */
245: list() {
246:
247: printhold(player);
248: }
249: /*
250: * This routine gives a list of a given players holdings
251: */
252: list_all() {
253:
254: reg int pl;
255:
256: while ((pl=getinp("Whose holdings do you want to see? ", name_list)) < num_play)
257: printhold(pl);
258: }
259: /*
260: * This routine gives the players a chance before it exits.
261: */
262: quit() {
263:
264: putchar('\n');
265: if (getyn("Do you all really want to quit? ", yn) == 0)
266: exit(0);
267: signal(2, quit);
268: }
269: /*
270: * This routine copies one structure to another
271: */
272: cpy_st(s1, s2, size)
273: reg int *s1, *s2, size; {
274:
275: size /= 2;
276: while (size--)
277: *s1++ = *s2++;
278: }
279: /*
280: * This routine forks off a shell. It uses the users login shell
281: */
282: shell_out() {
283:
284: static char *shell = NULL;
285:
286: printline();
287: if (shell == NULL)
288: shell = shell_in();
289: fflush();
290: if (!fork()) {
291: signal(SIGINT, SIG_DFL);
292: execsh(shell);
293: }
294: ignoresigs();
295: wait();
296: resetsigs();
297: putchar('\n');
298: printline();
299: }
300: /*
301: * This routine looks up the users login shell
302: */
303: # include <sys/types.h>
304: # include <pwd.h>
305:
306: struct passwd *getpwuid();
307:
308: char *getenv();
309:
310: char *
311: shell_in() {
312:
313: reg struct passwd *pp;
314: reg char *sp;
315:
316: if ((sp = getenv("SHELL")) == NULL) {
317: pp = getpwuid(getuid());
318: if (pp->pw_shell[0] != '\0')
319: return pp->pw_shell;
320: else
321: return shell_def;
322: /*return (*(pp->pw_shell) != '\0' ? pp->pw_shell : shell_def);*/
323: }
324: return sp;
325: }
326: /*
327: * This routine sets things up to ignore all the signals.
328: */
329: ignoresigs() {
330:
331: reg int i;
332:
333: for (i = 0; i < NSIG; i++)
334: signal(i, SIG_IGN);
335: }
336: /*
337: * This routine sets up things as they were before.
338: */
339: resetsigs() {
340:
341: reg int i;
342:
343: for (i = 0; i < NSIG; i++)
344: signal(i, SIG_DFL);
345: signal(2, quit);
346: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.