* Concatenates two strings with a maximum length. * * @param d The destination string. * @param s The source string. * @param n d will have at most n-1 characters (plus NUL) after invocation. * @return The total length of the concatenated string. */
| 161 | * @return The total length of the concatenated string. |
| 162 | */ |
| 163 | size_t strlcat(char *d, const char *s, size_t n) |
| 164 | { |
| 165 | size_t sl = strlen(s); |
| 166 | size_t dl = strlen(d); |
| 167 | |
| 168 | if (n <= dl + 1) |
| 169 | return sl + dl; |
| 170 | |
| 171 | char *p = d + dl; |
| 172 | size_t max = n > (sl + dl) ? sl : (n - dl - 1); |
| 173 | |
| 174 | for (size_t i = 0; i < max; i++) |
| 175 | p[i] = s[i]; |
| 176 | |
| 177 | p[max] = '\0'; |
| 178 | return sl + dl; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Find a character in a string. |