Skip to content

Commit

Permalink
Altough strndup is POSIX.1-2008; Windows does not provide it
Browse files Browse the repository at this point in the history
Add a backup function for Windows machines.

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz authored and pcercuei committed Feb 17, 2022
1 parent 60c33c4 commit 77a3f61
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ void iio_channel_init_finalize(struct iio_channel *chn);
unsigned int find_channel_modifier(const char *s, size_t *len_p);

char *iio_strdup(const char *str);
char *iio_strndup(const char *str, size_t n);
size_t iio_strlcpy(char * __restrict dst, const char * __restrict src, size_t dsize);
char * iio_getenv (char * envvar);

Expand Down
18 changes: 18 additions & 0 deletions utilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,24 @@ char *iio_strdup(const char *str)
#endif
}

/* strndup conforms to POSIX.1-2008; but Windows does not provided it
*/
char *iio_strndup(const char *str, size_t n)
{
#ifdef HAS_STRNDUP
return strndup(str, n);
#else
size_t len = strnlen(str, n + 1);
char *buf = malloc(len + 1);
if (buf) {
/* len = size of buf, so memcpy is OK */
memcpy(buf, str, len); /* Flawfinder: ignore */
buf[len] = 0;
}
return buf;
#endif
}

/* strlcpy is designed to be safer, more consistent, and less error prone
* replacements for strncpy, since it guarantees to NUL-terminate the result.
*
Expand Down

0 comments on commit 77a3f61

Please sign in to comment.