add a_strncmp() to util.h and use a_str*() functions everywhere

This commit is contained in:
Julien Danjou 2007-09-24 17:50:42 +02:00
parent 89fa270bca
commit 256717c792
4 changed files with 36 additions and 19 deletions

View File

@ -379,12 +379,14 @@ main(int argc, char *argv[])
switch (r = read(STDIN_FILENO, awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext) - 1)) switch (r = read(STDIN_FILENO, awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext) - 1))
{ {
case -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'; awesomeconf[0].statustext[sizeof(awesomeconf[0].statustext) - 1] = '\0';
readin = False; readin = False;
break; break;
case 0: case 0:
strncpy(awesomeconf[0].statustext, "EOF", 4); a_strncpy(awesomeconf[0].statustext, sizeof(awesomeconf[0].statustext),
"EOF", 4);
readin = False; readin = False;
break; break;
default: default:
@ -392,7 +394,8 @@ main(int argc, char *argv[])
p >= awesomeconf[0].statustext && *p == '\n'; *p-- = '\0'); p >= awesomeconf[0].statustext && *p == '\n'; *p-- = '\0');
for(; p >= awesomeconf[0].statustext && *p != '\n'; --p); for(; p >= awesomeconf[0].statustext && *p != '\n'; --p);
if(p > awesomeconf[0].statustext) 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]); drawstatusbar(dpy, &dc[0], &awesomeconf[0]);
} }

View File

@ -164,7 +164,7 @@ set_default_config(awesome_config *awesomeconf)
{ {
/** \todo most of this stuff aren't freed when we initialize /** \todo most of this stuff aren't freed when we initialize
* the real configuration, we should add a clean conf function */ * 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.width = 0;
awesomeconf->statusbar.height = 0; awesomeconf->statusbar.height = 0;
awesomeconf->opacity_unfocused = -1; awesomeconf->opacity_unfocused = -1;
@ -215,14 +215,16 @@ parse_config(Display * disp, int scr, DC * drawcontext, awesome_config *awesomec
const char *tmp, *homedir; const char *tmp, *homedir;
char *confpath; char *confpath;
KeySym tmp_key; KeySym tmp_key;
ssize_t confpath_len;
set_default_config(awesomeconf); set_default_config(awesomeconf);
homedir = getenv("HOME"); homedir = getenv("HOME");
confpath = p_new(char, strlen(homedir) + strlen(AWESOME_CONFIG_FILE) + 2); confpath_len = a_strlen(homedir) + a_strlen(AWESOME_CONFIG_FILE) + 2;
strcpy(confpath, homedir); confpath = p_new(char, confpath_len);
strcat(confpath, "/"); a_strcpy(confpath, confpath_len, homedir);
strcat(confpath, AWESOME_CONFIG_FILE); a_strcat(confpath, confpath_len, "/");
a_strcat(confpath, confpath_len, AWESOME_CONFIG_FILE);
config_init(&awesomelibconf); 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].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"))); 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].tags = NULL;
awesomeconf->rules[i].isfloating = awesomeconf->rules[i].isfloating =
config_setting_get_bool(config_setting_get_member(confsubrules, "float")); 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 */ /* barpos */
tmp = config_lookup_string(&awesomelibconf, "awesome.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; 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; awesomeconf->statusbar_default_position = BarBot;
else else
awesomeconf->statusbar_default_position = BarTop; awesomeconf->statusbar_default_position = BarTop;

14
util.c
View File

@ -63,13 +63,13 @@ uicb_spawn(Display * disp,
char *tmp, newdisplay[128]; char *tmp, newdisplay[128];
if(!shell && !(shell = getenv("SHELL"))) if(!shell && !(shell = getenv("SHELL")))
shell = strdup("/bin/sh"); shell = a_strdup("/bin/sh");
if(!arg) if(!arg)
return; return;
if((tmp = getenv("DISPLAY"))) if((tmp = getenv("DISPLAY")))
{ {
display = strdup(tmp); display = a_strdup(tmp);
if((tmp = strrchr(display, '.'))) if((tmp = strrchr(display, '.')))
*tmp = '\0'; *tmp = '\0';
snprintf(newdisplay, sizeof(newdisplay), "%s.%d", display, awesomeconf->screen); snprintf(newdisplay, sizeof(newdisplay), "%s.%d", display, awesomeconf->screen);
@ -96,14 +96,14 @@ uicb_spawn(Display * disp,
} }
Bool 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; char **list = NULL;
int n; int n;
XTextProperty name; XTextProperty name;
if(!text || size == 0) if(!text || !textlen)
return False; return False;
text[0] = '\0'; text[0] = '\0';
@ -113,15 +113,15 @@ xgettextprop(Display *disp, Window w, Atom atom, char *text, unsigned int size)
return False; return False;
if(name.encoding == XA_STRING) 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) 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); XFreeStringList(list);
} }
text[size - 1] = '\0'; text[textlen - 1] = '\0';
XFree(name.value); XFree(name.value);
return True; return True;

14
util.h
View File

@ -160,6 +160,18 @@ static inline int a_strcmp(const char *a, const char *b)
return strcmp(NONULL(a), NONULL(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 <tt>strncmp(a, b, n)</tt>, 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_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))); 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 die(const char *, ...) __attribute__ ((noreturn)) __attribute__ ((format(printf, 1, 2)));
void eprint(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); double compute_new_value_from_arg(const char *, double);
UICB_PROTO(uicb_spawn); UICB_PROTO(uicb_spawn);