[lttng-dev] [Babeltrace RFC PATCH 18/28] Add MinGW implementation of strnlen

Jérémie Galarneau jeremie.galarneau at efficios.com
Mon May 13 16:20:37 EDT 2013


On Thu, May 2, 2013 at 8:00 AM, Ikaheimonen, JP
<jp_ikaheimonen at mentor.com> wrote:
> ---
>  compat/compat_strlib.c             | 23 +++++++++++++++++++++++
>  formats/ctf/types/string.c         |  2 +-
>  include/babeltrace/compat/string.h |  1 +
>  3 files changed, 25 insertions(+), 1 deletion(-)
>
> diff --git a/compat/compat_strlib.c b/compat/compat_strlib.c
> index 3cbd455..980a8f5 100644
> --- a/compat/compat_strlib.c
> +++ b/compat/compat_strlib.c
> @@ -50,6 +50,29 @@ int strerror_r(int errnum, char *buf, size_t buflen)
>         buf[buflen - 1] = '\0';
>         return 0;
>  }
> +
> +size_t strnlen(const char *str, size_t maxlen)
> +{

Any reason you are not using the native strnlen? I'm fairly sure it is
available on Windows.
I know MinGW does not provide some standard function prototypes (may
or may not be the case here)... I think finding the function's address
explicitly would be more appropriate here (using GetProcAddress on
msvcrt.dll).

> +       char * ptr;
> +       size_t len;
> +       ptr = (char *)str;
> +       len = 0;
> +
> +       /* while there still are characters in the string */
> +       while(*ptr != '\0') {
> +               /* increment counter */
> +               len++;
> +               /* check that we do not exceed the given maximum */
> +               if (len == maxlen) {
> +                       return len;
> +               }
> +               /* next character */
> +               ptr++;
> +
> +       }
> +       return len;
> +}
> +
>  #endif
>
>  int compat_strerror_r(int errnum, char *buf, size_t buflen)
> diff --git a/formats/ctf/types/string.c b/formats/ctf/types/string.c
> index a2433bf..a2f513d 100644
> --- a/formats/ctf/types/string.c
> +++ b/formats/ctf/types/string.c
> @@ -29,7 +29,7 @@
>  #include <babeltrace/babeltrace-internal.h>
>  #include <babeltrace/ctf/types.h>
>  #include <limits.h>            /* C99 limits */
> -#include <string.h>
> +#include <babeltrace/compat/string.h>
>
>  int ctf_string_read(struct bt_stream_pos *ppos, struct bt_definition *definition)
>  {
> diff --git a/include/babeltrace/compat/string.h b/include/babeltrace/compat/string.h
> index 501d003..82f411e 100644
> --- a/include/babeltrace/compat/string.h
> +++ b/include/babeltrace/compat/string.h
> @@ -5,6 +5,7 @@
>
>  #ifdef __MINGW32__
>  char *strtok_r(char *str, const char *delim, char **nextp);
> +size_t strnlen(const char *str, size_t maxlen);
>  #endif
>
>  int compat_strerror_r(int errnum, char *buf, size_t buflen);
> --
> 1.8.1.msysgit.1
>
>
> _______________________________________________
> lttng-dev mailing list
> lttng-dev at lists.lttng.org
> http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev



-- 
Jérémie Galarneau
EfficiOS Inc.
http://www.efficios.com



More information about the lttng-dev mailing list