60 likes | 155 Views
/* Descripción XDR */ const MAXUSERNAME=32; const MAXFILELEN=65535; const MAXNAMELEN=255; enum filekind { TEXT=0, DATA=1, EXEC=2 }; union filetype switch (filekind kind) { case TEXT: void; case DATA: string creator<MAXNAMELEN>;
E N D
/* Descripción XDR */ const MAXUSERNAME=32; const MAXFILELEN=65535; const MAXNAMELEN=255; enum filekind { TEXT=0, DATA=1, EXEC=2 }; union filetype switch (filekind kind) { case TEXT: void; case DATA: string creator<MAXNAMELEN>; case EXEC: string interpretor<MAXNAMELEN>; }; struct file { string filename<MAXNAMELEN>; filetype type; string owner<MAXUSERNAME>; opaque data <MAXFILELEN>; }; fileXDR.x
fileXDR.h struct filetype { filekind kind; union { char *creator; char *interpretor; } filetype_u; }; typedef struct filetype filetype; bool_t xdr_filetype(); struct file { char *filename; filetype type; char *owner; struct { u_int data_len; char *data_val; } data; }; typedef struct file file; bool_t xdr_file(); #include <rpc/types.h> #define MAXUSERNAME 32 #define MAXFILELEN 65535 #define MAXNAMELEN 255 enum filekind { TEXT = 0, DATA = 1, EXEC = 2, }; typedef enum filekind filekind; bool_t xdr_filekind();
fileXDR_xdr.c (I) #include <rpc/rpc.h> #include "fileXDR.h" bool_t xdr_filekind(xdrs, objp) XDR *xdrs; filekind *objp; { if (!xdr_enum(xdrs, (enum_t *)objp)) { return (FALSE); } return (TRUE); }
bool_t xdr_filetype(xdrs, objp) XDR *xdrs; filetype *objp; { if (!xdr_filekind(xdrs, &objp->kind)) { return (FALSE); } switch (objp->kind) { case TEXT: break; case DATA: if (!xdr_string(xdrs, &objp->filetype_u.creator, MAXNAMELEN)) { return (FALSE); } break; case EXEC: if (!xdr_string(xdrs, &objp->filetype_u.interpretor, MAXNAMELEN)){ return (FALSE); } break; default: return (FALSE); } return (TRUE); } fileXDR_xdr.c (II)
fileXDR_xdr.c (III) bool_t xdr_file(xdrs, objp) XDR *xdrs; file *objp; { if (!xdr_string(xdrs, &objp->filename, MAXNAMELEN)) { return (FALSE); } if (!xdr_filetype(xdrs, &objp->type)) { return (FALSE); } if (!xdr_string(xdrs, &objp->owner, MAXUSERNAME)) { return (FALSE); } if (!xdr_bytes(xdrs, (char **)&objp->data.data_val, (u_int *)&objp->data.data_len, MAXFILELEN)) { return (FALSE); } return (TRUE); }
Escribir.c #include <stdio.h> #include <rpc/rpc.h> #include "fileXDR.h" int main() { XDR xdr_handle; FILE *fp; file fichero; char data[5]; data[0]='a'; data[1]='b'; data[2]='c'; data[3]='d'; data[4]='e'; if((fp=fopen("salida","w"))!=NULL) { xdrstdio_create(&xdr_handle,fp,XDR_ENCODE); fichero.filename="nombrefichero"; fichero.type.kind=TEXT; fichero.owner="nombrepropietario"; fichero.data.data_len=5; fichero.data.data_val=data; xdr_file(&xdr_handle,&fichero); xdr_destroy(&xdr_handle); fclose(fp); } }