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 */
18 #endif /* HAVE_UNISTD_H */
19 #include <sys/types.h>
24 #endif /* HAVE_FCNTL_H */
27 #include <atalk/dsi.h>
28 #include <netatalk/endian.h>
31 #define MIN(a,b) ((a) < (b) ? (a) : (b))
34 /* initialize relevant things for dsi_write. this returns the amount
35 * of data in the data buffer. the interface has been reworked to allow
36 * for arbitrary buffers. */
37 size_t dsi_writeinit(DSI *dsi, void *buf, const size_t buflen _U_)
41 /* figure out how much data we have. do a couple checks for 0
43 header = ntohl(dsi->header.dsi_code);
44 dsi->datasize = header ? ntohl(dsi->header.dsi_len) - header : 0;
45 if (dsi->datasize > 0) {
46 len = MIN(sizeof(dsi->commands) - header, dsi->datasize);
48 /* write last part of command buffer into buf */
49 memcpy(buf, dsi->commands + header, len);
51 /* recalculate remaining data */
59 /* fill up buf and then return. this should be called repeatedly
60 * until all the data has been read. i block alarm processing
61 * during the transfer to avoid sending unnecessary tickles. */
62 size_t dsi_write(DSI *dsi, void *buf, const size_t buflen)
66 if (((length = MIN(buflen, dsi->datasize)) > 0) &&
67 ((length = dsi_stream_read(dsi, buf, length)) > 0)) {
68 dsi->datasize -= length;
74 /* flush any unread buffers. */
75 void dsi_writeflush(DSI *dsi)
79 while (dsi->datasize > 0) {
80 length = dsi_stream_read(dsi, dsi->data,
81 MIN(sizeof(dsi->data), dsi->datasize));
83 dsi->datasize -= length;