Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
string concatenation
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="nrk, post: 1701884"] I was introduced to them by someone who worked on *BSD platforms quite a bit, and ended up writing equivalents of my own. This being a boring afternoon, here's my implementation of strlcpy and strlcat. Comments most likely lifted from *BSD sources as I am always too lazy to write them myself. As everyone knows, my code *never* has any bugs :-) strlcpy.c: #include <stddef.h> /* * Copies src to string dst of size sz. sz is the full size of dst * including the NUL character. At most sz-1 characters will be * copied. Always NUL terminates dst, and doesn't fill dst with NULs * like strncpy when sz > strlen(src). src *MUST* be a valid NUL * terminated string. * * Returns strlen(src). * * If retval >= sz, truncation occurred. */ size_t strlcpy(char *dst, const char *src, size_t sz) { register char *d = dst; register const char *s = src; register size_t n = sz; if ( n ) { --n; while ( n && *s ) *d++ = *s++, --n; *d = 0; } while ( *s++ ); return s - src - 1; } strlcat.c: #include <stddef.h> /* * Appends src to string dst of size sz (unlike strncat, sz is the * full size of dst, not space left). At most sz-1 characters will be * copied. Always NUL terminates (unless sz <= strlen(dst)). * * Returns strlen(src) + MIN(sz, strlen(initial dst)). * * If retval >= sz, truncation occurred. */ size_t strlcat(char *dst, const char *src, size_t sz) { register char *d = dst; register const char *s = src; register size_t n = sz; if ( n ) { --n; while ( n && *d ) ++d, --n; if ( n ) { while ( n && *s ) *d++ = *s++, --n; *d = 0; } n = d - dst + (*d != 0); } src = s; while ( *s++ ) ; return n + (s - src - 1); } -nrk. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
string concatenation
Top