2 * $Id: dsi_read.c,v 1.7 2009-10-25 06:13:11 didg Exp $
4 * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
5 * All rights reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
16 #endif /* HAVE_UNISTD_H */
18 #include <sys/types.h>
20 #ifdef HAVE_SYS_FILIO_H
21 #include <sys/filio.h>
24 #include <atalk/dsi.h>
27 #define min(a,b) ((a) < (b) ? (a) : (b))
30 /* streaming i/o for afp_read. this is all from the perspective of the
31 * client. it basically does the reverse of dsi_write. on first entry,
32 * it will send off the header plus whatever is in its command
33 * buffer. it returns the amount of stuff still to be read
34 * (constrained by the buffer size). */
35 ssize_t dsi_readinit(DSI *dsi, void *buf, const size_t buflen,
36 const size_t size, const int err)
39 dsi->noreply = 1; /* we will handle our own replies */
40 dsi->header.dsi_flags = DSIFL_REPLY;
41 /*dsi->header.dsi_command = DSIFUNC_CMD;*/
42 dsi->header.dsi_len = htonl(size);
43 dsi->header.dsi_code = htonl(err);
46 if (dsi_stream_send(dsi, buf, buflen)) {
47 dsi->datasize = size - buflen;
48 return min(dsi->datasize, buflen);
51 return -1; /* error */
54 void dsi_readdone(DSI *dsi)
59 /* send off the data */
60 ssize_t dsi_read(DSI *dsi, void *buf, const size_t buflen)
64 len = dsi_stream_write(dsi, buf, buflen, 0);
68 return min(dsi->datasize, buflen);