summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Packard <keithp@keithp.com>2010-07-26 18:10:07 -0700
committerKeith Packard <keithp@keithp.com>2010-07-26 18:10:07 -0700
commit0a782026f6b19e84ffd44f1ae1b466363474bd30 (patch)
tree514b3d2ae0289b875c298e44a1817eb2107688ba
parentb51497597868a40df039dd3ca11b35a6258bbbb3 (diff)
Darwin doesn't have strndup.
This provides a private version of this GNU extension. Signed-off-by: Keith Packard <keithp@keithp.com>
-rw-r--r--ao-tools/libaltos/libaltos.c22
1 files changed, 21 insertions, 1 deletions
diff --git a/ao-tools/libaltos/libaltos.c b/ao-tools/libaltos/libaltos.c
index 7d471f38..df0d5b2e 100644
--- a/ao-tools/libaltos/libaltos.c
+++ b/ao-tools/libaltos/libaltos.c
@@ -40,6 +40,26 @@ match_dev(char *product, int serial, struct altos_device *device)
return i;
}
+#ifdef DARWIN
+/* Mac OS X don't have strndup even if _GNU_SOURCE is defined */
+static char *
+altos_strndup (const char *s, size_t n)
+{
+ size_t len = strlen (s);
+ char *ret;
+
+ if (len <= n)
+ return strdup (s);
+ ret = malloc(n + 1);
+ strncpy(ret, s, n);
+ ret[n] = '\0';
+ return ret;
+}
+
+#else
+#define altos_strndup strndup
+#endif
+
int
altos_find_by_arg(char *arg, char *default_product, struct altos_device *device)
{
@@ -61,7 +81,7 @@ altos_find_by_arg(char *arg, char *default_product, struct altos_device *device)
/* check for <product>:<serial> */
colon = strchr(arg, ':');
if (colon) {
- product = strndup(arg, colon - arg);
+ product = altos_strndup(arg, colon - arg);
serial = strtol(colon + 1, &end, 0);
if (*end != '\0')
return 0;