Read a line making sure that every char will not require more than 'timeout' * milliseconds to be read. * * On success the number of bytes read is returned, otherwise -1. * On success the string is always correctly terminated with a 0 byte. */
| 123 | * On success the number of bytes read is returned, otherwise -1. |
| 124 | * On success the string is always correctly terminated with a 0 byte. */ |
| 125 | ssize_t syncReadLine(int fd, char *ptr, ssize_t size, long long timeout) { |
| 126 | ssize_t nread = 0; |
| 127 | |
| 128 | size--; |
| 129 | while(size) { |
| 130 | char c; |
| 131 | |
| 132 | if (syncRead(fd,&c,1,timeout) == -1) return -1; |
| 133 | if (c == '\n') { |
| 134 | *ptr = '\0'; |
| 135 | if (nread && *(ptr-1) == '\r') *(ptr-1) = '\0'; |
| 136 | return nread; |
| 137 | } else { |
| 138 | *ptr++ = c; |
| 139 | *ptr = '\0'; |
| 140 | nread++; |
| 141 | } |
| 142 | size--; |
| 143 | } |
| 144 | return nread; |
| 145 | } |
no test coverage detected