Menu

[r851]: / branches / Version-1 / php-java-bridge / sio.c  Maximize  Restore  History

Download this file

56 lines (44 with data), 1.1 kB

 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
#include "sio.h"
#ifdef HAVE_BROKEN_STDIO
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
/* fix/workaround for broken stdio implementations */
size_t sfwrite(const void *ptr, size_t size, size_t nmemb, SFILE *stream) {
size_t c=0, s, n;
//assert(nmemb>0);
while(nmemb--) {
s = n = 0;
while((size>s)&&((n=send(stream->file, ptr+s+c, size-s, 0)) > 0)) s+=n;
c += s;
if(n == -1) { stream->eof = 1; break; }
}
return c/size;
}
size_t sfread(void *ptr, size_t size, size_t nmemb, SFILE *stream) {
size_t c=0, s, n;
//assert(nmemb>0);
while(nmemb--) {
s = n = 0;
while((size>s)&&((n=recv(stream->file, ptr+s+c, size-s, 0)) > 0)) s+=n;
c += s;
if(n == -1) { stream->eof = 1; break; }
}
return c/size;
}
SFILE* sfdopen(int fd, char*flags) {
SFILE*f = calloc(1, sizeof*f);
if(f)
f->file = fd;
else
{ f->err=1; errno=ENOMEM; }
return f;
}
int sfclose(SFILE *f) {
int r = close(f->file);
free(f);
return r==-1?EOF:0;
}
#endif
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.