source: rtems-libbsd/freebsd/lib/libc/xdr/xdr_stdio.c @ f41a394

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since f41a394 was f41a394, checked in by Sebastian Huber <sebastian.huber@…>, on Jun 9, 2016 at 9:46:09 AM

XDR(3): Import from FreeBSD

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