From c5382e110dbe2fdeb470aea2c3c92437d4778553 Mon Sep 17 00:00:00 2001 From: Marius Tolzmann Date: Tue, 28 Jul 2015 01:10:49 +0200 Subject: [PATCH] mx_util: Add mx_strvec_() and remove mxq_util --- .gitignore | 1 - Makefile | 21 +----- mx_util.c | 191 +++++++++++++++++++++++++++++++++++++++++++++++ mx_util.h | 9 +++ mxq_util.c | 215 ----------------------------------------------------- mxq_util.h | 17 ----- mxqd.c | 3 +- mxqdump.c | 2 +- mxqsub.c | 4 +- test.c | 2 - 10 files changed, 205 insertions(+), 260 deletions(-) delete mode 100644 mxq_util.c delete mode 100644 mxq_util.h diff --git a/.gitignore b/.gitignore index d9ac12bb..27915310 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,6 @@ mxq_job.o mxqd.o mxqsub.o mxqkill.o -mxq_util.o test_mx_util.o test_mx_log.o test_mx_mysql.o diff --git a/Makefile b/Makefile index 818f82dd..c926ac18 100644 --- a/Makefile +++ b/Makefile @@ -184,11 +184,6 @@ mx_mysql.h += $(mx_util.h) mx_mxq.h += mx_mxq.h -### mxq_util.h --------------------------------------------------------- - -mxq_util.h += mxq_util.h -mxq_util.h += $(mx_log.h) - ### mxq_group.h -------------------------------------------------------- mxq_group.h += mxq_group.h @@ -250,7 +245,7 @@ clean: CLEAN += mxq_log.o ### mxqdump.o --------------------------------------------------- mxqdump.o: $(mx_log.h) -mxqdump.o: $(mxq_util.h) +mxqdump.o: $(mx_util.h) mxqdump.o: $(mx_mysql.h) mxqdump.o: $(mx_getopt.h) mxqdump.o: CFLAGS += $(CFLAGS_MYSQL) @@ -270,15 +265,6 @@ mxqkill.o: CFLAGS += $(CFLAGS_MYSQL) clean: CLEAN += mxqkill.o -### mxq_util.o --------------------------------------------------------- - -mxq_util.o: $(mx_log.h) -mxq_util.o: $(mxq_util.h) -mxq_util.o: CFLAGS += $(CFLAGS_MYSQL) -mxq_util.o: CFLAGS += -Wno-unused-variable - -clean: CLEAN += mxq_util.o - ### mxq_group.o -------------------------------------------------------- mxq_group.o: $(mx_log.h) @@ -324,7 +310,7 @@ mxqsub.o: $(mx_mysql.h) mxqsub.o: $(mxq.h) mxqsub.o: $(mxq_group.h) mxqsub.o: $(mxq_job.h) -mxqsub.o: $(mxq_util.h) +mxqsub.o: $(mx_util.h) mxqsub.o: CFLAGS += $(CFLAGS_MYSQL) clean: CLEAN += mxqsub.o @@ -340,7 +326,6 @@ mxqd: mxq_log.o mxqd: mx_getopt.o mxqd: mxq_group.o mxqd: mxq_job.o -mxqd: mxq_util.o mxqd: mx_mysql.o mxqd: LDLIBS += $(LDLIBS_MYSQL) @@ -354,7 +339,6 @@ install:: mxqd ### mxqsub ------------------------------------------------------------ mxqsub: mx_getopt.o -mxqsub: mxq_util.o mxqsub: mx_util.o mxqsub: mx_log.o mxqsub: mx_mysql.o @@ -373,7 +357,6 @@ mxqdump: mx_log.o mxqdump: mx_mysql.o mxqdump: mxq_group.o mxqdump: mxq_job.o -mxqdump: mxq_util.o mxqdump: mx_util.o mxqdump: mx_getopt.o mxqdump: LDLIBS += $(LDLIBS_MYSQL) diff --git a/mx_util.c b/mx_util.c index a9b55465..b6f404b3 100644 --- a/mx_util.c +++ b/mx_util.c @@ -633,3 +633,194 @@ void *mx_calloc_forever_sec(size_t nmemb, size_t size, unsigned int time) return ptr; } + +char **mx_strvec_new(void) +{ + char **strvec; + + strvec = calloc(sizeof(*strvec), 1); + + return strvec; +} + +static inline size_t mx_strvec_length_cache(char **strvec, int32_t len) +{ + static char ** sv = NULL; + static size_t l = 0; + + if (likely(len == -1)) { + if (likely(sv == strvec)) { + return l; + } + return -1; + } + + if (likely(sv == strvec)) { + l = len; + } else { + sv = strvec; + l = len; + } + return l; +} + +size_t mx_strvec_length(char ** strvec) +{ + char ** sv; + size_t len; + + assert(strvec); + + sv = strvec; + + len = mx_strvec_length_cache(sv, -1); + if (len != -1) + return len; + + for (; *sv; sv++); + + len = sv-strvec; + mx_strvec_length_cache(sv, len); + return len; +} + +int mx_strvec_push_str(char *** strvecp, char * str) +{ + char ** sv; + + size_t len; + + assert(strvecp); + assert(*strvecp); + assert(str); + + len = mx_strvec_length(*strvecp); + + sv = realloc(*strvecp, sizeof(**strvecp) * (len + 2)); + if (!sv) { + return 0; + } + + sv[len++] = str; + sv[len] = NULL; + + mx_strvec_length_cache(sv, len); + + *strvecp = sv; + + return 1; +} + +int mx_strvec_push_strvec(char ***strvecp, char **strvec) +{ + char **sv; + + size_t len1; + size_t len2; + + assert(strvecp); + assert(*strvecp); + assert(strvec); + + len1 = mx_strvec_length(*strvecp); + len2 = mx_strvec_length(strvec); + + sv = realloc(*strvecp, sizeof(**strvecp) * (len1 + len2 + 1)); + if (!sv) { + return 0; + } + + memcpy(sv+len1, strvec, sizeof(*strvec) * (len2 + 1)); + + mx_strvec_length_cache(sv, len1+len2); + + *strvecp = sv; + + return 1; +} + +char *mx_strvec_to_str(char **strvec) +{ + char **sv; + char* buf; + char* s; + size_t totallen; + char* str; + + assert(strvec); + + totallen = 0; + for (sv = strvec; *sv; sv++) { + totallen += strlen(*sv); + } + + buf = malloc(sizeof(*buf) * (totallen * 2 + 2) + 1); + if (!buf) + return NULL; + + str = buf; + *str = 0; + + for (sv = strvec; *sv; sv++) { + for (s=*sv; *s; s++) { + switch (*s) { + case '\\': + *(str++) = '\\'; + *(str++) = '\\'; + break; + + default: + *(str++) = *s; + } + } + *(str++) = '\\'; + *(str++) = '0'; + } + + *str = '\0'; + + return buf; +} + +void mx_strvec_free(char **strvec) +{ + char **sv; + + if (!strvec) + return; + + for (sv = strvec; *sv; sv++) { + free(sv); + } + free(strvec); +} + +char **mx_strvec_from_str(char *str) +{ + int res; + char* s; + char* p; + char** strvec; + + strvec = mx_strvec_new(); + if (!strvec) + return NULL; + + for (s=str; *s; s=p+2) { + p = strstr(s, "\\0"); + if (!p) { + free(strvec); + errno = EINVAL; + return NULL; + } + *p = 0; + + res = mx_strvec_push_str(&strvec, s); + if (!res) { + free(strvec); + return NULL; + } + } + + return strvec; +} diff --git a/mx_util.h b/mx_util.h index 9a48b908..227007bc 100644 --- a/mx_util.h +++ b/mx_util.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "mx_log.h" @@ -111,4 +112,12 @@ int mx_sleep_nofail(unsigned int seconds); void *mx_calloc_forever_sec(size_t nmemb, size_t size, unsigned int time); +char** mx_strvec_new(void); +size_t mx_strvec_length(char **strvec); +int mx_strvec_push_str(char ***strvecp, char *str); +int mx_strvec_push_strvec(char*** strvecp, char **strvec); +char* mx_strvec_to_str(char **strvec); +char** mx_strvec_from_str(char *str); +void mx_strvec_free(char **strvec); + #endif diff --git a/mxq_util.c b/mxq_util.c deleted file mode 100644 index 837537a0..00000000 --- a/mxq_util.c +++ /dev/null @@ -1,215 +0,0 @@ - -#define _GNU_SOURCE - -#include -#include -#include - -#include -#include - -#include - -#include -#include - -#include -#include "mx_log.h" -#include "mx_util.h" - -#include "mxq_util.h" - -char **strvec_new(void) -{ - char **strvec; - - strvec = calloc(sizeof(*strvec), 1); - - return strvec; -} - -static inline size_t strvec_length_cache(char **strvec, int32_t len) -{ - static char ** sv = NULL; - static size_t l = 0; - - if (likely(len == -1)) { - if (likely(sv == strvec)) { - return l; - } - return -1; - } - - if (likely(sv == strvec)) { - l = len; - } else { - sv = strvec; - l = len; - } - return l; -} - -size_t strvec_length(char ** strvec) -{ - char ** sv; - size_t len; - - assert(strvec); - - sv = strvec; - - len = strvec_length_cache(sv, -1); - if (len != -1) - return len; - - for (; *sv; sv++); - - len = sv-strvec; - strvec_length_cache(sv, len); - return len; -} - -int strvec_push_str(char *** strvecp, char * str) -{ - char ** sv; - - size_t len; - - assert(strvecp); - assert(*strvecp); - assert(str); - - len = strvec_length(*strvecp); - - sv = realloc(*strvecp, sizeof(**strvecp) * (len + 2)); - if (!sv) { - return 0; - } - - sv[len++] = str; - sv[len] = NULL; - - strvec_length_cache(sv, len); - - *strvecp = sv; - - return 1; -} - -int strvec_push_strvec(char ***strvecp, char **strvec) -{ - char **sv; - - size_t len1; - size_t len2; - - assert(strvecp); - assert(*strvecp); - assert(strvec); - - len1 = strvec_length(*strvecp); - len2 = strvec_length(strvec); - - sv = realloc(*strvecp, sizeof(**strvecp) * (len1 + len2 + 1)); - if (!sv) { - return 0; - } - - memcpy(sv+len1, strvec, sizeof(*strvec) * (len2 + 1)); - - strvec_length_cache(sv, len1+len2); - - *strvecp = sv; - - return 1; -} - -char *strvec_to_str(char **strvec) -{ - char **sv; - char* buf; - char* s; - size_t totallen; - char* str; - - assert(strvec); - - totallen = 0; - for (sv = strvec; *sv; sv++) { - totallen += strlen(*sv); - } - - buf = malloc(sizeof(*buf) * (totallen * 2 + 2) + 1); - if (!buf) - return NULL; - - str = buf; - *str = 0; - - for (sv = strvec; *sv; sv++) { - for (s=*sv; *s; s++) { - switch (*s) { - case '\\': - *(str++) = '\\'; - *(str++) = '\\'; - break; - - default: - *(str++) = *s; - } - } - *(str++) = '\\'; - *(str++) = '0'; - } - - *str = '\0'; - - return buf; -} - -void strvec_free(char **strvec) -{ - char **sv; - char* buf; - char* s; - size_t totallen; - char* str; - - if (!strvec) - return; - - for (sv = strvec; *sv; sv++) { - free(sv); - } - free(strvec); -} - -char **strvec_from_str(char *str) -{ - int res; - char* s; - char* p; - char** strvec; - - strvec = strvec_new(); - if (!strvec) - return NULL; - - for (s=str; *s; s=p+2) { - p = strstr(s, "\\0"); - if (!p) { - free(strvec); - errno = EINVAL; - return NULL; - } - *p = 0; - - res = strvec_push_str(&strvec, s); - if (!res) { - free(strvec); - return NULL; - } - } - - return strvec; -} diff --git a/mxq_util.h b/mxq_util.h deleted file mode 100644 index 8ddbfdda..00000000 --- a/mxq_util.h +++ /dev/null @@ -1,17 +0,0 @@ - -#ifndef __MXQ_UTIL_H__ -#define __MXQ_UTIL_H__ 1 - -#include -#include -#include - -char** strvec_new(void); -size_t strvec_length(char **strvec); -int strvec_push_str(char ***strvecp, char *str); -int strvec_push_strvec(char*** strvecp, char **strvec); -char* strvec_to_str(char **strvec); -char** strvec_from_str(char *str); -void strvec_free(char **strvec); - -#endif diff --git a/mxqd.c b/mxqd.c index 7c5886f1..034e2c17 100644 --- a/mxqd.c +++ b/mxqd.c @@ -33,7 +33,6 @@ #include "mxq_group.h" #include "mxq_job.h" #include "mx_mysql.h" -#include "mxq_util.h" #include "mxqd.h" #ifndef MXQ_VERSION @@ -1014,7 +1013,7 @@ unsigned long start_job(struct mxq_group_list *group) } - argv = strvec_from_str(mxqjob.job_argv_str); + argv = mx_strvec_from_str(mxqjob.job_argv_str); if (!argv) { mx_log_err("job=%s(%d):%lu:%lu Can't recaculate commandline. str_to_strvev(%s) failed: %m", group->group.user_name, group->group.user_uid, group->group.group_id, mxqjob.job_id, diff --git a/mxqdump.c b/mxqdump.c index 10243725..424a279c 100644 --- a/mxqdump.c +++ b/mxqdump.c @@ -19,7 +19,7 @@ #include "mxq.h" -#include "mxq_util.h" +#include "mx_util.h" #include "mx_mysql.h" #include "mxq_group.h" diff --git a/mxqsub.c b/mxqsub.c index d0768fd5..cbd311c9 100644 --- a/mxqsub.c +++ b/mxqsub.c @@ -26,8 +26,6 @@ #include -#include "mxq_util.h" - #include "mxq_group.h" #include "mxq_job.h" @@ -684,7 +682,7 @@ int main(int argc, char *argv[]) arg_stderr = arg_stderr_absolute; } - arg_args = strvec_to_str(argv); + arg_args = mx_strvec_to_str(argv); assert(arg_args); /******************************************************************/ diff --git a/test.c b/test.c index 9d21e944..6cac737e 100644 --- a/test.c +++ b/test.c @@ -4,8 +4,6 @@ #include #include -#include "mxq_util.h" - static void display_mallinfo(void) { struct mallinfo mi;