Instead of returning the same value as the parameter to this function, return the length of the string, which can be useful to the caller when its non-zero (e.g. to find the end of the string).
Signed-off-by: Dave Reisner dreisner@archlinux.org --- This should make an earlier email from dan make a bit more sense when he referred to "the new return value"
src/pacman/util.c | 18 +++++++++--------- src/pacman/util.h | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/src/pacman/util.c b/src/pacman/util.c index 7846291..4160c44 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -325,13 +325,13 @@ char *strtoupper(char *str)
/* Trim whitespace and newlines from a string */ -char *strtrim(char *str) +size_t strtrim(char *str) { - char *pch = str; + char *end, *pch = str;
if(str == NULL || *str == '\0') { /* string is empty, so we're done. */ - return str; + return 0; }
while(isspace((unsigned char)*pch)) { @@ -348,16 +348,16 @@ char *strtrim(char *str)
/* check if there wasn't anything but whitespace in the string. */ if(*str == '\0') { - return str; + return 0; }
- pch = (str + (strlen(str) - 1)); - while(isspace((unsigned char)*pch)) { - pch--; + end = (str + strlen(str) - 1); + while(isspace((unsigned char)*end)) { + end--; } - *++pch = '\0'; + *++end = '\0';
- return str; + return end - pch; }
/* Replace all occurances of 'needle' with 'replace' in 'str', returning diff --git a/src/pacman/util.h b/src/pacman/util.h index 6ec962f..6291939 100644 --- a/src/pacman/util.h +++ b/src/pacman/util.h @@ -56,7 +56,7 @@ const char *mbasename(const char *path); char *mdirname(const char *path); void indentprint(const char *str, size_t indent); char *strtoupper(char *str); -char *strtrim(char *str); +size_t strtrim(char *str); char *strreplace(const char *str, const char *needle, const char *replace); alpm_list_t *strsplit(const char *str, const char splitchar); void string_display(const char *title, const char *string);
Signed-off-by: Dave Reisner dreisner@archlinux.org --- src/util/pactree.c | 18 +++++++++--------- 1 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/src/util/pactree.c b/src/util/pactree.c index 55d63d7..77ee75e 100644 --- a/src/util/pactree.c +++ b/src/util/pactree.c @@ -117,13 +117,13 @@ char *strndup(const char *s, size_t n) } #endif
-static char *strtrim(char *str) +static size_t strtrim(char *str) { - char *pch = str; + char *end, *pch = str;
if(str == NULL || *str == '\0') { /* string is empty, so we're done. */ - return str; + return 0; }
while(isspace((unsigned char)*pch)) { @@ -140,16 +140,16 @@ static char *strtrim(char *str)
/* check if there wasn't anything but whitespace in the string. */ if(*str == '\0') { - return str; + return 0; }
- pch = (str + (strlen(str) - 1)); - while(isspace((unsigned char)*pch)) { - pch--; + end = (str + strlen(str) - 1); + while(isspace((unsigned char)*end)) { + end--; } - *++pch = '\0'; + *++end = '\0';
- return str; + return end - pch; }
static int register_syncs(void) {
- take advantage of the new strtrim return value - tighten scope on line pointer
Signed-off-by: Dave Reisner dreisner@archlinux.org --- src/util/pactree.c | 15 +++++++++------ 1 files changed, 9 insertions(+), 6 deletions(-)
diff --git a/src/util/pactree.c b/src/util/pactree.c index 77ee75e..0adc2ea 100644 --- a/src/util/pactree.c +++ b/src/util/pactree.c @@ -154,7 +154,7 @@ static size_t strtrim(char *str)
static int register_syncs(void) { FILE *fp; - char *ptr, *section = NULL; + char *section = NULL; char line[LINE_MAX]; const alpm_siglevel_t level = ALPM_SIG_DATABASE | ALPM_SIG_DATABASE_OPTIONAL;
@@ -165,20 +165,23 @@ static int register_syncs(void) { }
while(fgets(line, LINE_MAX, fp)) { - strtrim(line); + size_t linelen; + char *ptr;
- if(line[0] == '#' || !strlen(line)) { + linelen = strtrim(line); + + if(line[0] == '#' || !linelen) { continue; }
if((ptr = strchr(line, '#'))) { *ptr = '\0'; - strtrim(line); + linelen = strtrim(line); }
- if(line[0] == '[' && line[strlen(line) - 1] == ']') { + if(line[0] == '[' && line[linelen - 1] == ']') { free(section); - section = strndup(&line[1], strlen(line) - 2); + section = strndup(&line[1], linelen - 2);
if(section && strcmp(section, "options") != 0) { alpm_db_register_sync(handle, section, level);
pacman-dev@lists.archlinux.org