From 256717c79224530733958fb89596b4f8196c99c7 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Mon, 24 Sep 2007 17:50:42 +0200 Subject: [PATCH] add a_strncmp() to util.h and use a_str*() functions everywhere --- awesome.c | 9 ++++++--- config.c | 18 ++++++++++-------- util.c | 14 +++++++------- util.h | 14 +++++++++++++- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/awesome.c b/awesome.c index 59ba712d..df6123ca 100644 --- a/awesome.c +++ b/awesome.c @@ -379,12 +379,14 @@ main(int argc, char *argv[]) switch (r = read(STDIN_FILENO, awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext) - 1)) { case -1: - strncpy(awesomeconf[0].statustext, strerror(errno), sizeof(awesomeconf[0].statustext) - 1); + a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext), + strerror(errno), sizeof(awesomeconf[0].statustext) - 1); awesomeconf[0].statustext[sizeof(awesomeconf[0].statustext) - 1] = '\0'; readin = False; break; case 0: - strncpy(awesomeconf[0].statustext, "EOF", 4); + a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext), + "EOF", 4); readin = False; break; default: @@ -392,7 +394,8 @@ main(int argc, char *argv[]) p >= awesomeconf[0].statustext && *p == '\n'; *p-- = '\0'); for(; p >= awesomeconf[0].statustext && *p != '\n'; --p); if(p > awesomeconf[0].statustext) - strncpy(awesomeconf[0].statustext, p + 1, sizeof(awesomeconf[0].statustext)); + a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext), + p + 1, sizeof(awesomeconf[0].statustext)); } drawstatusbar(dpy, &dc[0], &awesomeconf[0]); } diff --git a/config.c b/config.c index eade05d9..2b71c1b7 100644 --- a/config.c +++ b/config.c @@ -164,7 +164,7 @@ set_default_config(awesome_config *awesomeconf) { /** \todo most of this stuff aren't freed when we initialize * the real configuration, we should add a clean conf function */ - strcpy(awesomeconf->statustext, "awesome-" VERSION); + a_strcpy(awesomeconf->statustext, sizeof(awesomeconf->statustext), "awesome-" VERSION); awesomeconf->statusbar.width = 0; awesomeconf->statusbar.height = 0; awesomeconf->opacity_unfocused = -1; @@ -215,14 +215,16 @@ parse_config(Display * disp, int scr, DC * drawcontext, awesome_config *awesomec const char *tmp, *homedir; char *confpath; KeySym tmp_key; + ssize_t confpath_len; set_default_config(awesomeconf); homedir = getenv("HOME"); - confpath = p_new(char, strlen(homedir) + strlen(AWESOME_CONFIG_FILE) + 2); - strcpy(confpath, homedir); - strcat(confpath, "/"); - strcat(confpath, AWESOME_CONFIG_FILE); + confpath_len = a_strlen(homedir) + a_strlen(AWESOME_CONFIG_FILE) + 2; + confpath = p_new(char, confpath_len); + a_strcpy(confpath, confpath_len, homedir); + a_strcat(confpath, confpath_len, "/"); + a_strcat(confpath, confpath_len, AWESOME_CONFIG_FILE); config_init(&awesomelibconf); @@ -315,7 +317,7 @@ parse_config(Display * disp, int scr, DC * drawcontext, awesome_config *awesomec { awesomeconf->rules[i].prop = a_strdup(config_setting_get_string(config_setting_get_member(confsubrules, "name"))); awesomeconf->rules[i].tags = a_strdup(config_setting_get_string(config_setting_get_member(confsubrules, "tags"))); - if(awesomeconf->rules[i].tags && !strlen(awesomeconf->rules[i].tags)) + if(awesomeconf->rules[i].tags && !a_strlen(awesomeconf->rules[i].tags)) awesomeconf->rules[i].tags = NULL; awesomeconf->rules[i].isfloating = config_setting_get_bool(config_setting_get_member(confsubrules, "float")); @@ -354,9 +356,9 @@ parse_config(Display * disp, int scr, DC * drawcontext, awesome_config *awesomec /* barpos */ tmp = config_lookup_string(&awesomelibconf, "awesome.barpos"); - if(tmp && !strncmp(tmp, "off", 6)) + if(tmp && !a_strncmp(tmp, "off", 6)) awesomeconf->statusbar_default_position = BarOff; - else if(tmp && !strncmp(tmp, "bottom", 6)) + else if(tmp && !a_strncmp(tmp, "bottom", 6)) awesomeconf->statusbar_default_position = BarBot; else awesomeconf->statusbar_default_position = BarTop; diff --git a/util.c b/util.c index 67a8c5ed..574009e9 100644 --- a/util.c +++ b/util.c @@ -63,13 +63,13 @@ uicb_spawn(Display * disp, char *tmp, newdisplay[128]; if(!shell && !(shell = getenv("SHELL"))) - shell = strdup("/bin/sh"); + shell = a_strdup("/bin/sh"); if(!arg) return; if((tmp = getenv("DISPLAY"))) { - display = strdup(tmp); + display = a_strdup(tmp); if((tmp = strrchr(display, '.'))) *tmp = '\0'; snprintf(newdisplay, sizeof(newdisplay), "%s.%d", display, awesomeconf->screen); @@ -96,14 +96,14 @@ uicb_spawn(Display * disp, } Bool -xgettextprop(Display *disp, Window w, Atom atom, char *text, unsigned int size) +xgettextprop(Display *disp, Window w, Atom atom, char *text, ssize_t textlen) { char **list = NULL; int n; XTextProperty name; - if(!text || size == 0) + if(!text || !textlen) return False; text[0] = '\0'; @@ -113,15 +113,15 @@ xgettextprop(Display *disp, Window w, Atom atom, char *text, unsigned int size) return False; if(name.encoding == XA_STRING) - strncpy(text, (char *) name.value, size - 1); + a_strncpy(text, textlen, (char *) name.value, textlen - 1); else if(XmbTextPropertyToTextList(disp, &name, &list, &n) >= Success && n > 0 && *list) { - strncpy(text, *list, size - 1); + a_strncpy(text, textlen, *list, textlen - 1); XFreeStringList(list); } - text[size - 1] = '\0'; + text[textlen - 1] = '\0'; XFree(name.value); return True; diff --git a/util.h b/util.h index ad158395..d902a7b2 100644 --- a/util.h +++ b/util.h @@ -160,6 +160,18 @@ static inline int a_strcmp(const char *a, const char *b) return strcmp(NONULL(a), NONULL(b)); } +/** \brief \c NULL resistant strncmp. + * \param[in] a the first string. + * \param[in] b the second string. + * \param[in] n the number of maximum chars to compare. + * \return strncmp(a, b, n), and treats \c NULL strings like \c "" + * ones. + */ +static inline int a_strncmp(const char *a, const char *b, ssize_t n) +{ + return strncmp(NONULL(a), NONULL(b), 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))); @@ -181,7 +193,7 @@ static inline ssize_t a_strcat(char *dst, ssize_t n, const char *src) void die(const char *, ...) __attribute__ ((noreturn)) __attribute__ ((format(printf, 1, 2))); void eprint(const char *, ...) __attribute__ ((noreturn)) __attribute__ ((format(printf, 1, 2))); -Bool xgettextprop(Display *, Window, Atom, char *, unsigned int); +Bool xgettextprop(Display *, Window, Atom, char *, ssize_t); double compute_new_value_from_arg(const char *, double); UICB_PROTO(uicb_spawn);