2 * $Id: dsi_write.c,v 1.3 2001-06-29 14:14:46 rufustfirefly 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)
39 const struct itimerval none = {{0, 0}, {0, 0}};
42 /* figure out how much data we have. do a couple checks for 0
44 header = ntohl(dsi->header.dsi_code);
45 dsi->datasize = header ? ntohl(dsi->header.dsi_len) - header : 0;
46 if (dsi->datasize > 0) {
47 len = MIN(sizeof(dsi->commands) - header, dsi->datasize);
49 /* write last part of command buffer into buf */
50 memcpy(buf, dsi->commands + header, len);
52 /* recalculate remaining data */
57 /* deal with signals. i'm doing it this way to ensure that we don't
58 * get confused if a writeflush on zero remaining data is, for some
60 sigprocmask(SIG_BLOCK, &dsi->sigblockset, NULL);
61 setitimer(ITIMER_REAL, &none, &dsi->savetimer);
65 /* fill up buf and then return. this should be called repeatedly
66 * until all the data has been read. i block alarm processing
67 * during the transfer to avoid sending unnecessary tickles. */
68 size_t dsi_write(DSI *dsi, void *buf, const size_t buflen)
72 if (((length = MIN(buflen, dsi->datasize)) > 0) &&
73 ((length = dsi_stream_read(dsi, buf, length)) > 0)) {
74 dsi->datasize -= length;
78 setitimer(ITIMER_REAL, &dsi->savetimer, NULL);
79 sigprocmask(SIG_UNBLOCK, &dsi->sigblockset, NULL);
83 /* flush any unread buffers. */
84 void dsi_writeflush(DSI *dsi)
88 while (dsi->datasize > 0) {
89 length = dsi_stream_read(dsi, dsi->data,
90 MIN(sizeof(dsi->data), dsi->datasize));
92 dsi->datasize -= length;
97 setitimer(ITIMER_REAL, &dsi->savetimer, NULL);
98 sigprocmask(SIG_UNBLOCK, &dsi->sigblockset, NULL);