-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathc_blockrw.c
134 lines (125 loc) · 3.31 KB
/
c_blockrw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/************************************************************************
* (C)opyright 1991-1999 BinTec Communications AG, All Rights Reserved
*
* Title: <one line description>
* Author: <username>
* $RCSfile: c_blockrw.c,v $
* $Revision: 56 $
* $Date: 2005-11-21 20:44:57 +0100 (Mon, 21 Nov 2005) $
* $State: Exp $
*
* Type: streams driver/module | application | library
* Products: ALL | XS,XM,XL,XP,BGO,BGP,XCM
* Interfaces: IPI, DLPI
* Libraries: -
* Switches: -
* Description: --
*-----------------------------------------------------------------------
* Current Log:
* -
***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>
#include "capidef.h"
#include "libcapi.h"
#include <unistd.h>
#if 1 /* print error */
static int c_blockrw_tracelevel = 1;
# define cTRACE(l, c) if ((l) <= c_blockrw_tracelevel) { c; }
#else
# define cTRACE(l, c) /* do nothing */
#endif
/****************************************************************************
*
* capi_blockread()
*
****************************************************************************/
size_t capi_blockread(int fd,
VOIDPTR block,
size_t size)
{
char *ptr = block;
unsigned long len;
while (size > 0) {
len = read( fd, ptr, (size_t)size);
if(len > 0){
size -= len;
ptr += len;
continue;
}
else if(len == 0){ /* got a EOF */
cTRACE( 1, fprintf( stderr, "capi_blockread <<EOF>>\n" ));
break;
}
/* if((len < 0) && (errno == EINTR)) */
if(errno == EINTR){
continue;
}
/* errno is only set if systemcall returns -1 */
cTRACE( 1, perror("capi_blockread"));
/* this is wrong */
cTRACE( 1, fprintf( stderr, "capi_blockread len=%ld size=%d waitfor=%lu\n",
len, ptr-(char*)block, (unsigned long)size));
return( -1 );
}
return (ptr-(char*)block);
}
/****************************************************************************
*
* capi_blockwrite()
*
****************************************************************************/
size_t capi_blockwrite(int fd,
VOIDPTR block,
size_t size)
{
char *ptr = block;
unsigned long len;
fd_set fds;
struct timeval tv;
while (size > 0) {
do {
again:
FD_ZERO(&fds);
FD_SET(fd, &fds);
tv.tv_sec = 10;
tv.tv_usec = 0;
switch(select(fd+1, NULL, &fds, NULL, &tv)) {
case 0:
cTRACE(0, fprintf(stderr, "select: timeout\n"));
return -1;
case -1:
cTRACE( 0, perror("select"));
if(errno == EINTR) goto again;
return -1;
}
errno = 0;
len = write( fd, ptr, size);
/*
* if (errno == EINTR) cTRACE( 0, perror("write:EINTR"));
*/
} while((len <= 0) && (errno == EINTR));
if (len > 0 ) {
size -= len;
ptr += len;
continue;
}
if (len <= 0) {
cTRACE( 0, perror("blockwrite"));
if(len==0){
cTRACE( 0, fprintf( stderr, "blockwrite <<EOF>>\n"));
}
else {
/* again wrong! */
cTRACE( 0, fprintf( stderr,
"blockwrite len=%ld size=%d waitfor=%lu\n",
len, ptr-(char*)block, (unsigned long)size));
}
return -1;
}
}
return (ptr-(char*)block);
}