2 * author: rofl0r (C) 2011-2013
3 * License: LGPL 2.1+ with static linking exception
7 #define _POSIX_C_SOURCE 200809L
12 #include "rocksock_internal.h"
14 #ifndef ROCKSOCK_FILENAME
15 #define ROCKSOCK_FILENAME __FILE__
18 // tries to read exactly one line, until '\n', then appends a '\0'
19 int rocksock_readline(rocksock
* sock
, char* buffer
, size_t bufsize
, size_t* bytesread
) {
20 // TODO: make more efficient by peeking into the buffer (Flag MSG_PEEK to recv), instead of reading byte by byte
21 // would need a different approach for ssl though.
22 if (!sock
) return RS_E_NULL
;
23 if (!buffer
|| !bufsize
|| !bytesread
)
24 return rocksock_seterror(sock
, RS_ET_OWN
, RS_E_NULL
,
25 ROCKSOCK_FILENAME
, __LINE__
);
27 size_t bytesread2
= 0;
30 while(*bytesread
< bufsize
) {
31 ret
= rocksock_recv(sock
, ptr
, 1, 1, &bytesread2
);
32 if(ret
|| !bytesread2
) return ret
;
33 *bytesread
+= bytesread2
;
34 if(ptr
> buffer
+ bufsize
)
36 if(*bytesread
> bufsize
) {
41 if(*bytesread
< bufsize
) {
42 buffer
[*bytesread
] = '\0';
49 return rocksock_seterror(sock
, RS_ET_OWN
, RS_E_OUT_OF_BUFFER
,
50 ROCKSOCK_FILENAME
, __LINE__
);