2 * $Id: dsi_write.c,v 1.5 2009-10-20 04:31:41 didg Exp $
4 * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
5 * All rights reserved. See COPYRIGHT.
7 * 7 Oct 1997 added checks for 0 data.
12 #endif /* HAVE_CONFIG_H */
14 /* this streams writes */
17 #include <sys/types.h>
23 #include <atalk/dsi.h>
26 #define MIN(a,b) ((a) < (b) ? (a) : (b))
29 /* initialize relevant things for dsi_write. this returns the amount
30 * of data in the data buffer. the interface has been reworked to allow
31 * for arbitrary buffers. */
32 size_t dsi_writeinit(DSI *dsi, void *buf, const size_t buflen _U_)
36 /* figure out how much data we have. do a couple checks for 0
38 header = ntohl(dsi->header.dsi_code);
39 dsi->datasize = header ? ntohl(dsi->header.dsi_len) - header : 0;
40 if (dsi->datasize > 0) {
41 len = MIN(sizeof(dsi->commands) - header, dsi->datasize);
43 /* write last part of command buffer into buf */
44 memcpy(buf, dsi->commands + header, len);
46 /* recalculate remaining data */
54 /* fill up buf and then return. this should be called repeatedly
55 * until all the data has been read. i block alarm processing
56 * during the transfer to avoid sending unnecessary tickles. */
57 size_t dsi_write(DSI *dsi, void *buf, const size_t buflen)
61 if (((length = MIN(buflen, dsi->datasize)) > 0) &&
62 ((length = dsi_stream_read(dsi, buf, length)) > 0)) {
63 dsi->datasize -= length;
69 /* flush any unread buffers. */
70 void dsi_writeflush(DSI *dsi)
74 while (dsi->datasize > 0) {
75 length = dsi_stream_read(dsi, dsi->data,
76 MIN(sizeof(dsi->data), dsi->datasize));
78 dsi->datasize -= length;