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>
24 #include <netatalk/endian.h>
27 #define MIN(a,b) ((a) < (b) ? (a) : (b))
30 /* initialize relevant things for dsi_write. this returns the amount
31 * of data in the data buffer. the interface has been reworked to allow
32 * for arbitrary buffers. */
33 size_t dsi_writeinit(DSI *dsi, void *buf, const size_t buflen _U_)
37 /* figure out how much data we have. do a couple checks for 0
39 header = ntohl(dsi->header.dsi_code);
40 dsi->datasize = header ? ntohl(dsi->header.dsi_len) - header : 0;
41 if (dsi->datasize > 0) {
42 len = MIN(sizeof(dsi->commands) - header, dsi->datasize);
44 /* write last part of command buffer into buf */
45 memcpy(buf, dsi->commands + header, len);
47 /* recalculate remaining data */
55 /* fill up buf and then return. this should be called repeatedly
56 * until all the data has been read. i block alarm processing
57 * during the transfer to avoid sending unnecessary tickles. */
58 size_t dsi_write(DSI *dsi, void *buf, const size_t buflen)
62 if (((length = MIN(buflen, dsi->datasize)) > 0) &&
63 ((length = dsi_stream_read(dsi, buf, length)) > 0)) {
64 dsi->datasize -= length;
70 /* flush any unread buffers. */
71 void dsi_writeflush(DSI *dsi)
75 while (dsi->datasize > 0) {
76 length = dsi_stream_read(dsi, dsi->data,
77 MIN(sizeof(dsi->data), dsi->datasize));
79 dsi->datasize -= length;