diff --git a/src/launcher.c b/src/launcher.c index 0a1ea2c..e2191f8 100644 --- a/src/launcher.c +++ b/src/launcher.c @@ -30,11 +30,6 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -/* conforming to glib use _GNU_SOURCE for asprintf declaration */ -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif - #include "wmfs.h" static char *complete_on_command(char*, size_t); @@ -257,7 +252,7 @@ complete_on_command(char *start, size_t hits) struct dirent *content; char **namelist = NULL; - int n = 0; + int n = 0, i; void *temp = NULL; if (!getenv("PATH") || !start || hits <= 0) @@ -291,12 +286,15 @@ complete_on_command(char *start, size_t hits) qsort(namelist, n, sizeof(char *), qsort_string_compare); free(path); + if(n > 0) + { ret = _strdup(namelist[((hits > 0) ? hits - 1 : 0) % n] + strlen(start)); - int i; - for(i = 0; i < n; i++) - free(namelist[i]); + for(i = 0; i < n; i++) + free(namelist[i]); + } + free(namelist); return ret; } @@ -331,7 +329,7 @@ complete_on_files(char *start, size_t hits) { /* remplace ~ by $HOME in dirname */ if (!strncmp(p, "~/", 2) && getenv("HOME")) - asprintf(&dirname, "%s%s", getenv("HOME"), p+1); + xasprintf(&dirname, "%s%s", getenv("HOME"), p+1); else dirname = _strdup(p); @@ -362,12 +360,12 @@ complete_on_files(char *start, size_t hits) if (!strncmp(content->d_name, p, strlen(p)) && ++count == hits) { /* If it's a directory append '/' to the completion */ - asprintf(&filepath, "%s/%s", path, content->d_name); + xasprintf(&filepath, "%s/%s", path, content->d_name); if (filepath && stat(filepath, &st) != -1) { if (S_ISDIR(st.st_mode)) - asprintf(&ret, "%s/", content->d_name + strlen(p)); + xasprintf(&ret, "%s/", content->d_name + strlen(p)); else ret = _strdup(content->d_name + strlen(p)); } diff --git a/src/parse/parse.c b/src/parse/parse.c index dd4ee6c..9f0d52f 100644 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -292,16 +292,16 @@ include(struct keyword *head) /* replace ~ by user directory */ if (head->name && head->name[0] == '~') { if ( (user = getpwuid(getuid())) && user->pw_dir) - asprintf(&filename, "%s%s", user->pw_dir, head->name+1); + xasprintf(&filename, "%s%s", user->pw_dir, head->name+1); else if (getenv("HOME")) - asprintf(&filename, "%s%s", getenv("HOME"), head->name+1); + xasprintf(&filename, "%s%s", getenv("HOME"), head->name+1); else /* to warning ? */ filename = head->name; } /* relative path from parent file */ else if (head->name && head->name[0] != '/') { base = strdup(kw->file->parent->name); - asprintf(&filename, "%s/%s", dirname(base), head->name); + xasprintf(&filename, "%s/%s", dirname(base), head->name); free(base); } else @@ -471,6 +471,7 @@ get_conf(const char *filename) } +/* calloc wrapper */ void * xcalloc(size_t nmemb, size_t size) { @@ -481,3 +482,19 @@ xcalloc(size_t nmemb, size_t size) return ret; } + +/* asprintf wrapper */ +int +xasprintf(char **strp, const char *fmt, ...) +{ + int ret; + va_list args; + + va_start(args, fmt); + ret = vasprintf(strp, fmt, args); + va_end(args); + + if (ret == -1) + err(EXIT_FAILURE, "asprintf"); + return ret; +} diff --git a/src/parse/parse.h b/src/parse/parse.h index 915e88d..87ef368 100644 --- a/src/parse/parse.h +++ b/src/parse/parse.h @@ -118,5 +118,7 @@ size_t fetch_opt_count(struct opt_type *); /* wrapper for calloc */ void *xcalloc(size_t, size_t); +/* wrapper for asprintf */ +int xasprintf(char **, const char *, ...); #endif /* PARSE_H */