All pastes #94305 Raw Edit

Anonymous

public text v1 · immutable
#94305 ·published 2006-07-21 04:08 UTC
rendered paste body
// sock is a fd from socket() buffer is a char* area allready made and size is the max bytes to put in there
int recvline(unsigned int sock,char* buffer,unsigned int size) {
        char buffer2[size];
        unsigned int bytesread;
        bytesread=recv(sock,buffer,size-2,MSG_PEEK);
        buffer[bytesread]='\0';
        if (bytesread==0){size=0;return -2;}
#ifdef recvlinedebug
        printf("%d bytes of text '%s' read in peek mode searching for \\r \\n or \\0\n",bytesread,buffer);
#endif
        for(unsigned int x=0;x<bytesread;x++){
                if ((buffer[x]=='\n')||(buffer[x]=='\r')||(buffer[x]=='\0')){
                        if ((buffer[x]=='\r')&&(buffer[x+1]=='\n')){x++;}
                        x++;
                        bytesread=recv(sock,buffer,x,0);
                        buffer[x]='\0';
                        removecrlf(buffer,buffer2,size);
                        strcpy(buffer,buffer2);
                        return bytesread;
                }
        }
        size=bytesread;
        return -1;
}