]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_write.c
massive commenting/autoconf changes
[netatalk.git] / libatalk / dsi / dsi_write.c
1 /*
2  * $Id: dsi_write.c,v 1.3 2001-06-29 14:14:46 rufustfirefly Exp $
3  *
4  * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
5  * All rights reserved. See COPYRIGHT.
6  *
7  * 7 Oct 1997 added checks for 0 data.
8  */
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif /* HAVE_CONFIG_H */
13
14 /* this streams writes */
15 #include <stdio.h>
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif /* HAVE_UNISTD_H */
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/time.h>
22 #ifdef HAVE_FCNTL_H
23 #include <fcntl.h>
24 #endif /* HAVE_FCNTL_H */
25 #include <string.h>
26
27 #include <atalk/dsi.h>
28 #include <netatalk/endian.h>
29
30 #ifndef MIN
31 #define MIN(a,b)     ((a) < (b) ? (a) : (b))
32 #endif /* ! MIN */
33
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)
38 {
39   const struct itimerval none = {{0, 0}, {0, 0}};
40   size_t len, header;
41
42   /* figure out how much data we have. do a couple checks for 0 
43    * data */
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);
48     
49     /* write last part of command buffer into buf */
50     memcpy(buf, dsi->commands + header, len);
51     
52     /* recalculate remaining data */
53     dsi->datasize -= len;
54   } else
55     len = 0;
56
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
59    * reason, needed. */
60   sigprocmask(SIG_BLOCK, &dsi->sigblockset, NULL);
61   setitimer(ITIMER_REAL, &none, &dsi->savetimer);
62   return len;
63 }
64
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)
69 {
70   size_t length;
71
72   if (((length = MIN(buflen, dsi->datasize)) > 0) &&
73       ((length = dsi_stream_read(dsi, buf, length)) > 0)) {
74     dsi->datasize -= length;
75     return length;
76   }
77
78   setitimer(ITIMER_REAL, &dsi->savetimer, NULL);
79   sigprocmask(SIG_UNBLOCK, &dsi->sigblockset, NULL);
80   return 0;
81 }
82
83 /* flush any unread buffers. */
84 void dsi_writeflush(DSI *dsi)
85 {
86   size_t length;
87
88   while (dsi->datasize > 0) { 
89     length = dsi_stream_read(dsi, dsi->data,
90                              MIN(sizeof(dsi->data), dsi->datasize));
91     if (length > 0)
92       dsi->datasize -= length;
93     else
94       break;
95   }
96
97   setitimer(ITIMER_REAL, &dsi->savetimer, NULL);
98   sigprocmask(SIG_UNBLOCK, &dsi->sigblockset, NULL);
99 }