]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_write.c
Mege maste
[netatalk.git] / libatalk / dsi / dsi_write.c
1 /*
2  * $Id: dsi_write.c,v 1.5 2009-10-20 04:31:41 didg 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 #include <unistd.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/time.h>
20 #include <fcntl.h>
21 #include <string.h>
22
23 #include <atalk/dsi.h>
24 #include <netatalk/endian.h>
25
26 #ifndef MIN
27 #define MIN(a,b)     ((a) < (b) ? (a) : (b))
28 #endif /* ! MIN */
29
30 /* initialize relevant things for dsi_write. this returns the amount
31  * of data in the data buffer. the interface has been reworked to allow
32  * for arbitrary buffers. */
33 size_t dsi_writeinit(DSI *dsi, void *buf, const size_t buflen _U_)
34 {
35   size_t len, header;
36
37   /* figure out how much data we have. do a couple checks for 0 
38    * data */
39   header = ntohl(dsi->header.dsi_code);
40   dsi->datasize = header ? ntohl(dsi->header.dsi_len) - header : 0;
41   if (dsi->datasize > 0) {
42     len = MIN(sizeof(dsi->commands) - header, dsi->datasize);
43     
44     /* write last part of command buffer into buf */
45     memcpy(buf, dsi->commands + header, len);
46     
47     /* recalculate remaining data */
48     dsi->datasize -= len;
49   } else
50     len = 0;
51
52   return len;
53 }
54
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)
59 {
60   size_t length;
61
62   if (((length = MIN(buflen, dsi->datasize)) > 0) &&
63       ((length = dsi_stream_read(dsi, buf, length)) > 0)) {
64     dsi->datasize -= length;
65     return length;
66   }
67   return 0;
68 }
69
70 /* flush any unread buffers. */
71 void dsi_writeflush(DSI *dsi)
72 {
73   size_t length;
74
75   while (dsi->datasize > 0) { 
76     length = dsi_stream_read(dsi, dsi->data,
77                              MIN(sizeof(dsi->data), dsi->datasize));
78     if (length > 0)
79       dsi->datasize -= length;
80     else
81       break;
82   }
83 }