* Duplicate a string with a max length of size * * @param s The string to duplicate. * @param size The max length of the string * @return A pointer to the copy of the original string. */
| 245 | * @return A pointer to the copy of the original string. |
| 246 | */ |
| 247 | char *strndup(const char *s, size_t size) |
| 248 | { |
| 249 | size_t n = strnlen(s, size); |
| 250 | char *p = malloc(n + 1); |
| 251 | |
| 252 | if (p != NULL) { |
| 253 | strncpy(p, s, n); |
| 254 | p[n] = 0; |
| 255 | } |
| 256 | return p; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Find a substring within a string. |