Commits

Greg Ward committed 95cd6b3

common: change get_till_eol() to just copy nchars if there is no newline.
This is more consistent and much simpler to implement.

  • Participants
  • Parent commits 91f5e85

Comments (0)

Files changed (2)

File src/common.c

 }
 
 void
-get_till_eol(char *dest, const char *src, int n)
+get_till_eol(char *dest, const char *src, int nchars)
 {
     char *newline = strchr(src, '\n');
-    if (newline) {
-        int m = (newline - src < n ? newline - src : n);
-        strncpy(dest, src, m);
-        dest[m] = '\0';
-    }
-    else {
-        // make sure caller doesn't use an uninitialized string
-        dest[0] = '\0';
-    }
+    if (newline && newline - src < nchars)
+        nchars = newline - src;
+    strncpy(dest, src, nchars);
+    dest[nchars] = '\0';
 }

File src/common.h

 void
 dump_hex(const char* data, char* buf, int datasize);
 
-/* Copy up to n chars from src to dest, stopping at the first newline
- * and terminating dest with a NUL char.  It is guaranteed that dest
- * will not contain a newline and that strlen(dest) <= n.  If src
- * contains no newlines, dest will be an empty string.  Caller must
- * allocate n+1 chars for dest.
+/* Copy up to nchars chars from src to dest, stopping at the first
+ * newline and terminating dest with a NUL char.  On return, it is
+ * guaranteed that dest will not contain a newline and that strlen(dest)
+ * <= nchars.  Caller must allocate nchars+1 chars for dest.
  */
 void
-get_till_eol(char *dest, const char *src, int n);
+get_till_eol(char *dest, const char *src, int nchars);
 
 #endif