To trim the leading spaces we are using strmove. But we were advised to use strlmove instead of strmove. I have read and used strlcpy and strlcat. Whether strlmove does the similar functionality and what all are its advantages?
Edit 1: Thank you Mike B and Chris Young. This is how we use strlcpy.
size_t strlcpy(char *dst, const char *src, size_t size) { strncpy(dst, src, size - 1); dst[size - 1] = '\0'; return(strlen(src)); }
So I was just thinking of using strlmove() also in the same way. I want to confirm whether any specifications is defined regarding the implementation of strlmove(). I know this is one of the best place i can ask.
Edit 2: strlmove() is implemented the same way as strlcpy() and strlcat() using memmove().
size_t strlmove(char *dst, const char *src, size_t size) { //Error if the size is 0 //If src length is greater than size; // memmove(dst, src, size-1) and dst[size] = \0; //Otherwise // memmove(dst, src, length(src)); return source len; }
Appreciate the help and support provided.
Thanks, Mathew Liju
As Chris Young mentions, these routines are not standard (or as far as I known in wide, common use) so I can’t be 100% certain with more specifics, but:
Typically the
strl()variations ofstr()routines take an additional parameter that indicates the size of the destination buffer. The routine guarantees that it will not write data past the end of the buffer (since it knows the size). Usuallystrl()functions will also guarantee that they will place a terminating null character at the end of the string or at the end of the buffer (potentially truncating any string created there) so that you are also guaranteed to have a terminated string that will be generally OK to pass to otherstr()functions. Note that if the length of the buffer is specified as 0 (zero) the function will not place a terminating null character (as there’s no room in the buffer for anything).In my opinion, it’s nearly always better to use a
strl()(orstrn()) function in preference to the correspondingstr()routine to prevent buffer overruns.