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 <atalk/util.h>
25 #include <atalk/logger.h>
27 /* initialize relevant things for dsi_write. this returns the amount
28 * of data in the data buffer. the interface has been reworked to allow
29 * for arbitrary buffers. */
30 size_t dsi_writeinit(DSI *dsi, void *buf, const size_t buflen _U_)
34 /* figure out how much data we have. do a couple checks for 0
36 header = ntohl(dsi->header.dsi_code);
37 dsi->datasize = header ? ntohl(dsi->header.dsi_len) - header : 0;
39 if (dsi->datasize > 0) {
40 len = MIN(sizeof(dsi->commands) - header, dsi->datasize);
42 /* write last part of command buffer into buf */
43 memcpy(buf, dsi->commands + header, len);
45 /* recalculate remaining data */
50 LOG(log_maxdebug, logtype_dsi, "dsi_writeinit: len: %ju, remaining DSI datasize: %jd",
51 (intmax_t)len, (intmax_t)dsi->datasize);
56 /* fill up buf and then return. this should be called repeatedly
57 * until all the data has been read. i block alarm processing
58 * during the transfer to avoid sending unnecessary tickles. */
59 size_t dsi_write(DSI *dsi, void *buf, const size_t buflen)
63 LOG(log_maxdebug, logtype_dsi, "dsi_write: remaining DSI datasize: %jd", (intmax_t)dsi->datasize);
65 if ((length = MIN(buflen, dsi->datasize)) > 0) {
66 if ((length = dsi_stream_read(dsi, buf, length)) > 0) {
67 LOG(log_maxdebug, logtype_dsi, "dsi_write: received: %ju", (intmax_t)length);
68 dsi->datasize -= length;
75 /* flush any unread buffers. */
76 void dsi_writeflush(DSI *dsi)
80 while (dsi->datasize > 0) {
81 length = dsi_stream_read(dsi, dsi->data,
82 MIN(sizeof(dsi->data), dsi->datasize));
84 dsi->datasize -= length;