OSDN Git Service

addid library source 20140221
[zither/ds-zither.git] / sources / lib / libms / socket_reada.c
1
2 #include<stdio.h>
3 #include<fcntl.h>
4 #include <errno.h>
5 #include <unistd.h>
6 #include <string.h>
7
8 #include "almemsys.h"
9
10 int socket_reada(int sk, char * readdata, int max){
11 int ret, stat, count;
12 char cha[64];
13
14 memset(cha, 0x00, 64);
15
16 ret = 0;
17 stat = 0;
18 count = 0;
19
20 while(1){
21
22    stat = read(sk, cha, 63);
23
24    if(stat <= 0){
25       if(readdata[ret-1] == '\r' || readdata[ret-1] == '\n'){
26          break;
27          }
28       else{
29          if(count > 2){
30             break;
31             }
32          count++;
33          }
34       }
35    else{
36       count = 0;
37       strncat(readdata, cha, stat);
38
39       memset(cha, 0x00, 64);
40       ret = ret + stat;
41       if(ret > max - 63){
42          fprintf(stderr, "socket_reada(): read size err. max=%d\n", max);
43          break;
44          }
45       }
46    }
47
48 return(ret);
49 }
50
51
52