2 * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
3 * All rights reserved. See COPYRIGHT.
5 * 7 Oct 1997 added checks for 0 data.
8 /* this streams writes */
11 #include <sys/types.h>
17 #include <atalk/dsi.h>
18 #include <netatalk/endian.h>
21 #define MIN(a,b) ((a) < (b) ? (a) : (b))
24 /* initialize relevant things for dsi_write. this returns the amount
25 * of data in the data buffer. the interface has been reworked to allow
26 * for arbitrary buffers. */
27 size_t dsi_writeinit(DSI *dsi, void *buf, const size_t buflen)
29 const struct itimerval none = {{0, 0}, {0, 0}};
32 /* figure out how much data we have. do a couple checks for 0
34 header = ntohl(dsi->header.dsi_code);
35 dsi->datasize = header ? ntohl(dsi->header.dsi_len) - header : 0;
36 if (dsi->datasize > 0) {
37 len = MIN(sizeof(dsi->commands) - header, dsi->datasize);
39 /* write last part of command buffer into buf */
40 memcpy(buf, dsi->commands + header, len);
42 /* recalculate remaining data */
47 /* deal with signals. i'm doing it this way to ensure that we don't
48 * get confused if a writeflush on zero remaining data is, for some
50 sigprocmask(SIG_BLOCK, &dsi->sigblockset, NULL);
51 setitimer(ITIMER_REAL, &none, &dsi->savetimer);
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;
68 setitimer(ITIMER_REAL, &dsi->savetimer, NULL);
69 sigprocmask(SIG_UNBLOCK, &dsi->sigblockset, NULL);
73 /* flush any unread buffers. */
74 void dsi_writeflush(DSI *dsi)
78 while (dsi->datasize > 0) {
79 length = dsi_stream_read(dsi, dsi->data,
80 MIN(sizeof(dsi->data), dsi->datasize));
82 dsi->datasize -= length;
87 setitimer(ITIMER_REAL, &dsi->savetimer, NULL);
88 sigprocmask(SIG_UNBLOCK, &dsi->sigblockset, NULL);