- rlen = read(sock, &buf[len], bytes-len);
- if (rlen<1) {
- CtdlLogPrintf(CTDL_ERR, "sock_read() failed: %s\n",
- strerror(errno));
- return(-1);
- }
- len = len + rlen;
- if (!keep_reading_until_full) return(len);
+int CtdlSockGetLine(int *sock, StrBuf *Target)
+{
+ CitContext *CCC=CC;
+ const char *Error;
+ int rc;
+
+ rc = StrBufTCP_read_buffered_line_fast(Target,
+ CCC->sReadBuf,
+ &CCC->sPos,
+ sock,
+ 5,
+ 1,
+ &Error);
+ if ((rc < 0) && (Error != NULL))
+ CtdlLogPrintf(CTDL_CRIT,
+ "%s failed: %s\n",
+ __FUNCTION__,
+ Error);
+ return rc;
+}
+
+
+/*
+ * client_getln() ... Get a LF-terminated line of text from the client.
+ * (This is implemented in terms of client_read() and could be
+ * justifiably moved out of sysdep.c)
+ */
+int sock_getln(int *sock, char *buf, int bufsize)
+{
+ int i, retval;
+ CitContext *CCC=CC;
+ const char *pCh;
+
+ retval = CtdlSockGetLine(sock, CCC->sMigrateBuf);
+
+ i = StrLength(CCC->sMigrateBuf);
+ pCh = ChrPtr(CCC->sMigrateBuf);
+ /* Strip the trailing LF, and the trailing CR if present.
+ */
+ if (bufsize <= i)
+ i = bufsize - 1;
+ while ( (i > 0)
+ && ( (pCh[i - 1]==13)
+ || ( pCh[i - 1]==10)) ) {
+ i--;
+ }
+ memcpy(buf, pCh, i);
+ buf[i] = 0;
+
+ FlushStrBuf(CCC->sMigrateBuf);
+ if (retval < 0) {
+ safestrncpy(&buf[i], "000", bufsize - i);