From ef603787544590b8c473d223bc05a2da93a2ea6e Mon Sep 17 00:00:00 2001 From: Nikos Ntarmos Date: Sat, 19 Apr 2008 22:24:23 +0300 Subject: [PATCH] [common] remove and getline and strndup usage There is no getline() on FreeBSD, nor any strndup(). Signed-off-by: Julien Danjou --- awesome-menu.c | 30 +++++++++++++++++++++++++++++- common/util.c | 21 +++++++++++++++++++++ common/util.h | 1 + configure.ac | 2 +- uicb.c | 7 ++----- 5 files changed, 54 insertions(+), 7 deletions(-) diff --git a/awesome-menu.c b/awesome-menu.c index 37b637792..15b86b2b5 100644 --- a/awesome-menu.c +++ b/awesome-menu.c @@ -19,7 +19,7 @@ * */ -/* getline(), asprintf() */ +/* asprintf() */ #define _GNU_SOURCE #define CHUNK_SIZE 4096 @@ -607,6 +607,34 @@ handle_kpress(XKeyEvent *e) } } +#ifndef HAVE_GETLINE +static int +getline(char ** buf, size_t* len, FILE* in) +{ + int i; + if (*buf) { + p_delete(buf); + (*buf) = NULL; + } + if (*len) + *len = 0; + + do { + p_realloc(buf, *len + 10); + (*len) += 10; + for (i = 0; i < 10 && !feof(in); i++) { + (*buf)[*len - 10 + i] = getchar(); + if ((*buf)[*len - 10 + i] == '\n' || + (*buf)[*len - 10 + i] == '\r') { + return (*len - 10 + i + 1); + } + } + } + while(!feof(in)); + return -1; +} +#endif + /** Fill the completion by reading on stdin. * \return true if something has been filled up, false otherwise. */ diff --git a/common/util.c b/common/util.c index 4b90201f9..c65dc3962 100644 --- a/common/util.c +++ b/common/util.c @@ -112,6 +112,27 @@ position_get_from_str(const char *pos) return Off; } +/** \brief safe limited strdup. + * + * Copies at most min(n-1, \c l) characters from \c src into a newly + * allocated buffer, always adding a final \c \\0, and returns that buffer. + * + * \param[in] src source string. + * \param[in] l maximum number of chars to copy. + * + * \return a newly allocated buffer containing the first \c l chars of \c src. +*/ +char* a_strndup(const char* src, ssize_t l) +{ + char* _tmpStr = p_new(char, l + 1); + if (_tmpStr) + { + a_strncpy(_tmpStr, l + 1, src, l); + _tmpStr[l] = 0; + } + return _tmpStr; +} + /** \brief safe limited strcpy. * * Copies at most min(n-1, \c l) characters from \c src into \c dst, diff --git a/common/util.h b/common/util.h index 9336bee63..d872b37f7 100644 --- a/common/util.h +++ b/common/util.h @@ -202,6 +202,7 @@ static inline int a_strncmp(const char *a, const char *b, ssize_t n) ssize_t a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l) __attribute__((nonnull(1))); ssize_t a_strcpy(char *dst, ssize_t n, const char *src) __attribute__((nonnull(1))); +char* a_strndup(const char *src, ssize_t l) __attribute__((nonnull(1))); /** \brief safe strcat. * diff --git a/configure.ac b/configure.ac index b729e91ad..0081f9aa0 100644 --- a/configure.ac +++ b/configure.ac @@ -146,7 +146,7 @@ AC_FUNC_REALLOC AC_FUNC_SELECT_ARGTYPES AC_TYPE_SIGNAL AC_FUNC_VPRINTF -AC_CHECK_FUNCS([memchr regcomp select setenv socket strchr strrchr strstr]) +AC_CHECK_FUNCS([getline memchr regcomp select setenv socket strchr strrchr strstr]) AC_CONFIG_FILES([Makefile]) AC_CONFIG_FILES([awesome.doxygen]) diff --git a/uicb.c b/uicb.c index 9704f49d9..67ad46bb0 100644 --- a/uicb.c +++ b/uicb.c @@ -23,9 +23,6 @@ * @defgroup ui_callback User Interface Callbacks */ -/* strndup() */ -#define _GNU_SOURCE - #include #include #include @@ -83,9 +80,9 @@ uicb_exec(int screen __attribute__ ((unused)), char *cmd) args = strchr(cmd, ' '); if(args) - path = strndup(cmd, args - cmd); + path = a_strndup(cmd, args - cmd); else - path = strndup(cmd, a_strlen(cmd)); + path = a_strndup(cmd, a_strlen(cmd)); execlp(path, cmd, NULL);