82400896dd09994ebe207f3b99d3068c09dc39f6
[bluesky.git] / TBBT / trace_play / rpc / xdr_stdio.c
1 #ifndef lint
2 static char sfs_xdr_stdio_id[] = "@(#)xdr_stdio.c     2.1     97/10/23";
3 #endif
4 /* @(#)xdr_stdio.c      2.1 88/07/29 4.0 RPCSRC */
5 /*
6  *   Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
7  *      All rights reserved.
8  *              Standard Performance Evaluation Corporation (SPEC)
9  *              6585 Merchant Place, Suite 100
10  *              Warrenton, VA 20187
11  *
12  *      This product contains benchmarks acquired from several sources who
13  *      understand and agree with SPEC's goal of creating fair and objective
14  *      benchmarks to measure computer performance.
15  *
16  *      This copyright notice is placed here only to protect SPEC in the
17  *      event the source is misused in any manner that is contrary to the
18  *      spirit, the goals and the intent of SPEC.
19  *
20  *      The source code is provided to the user or company under the license
21  *      agreement for the SPEC Benchmark Suite for this product.
22  */
23 /*
24  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
25  * unrestricted use provided that this legend is included on all tape
26  * media and as a part of the software program in whole or part.  Users
27  * may copy or modify Sun RPC without charge, but are not authorized
28  * to license or distribute it to anyone else except as part of a product or
29  * program developed by the user.
30  * 
31  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
32  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
33  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
34  * 
35  * Sun RPC is provided with no support and without any obligation on the
36  * part of Sun Microsystems, Inc. to assist in its use, correction,
37  * modification or enhancement.
38  * 
39  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
40  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
41  * OR ANY PART THEREOF.
42  * 
43  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
44  * or profits or other special, indirect and consequential damages, even if
45  * Sun has been advised of the possibility of such damages.
46  * 
47  * Sun Microsystems, Inc.
48  * 2550 Garcia Avenue
49  * Mountain View, California  94043
50  */
51 #if !defined(lint) && defined(SCCSIDS)
52 static char sccsid[] = "@(#)xdr_stdio.c 1.16 87/08/11 Copyr 1984 Sun Micro";
53 #endif
54
55 /*
56  * xdr_stdio.c, XDR implementation on standard i/o file.
57  *
58  * Copyright (C) 1984, Sun Microsystems, Inc.
59  *
60  * This set of routines implements a XDR on a stdio stream.
61  * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
62  * from the stream.
63  */
64
65 #include <stdio.h>
66 #include <sys/types.h>
67 #include "rpc/types.h"
68 #include "rpc/xdr.h"
69 #include "rpc/osdep.h" 
70
71 static bool_t   xdrstdio_getlong(XDR *, int32_t *);
72 static bool_t   xdrstdio_putlong(XDR *, int32_t *);
73 static bool_t   xdrstdio_getbytes(XDR *, void *, uint_t);
74 static bool_t   xdrstdio_putbytes(XDR *, void *, uint_t);
75 static uint_t   xdrstdio_getpos(XDR *);
76 static bool_t   xdrstdio_setpos(XDR *, uint_t);
77 static int32_t *xdrstdio_inline(XDR *, uint_t);
78 static void     xdrstdio_destroy(XDR *);
79
80 /*
81  * Ops vector for stdio type XDR
82  */
83 static struct xdr_ops   xdrstdio_ops = {
84         xdrstdio_getlong,       /* deseraialize a long int */
85         xdrstdio_putlong,       /* seraialize a long int */
86         xdrstdio_getbytes,      /* deserialize counted bytes */
87         xdrstdio_putbytes,      /* serialize counted bytes */
88         xdrstdio_getpos,        /* get offset in the stream */
89         xdrstdio_setpos,        /* set offset in the stream */
90         xdrstdio_inline,        /* prime stream for inline macros */
91         xdrstdio_destroy        /* destroy stream */
92 };
93
94 /*
95  * Initialize a stdio xdr stream.
96  * Sets the xdr stream handle xdrs for use on the stream file.
97  * Operation flag is set to op.
98  */
99 void
100 xdrstdio_create(
101         XDR *xdrs,
102         FILE *file,
103         enum xdr_op op)
104 {
105
106         xdrs->x_op = op;
107         xdrs->x_ops = &xdrstdio_ops;
108         xdrs->x_private = (void *)file;
109         xdrs->x_handy = 0;
110         xdrs->x_base = 0;
111 }
112
113 /*
114  * Destroy a stdio xdr stream.
115  * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
116  */
117 static void
118 xdrstdio_destroy(
119         XDR *xdrs)
120 {
121         (void)fflush((FILE *)xdrs->x_private);
122         /* xx should we close the file ?? */
123 }
124
125 static bool_t
126 xdrstdio_getlong(
127         XDR *xdrs,
128         int32_t *lp)
129 {
130
131         if (fread((void *)lp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
132                 return (FALSE);
133         *lp = ntohl(*lp);
134         return (TRUE);
135 }
136
137 static bool_t
138 xdrstdio_putlong(
139         XDR *xdrs,
140         int32_t *lp)
141 {
142
143         int32_t mycopy = htonl(*lp);
144         lp = &mycopy;
145
146         if (fwrite((void *)lp, sizeof(int32_t), 1, (FILE *)xdrs->x_private) != 1)
147                 return (FALSE);
148         return (TRUE);
149 }
150
151 static bool_t
152 xdrstdio_getbytes(
153         XDR *xdrs,
154         void * addr,
155         uint_t len)
156 {
157
158         if ((len != 0) && (fread(addr, (int)len, 1, (FILE *)xdrs->x_private) != 1))
159                 return (FALSE);
160         return (TRUE);
161 }
162
163 static bool_t
164 xdrstdio_putbytes(
165         XDR *xdrs,
166         void * addr,
167         uint_t len)
168 {
169
170         if ((len != 0) && (fwrite(addr, (int)len, 1, (FILE *)xdrs->x_private) != 1))
171                 return (FALSE);
172         return (TRUE);
173 }
174
175 static uint_t
176 xdrstdio_getpos(
177         XDR *xdrs)
178 {
179
180         return ((uint_t) ftell((FILE *)xdrs->x_private));
181 }
182
183 static bool_t
184 xdrstdio_setpos(
185         XDR *xdrs,
186         uint_t pos)
187
188
189         return ((fseek((FILE *)xdrs->x_private, (int32_t)pos, 0) < 0) ?
190                 FALSE : TRUE);
191 }
192
193 /* ARGSUSED */
194 static int32_t *
195 xdrstdio_inline(
196         XDR *xdrs,
197         uint_t len)
198 {
199
200         /*
201          * Must do some work to implement this: must insure
202          * enough data in the underlying stdio buffer,
203          * that the buffer is aligned so that we can indirect through a
204          * int32_t *, and stuff this pointer in xdrs->x_buf.  Doing
205          * a fread or fwrite to a scratch buffer would defeat
206          * most of the gains to be had here and require storage
207          * management on this buffer, so we don't do this.
208          */
209         return (NULL);
210 }