]> arthur.barton.de Git - netatalk.git/blob - libatalk/dsi/dsi_write.c
Merge remote branch 'origin/product-2-2' into develop
[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
25 #ifndef MIN
26 #define MIN(a,b)     ((a) < (b) ? (a) : (b))
27 #endif /* ! MIN */
28
29 /* initialize relevant things for dsi_write. this returns the amount
30  * of data in the data buffer. the interface has been reworked to allow
31  * for arbitrary buffers. */
32 size_t dsi_writeinit(DSI *dsi, void *buf, const size_t buflen _U_)
33 {
34   size_t len, header;
35
36   /* figure out how much data we have. do a couple checks for 0 
37    * data */
38   header = ntohl(dsi->header.dsi_code);
39   dsi->datasize = header ? ntohl(dsi->header.dsi_len) - header : 0;
40   if (dsi->datasize > 0) {
41     len = MIN(sizeof(dsi->commands) - header, dsi->datasize);
42     
43     /* write last part of command buffer into buf */
44     memcpy(buf, dsi->commands + header, len);
45     
46     /* recalculate remaining data */
47     dsi->datasize -= len;
48   } else
49     len = 0;
50
51   return len;
52 }
53
54 /* fill up buf and then return. this should be called repeatedly
55  * until all the data has been read. i block alarm processing 
56  * during the transfer to avoid sending unnecessary tickles. */
57 size_t dsi_write(DSI *dsi, void *buf, const size_t buflen)
58 {
59   size_t length;
60
61   if (((length = MIN(buflen, dsi->datasize)) > 0) &&
62       ((length = dsi_stream_read(dsi, buf, length)) > 0)) {
63     dsi->datasize -= length;
64     return length;
65   }
66   return 0;
67 }
68
69 /* flush any unread buffers. */
70 void dsi_writeflush(DSI *dsi)
71 {
72   size_t length;
73
74   while (dsi->datasize > 0) { 
75     length = dsi_stream_read(dsi, dsi->data,
76                              MIN(sizeof(dsi->data), dsi->datasize));
77     if (length > 0)
78       dsi->datasize -= length;
79     else
80       break;
81   }
82 }