From 68f906f278aba60c669e2cbcb7185ae970f1aa18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Tarl=C3=A1=20Cardoso=20Lemos?= Date: Tue, 30 Nov 2010 22:47:16 -0200 Subject: [PATCH] util.c is the proper place for those functions. --- include/config.h | 3 --- include/util.h | 14 ++++++++++++++ src/config.c | 41 ----------------------------------------- src/util.c | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 44 deletions(-) diff --git a/include/config.h b/include/config.h index eaa14f60..ccff0fc8 100644 --- a/include/config.h +++ b/include/config.h @@ -129,9 +129,6 @@ struct Config { } bar; }; -char *resolve_tilde(const char *path); -bool path_exists(const char *path); - /** * Reads the configuration from ~/.i3/config or /etc/i3/config if not found. * diff --git a/include/util.h b/include/util.h index fab7e399..46a15655 100644 --- a/include/util.h +++ b/include/util.h @@ -103,6 +103,20 @@ void check_error(xcb_connection_t *conn, xcb_void_cookie_t cookie, */ char *convert_utf8_to_ucs2(char *input, int *real_strlen); +/* + * This function resolves ~ in pathnames. + * It may resolve wildcards in the first part of the path, but if no match + * or multiple matches are found, it just returns a copy of path as given. + * + */ +char *resolve_tilde(const char *path); + +/* + * Checks if the given path exists by calling stat(). + * + */ +bool path_exists(const char *path); + /* * Restart i3 in-place * appends -a to argument list to disable autostart diff --git a/src/config.c b/src/config.c index e60fd9b0..683310c8 100644 --- a/src/config.c +++ b/src/config.c @@ -22,47 +22,6 @@ Config config; struct modes_head modes; - -/* - * This function resolves ~ in pathnames. - * It may resolve wildcards in the first part of the path, but if no match - * or multiple matches are found, it just returns a copy of path as given. - * - */ -char *resolve_tilde(const char *path) { - static glob_t globbuf; - char *head, *tail, *result; - - tail = strchr(path, '/'); - head = strndup(path, tail ? tail - path : strlen(path)); - - int res = glob(head, GLOB_TILDE, NULL, &globbuf); - free(head); - /* no match, or many wildcard matches are bad */ - if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1) - result = sstrdup(path); - else if (res != 0) { - die("glob() failed"); - } else { - head = globbuf.gl_pathv[0]; - result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1); - strncpy(result, head, strlen(head)); - strncat(result, tail, strlen(tail)); - } - globfree(&globbuf); - - return result; -} - -/* - * Checks if the given path exists by calling stat(). - * - */ -bool path_exists(const char *path) { - struct stat buf; - return (stat(path, &buf) == 0); -} - /** * Ungrabs all keys, to be called before re-grabbing the keys because of a * mapping_notify event or a configuration file reload diff --git a/src/util.c b/src/util.c index 0c36cf57..89f55960 100644 --- a/src/util.c +++ b/src/util.c @@ -175,6 +175,46 @@ char *convert_utf8_to_ucs2(char *input, int *real_strlen) { return buffer; } +/* + * This function resolves ~ in pathnames. + * It may resolve wildcards in the first part of the path, but if no match + * or multiple matches are found, it just returns a copy of path as given. + * + */ +char *resolve_tilde(const char *path) { + static glob_t globbuf; + char *head, *tail, *result; + + tail = strchr(path, '/'); + head = strndup(path, tail ? tail - path : strlen(path)); + + int res = glob(head, GLOB_TILDE, NULL, &globbuf); + free(head); + /* no match, or many wildcard matches are bad */ + if (res == GLOB_NOMATCH || globbuf.gl_pathc != 1) + result = sstrdup(path); + else if (res != 0) { + die("glob() failed"); + } else { + head = globbuf.gl_pathv[0]; + result = scalloc(strlen(head) + (tail ? strlen(tail) : 0) + 1); + strncpy(result, head, strlen(head)); + strncat(result, tail, strlen(tail)); + } + globfree(&globbuf); + + return result; +} + +/* + * Checks if the given path exists by calling stat(). + * + */ +bool path_exists(const char *path) { + struct stat buf; + return (stat(path, &buf) == 0); +} + /* * Goes through the list of arguments (for exec()) and checks if the given argument * is present. If not, it copies the arguments (because we cannot realloc it) and