From 9eda7fb6fb7f33e13d4e98a203276a324acd967c Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sun, 23 Oct 2011 18:02:01 +0100 Subject: [PATCH] move strndup to libi3 --- i3-config-wizard/main.c | 24 ------------------------ i3bar/src/xcb.c | 23 ----------------------- include/libi3.h | 12 ++++++++++++ include/util.h | 12 ------------ libi3/strndup.c | 37 +++++++++++++++++++++++++++++++++++++ src/util.c | 24 ------------------------ 6 files changed, 49 insertions(+), 83 deletions(-) create mode 100644 libi3/strndup.c diff --git a/i3-config-wizard/main.c b/i3-config-wizard/main.c index df0db1fc..09320dfb 100644 --- a/i3-config-wizard/main.c +++ b/i3-config-wizard/main.c @@ -74,30 +74,6 @@ Display *dpy; char *rewrite_binding(const char *bindingline); static void finish(); -#if defined(__APPLE__) - -/* - * Taken from FreeBSD - * Returns a pointer to a new string which is a duplicate of the - * string, but only copies at most n characters. - * - */ -char *strndup(const char *str, size_t n) { - size_t len; - char *copy; - - for (len = 0; len < n && str[len]; len++) - continue; - - if ((copy = malloc(len + 1)) == NULL) - return (NULL); - memcpy(copy, str, len); - copy[len] = '\0'; - return (copy); -} - -#endif - /* * This function resolves ~ in pathnames. * It may resolve wildcards in the first part of the path, but if no match diff --git a/i3bar/src/xcb.c b/i3bar/src/xcb.c index 40d19a3e..01caeeea 100644 --- a/i3bar/src/xcb.c +++ b/i3bar/src/xcb.c @@ -36,29 +36,6 @@ #include "common.h" #include "libi3.h" -#if defined(__APPLE__) - -/* - * Taken from FreeBSD - * Returns a pointer to a new string which is a duplicate of the - * string, but only copies at most n characters. - * - */ -char *strndup(const char *str, size_t n) { - size_t len; - char *copy; - - for (len = 0; len < n && str[len]; len++) - continue; - - copy = smalloc(len + 1); - memcpy(copy, str, len); - copy[len] = '\0'; - return (copy); -} - -#endif - /* We save the Atoms in an easy to access array, indexed by an enum */ enum { #define ATOM_DO(name) name, diff --git a/include/libi3.h b/include/libi3.h index 797ba424..f4cd2160 100644 --- a/include/libi3.h +++ b/include/libi3.h @@ -109,4 +109,16 @@ void fake_configure_notify(xcb_connection_t *conn, xcb_rectangle_t r, xcb_window */ uint32_t get_colorpixel(const char *hex) __attribute__((const)); +#if defined(__APPLE__) + +/* + * Taken from FreeBSD + * Returns a pointer to a new string which is a duplicate of the + * string, but only copies at most n characters. + * + */ +char *strndup(const char *str, size_t n); + +#endif + #endif diff --git a/include/util.h b/include/util.h index efef0bd0..68009442 100644 --- a/include/util.h +++ b/include/util.h @@ -139,16 +139,4 @@ void *memmem(const void *l, size_t l_len, const void *s, size_t s_len); #endif -#if defined(__APPLE__) - -/* - * Taken from FreeBSD - * Returns a pointer to a new string which is a duplicate of the - * string, but only copies at most n characters. - * - */ -char *strndup(const char *str, size_t n); - -#endif - #endif diff --git a/libi3/strndup.c b/libi3/strndup.c new file mode 100644 index 00000000..f02e0af5 --- /dev/null +++ b/libi3/strndup.c @@ -0,0 +1,37 @@ +/* + * vim:ts=4:sw=4:expandtab + * + * i3 - an improved dynamic tiling window manager + * + * © 2009-2011 Michael Stapelberg and contributors + * + * See file LICENSE for license information. + * + */ +#include +#include + +#include "libi3.h" + +#if defined(__APPLE__) + +/* + * Taken from FreeBSD + * Returns a pointer to a new string which is a duplicate of the + * string, but only copies at most n characters. + * + */ +char *strndup(const char *str, size_t n) { + size_t len; + char *copy; + + for (len = 0; len < n && str[len]; len++) + continue; + + copy = smalloc(len + 1); + memcpy(copy, str, len); + copy[len] = '\0'; + return (copy); +} + +#endif diff --git a/src/util.c b/src/util.c index fa50d2e8..f894eb3f 100644 --- a/src/util.c +++ b/src/util.c @@ -415,27 +415,3 @@ void *memmem(const void *l, size_t l_len, const void *s, size_t s_len) { } #endif - -#if defined(__APPLE__) - -/* - * Taken from FreeBSD - * Returns a pointer to a new string which is a duplicate of the - * string, but only copies at most n characters. - * - */ -char *strndup(const char *str, size_t n) { - size_t len; - char *copy; - - for (len = 0; len < n && str[len]; len++) - continue; - - if ((copy = malloc(len + 1)) == NULL) - return (NULL); - memcpy(copy, str, len); - copy[len] = '\0'; - return (copy); -} - -#endif -- 2.39.5