diff options
-rw-r--r-- | git-access.c | 1 | ||||
-rw-r--r-- | liquivision.c | 17 | ||||
-rw-r--r-- | save-xml.c | 5 | ||||
-rw-r--r-- | strndup.h | 21 |
4 files changed, 25 insertions, 19 deletions
diff --git a/git-access.c b/git-access.c index 521d552e9..ca0f3a161 100644 --- a/git-access.c +++ b/git-access.c @@ -12,6 +12,7 @@ #include "dive.h" #include "membuffer.h" +#include "strndup.h" /* * The libgit2 people are incompetent at making libraries. They randomly change diff --git a/liquivision.c b/liquivision.c index 6eefdc123..cabfa0b17 100644 --- a/liquivision.c +++ b/liquivision.c @@ -3,7 +3,7 @@ #include "dive.h" #include "divelist.h" #include "file.h" - +#include "strndup.h" // Convert bytes into an INT #define array_uint16_le(p) ((unsigned int) (p)[0] \ @@ -12,21 +12,6 @@ + ((p)[1]<<8) + ((p)[2]<<16) \ + ((p)[3]<<24)) -#if __WIN32__ -static char *strndup (const char *s, size_t n) -{ - char *cpy; - size_t len = strlen(s); - if (n < len) - len = n; - if ((cpy = malloc(len + 1)) != NULL) { - cpy[len] = '\0'; - memcpy(cpy, s, len); - } - return cpy; -} -#endif - struct lv_event { time_t time; struct pressure { diff --git a/save-xml.c b/save-xml.c index 15c667d35..173f6a8ec 100644 --- a/save-xml.c +++ b/save-xml.c @@ -10,6 +10,7 @@ #include "dive.h" #include "device.h" #include "membuffer.h" +#include "strndup.h" /* * We're outputting utf8 in xml. @@ -45,9 +46,7 @@ static void show_utf8(struct membuffer *b, const char *text, const char *pre, co return; while (len && isascii(text[len - 1]) && isspace(text[len - 1])) len--; - /* strndup would be easier, but that doesn't appear to exist on Windows / Mac */ - cleaned = strdup(text); - cleaned[len] = '\0'; + cleaned = strndup(text, len); put_string(b, pre); quote(b, cleaned, is_attribute); put_string(b, post); diff --git a/strndup.h b/strndup.h new file mode 100644 index 000000000..84e18b60f --- /dev/null +++ b/strndup.h @@ -0,0 +1,21 @@ +#ifndef STRNDUP_H +#define STRNDUP_H +#if __WIN32__ +static char *strndup (const char *s, size_t n) +{ + char *cpy; + size_t len = strlen(s); + if (n < len) + len = n; + if ((cpy = malloc(len + 1)) != + NULL) { + cpy[len] = + '\0'; + memcpy(cpy, + s, + len); + } + return cpy; +} +#endif +#endif /* STRNDUP_H */ |