xref: /dragonfly/games/mille/varpush.c (revision 45774827)
1 /*-
2  * Copyright (c) 1982, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)varpush.c	8.1 (Berkeley) 5/31/93
30  * $FreeBSD: src/games/mille/varpush.c,v 1.6 1999/12/12 06:17:25 billf Exp $
31  */
32 
33 #include <errno.h>
34 #include <paths.h>
35 #include "mille.h"
36 
37 /*
38  * @(#)varpush.c	1.1 (Berkeley) 4/1/82
39  */
40 
41 /*
42  *	push variables around via the routine func() on the file
43  * channel file.  func() is either readv or writev.
44  */
45 bool
46 varpush(int file, ssize_t (*func)(int, const struct iovec *, int))
47 {
48 	int		temp;
49 	const struct iovec iov[] = {
50 		{ (void *) &Debug,	sizeof Debug },
51 		{ (void *) &Finished,	sizeof Finished },
52 		{ (void *) &Order,	sizeof Order },
53 		{ (void *) &End,	sizeof End },
54 		{ (void *) &On_exit,	sizeof On_exit },
55 		{ (void *) &Handstart,	sizeof Handstart },
56 		{ (void *) &Numgos,	sizeof Numgos },
57 		{ (void *)  Numseen,	sizeof Numseen },
58 		{ (void *) &Play,	sizeof Play },
59 		{ (void *) &Window,	sizeof Window },
60 		{ (void *)  Deck,	sizeof Deck },
61 		{ (void *) &Discard,	sizeof Discard },
62 		{ (void *)  Player,	sizeof Player }
63 	};
64 
65 	if (((func)(file, iov, sizeof(iov) /sizeof(iov[0]))) < 0) {
66 		error(strerror(errno));
67 		return FALSE;
68 	}
69 
70 	if (func == readv) {
71 		if ((read(file, (void *) &temp, sizeof temp)) < 0) {
72 			error(strerror(errno));
73 			return FALSE;
74 		}
75 		Topcard = &Deck[temp];
76 #ifdef DEBUG
77 		if (Debug) {
78 			char	buf[80];
79 over:
80 			printf("Debug file:");
81 			fgets(buf, sizeof(buf), stdin);
82 			if ((outf = fopen(buf, "w")) == NULL) {
83 				perror(buf);
84 				goto over;
85 			}
86 			if (strcmp(buf, _PATH_DEVNULL) != 0)
87 				setbuf(outf, NULL);
88 		}
89 #endif
90 	} else {
91 		temp = Topcard - Deck;
92 		if ((write(file, (void *) &temp, sizeof temp)) < 0) {
93 			error(strerror(errno));
94 			return FALSE;
95 		}
96 	}
97 	return TRUE;
98 }
99