+
+/*
+ A strncpy() replacement that ensures the resulting dest buffer has
+ a zero (nil) terminator even if the source is longer than the dest
+ length.
+ A max of len-1 bytes are copied from src to dest. The copy stops when
+ a zero (nil) is encountered in the src, or len-1 bytes are copied.
+ The string is always nil terminated.
+ The string length is returned.
+
+ It is the responsiblity of the caller to ensure that dest is at
+ least len bytes in length.
+
+ If either src/dest is invalid (nil) a value of -1 is returned.
+*/
+static inline int zt_buf_fill( char* dest, char const* src, int len ) {
+ char* dp;
+ char const* sp;
+ int n; // num moved
+
+ if( dest == NULL || src == NULL ) {
+ return -1;
+ }
+
+ dp = dest;
+ sp = src;
+ n = 0;
+ while( *sp && n < len-1 ) {
+ *(dp++) = *(sp++);
+ n++;
+ }
+
+ *dp = 0;
+ return n;
+}
+