diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/ipc-client.c | 11 | ||||
-rw-r--r-- | common/list.c | 26 | ||||
-rw-r--r-- | common/loop.c | 6 | ||||
-rw-r--r-- | common/meson.build | 1 | ||||
-rw-r--r-- | common/readline.c | 72 | ||||
-rw-r--r-- | common/stringop.c | 35 | ||||
-rw-r--r-- | common/util.c | 9 |
7 files changed, 37 insertions, 123 deletions
diff --git a/common/ipc-client.c b/common/ipc-client.c index 496fd131..3515ef0a 100644 --- a/common/ipc-client.c +++ b/common/ipc-client.c @@ -7,7 +7,6 @@ #include <sys/un.h> #include <unistd.h> #include "ipc-client.h" -#include "readline.h" #include "log.h" static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; @@ -18,28 +17,30 @@ char *get_socketpath(void) { if (swaysock) { return strdup(swaysock); } + char *line = NULL; + size_t line_size = 0; FILE *fp = popen("sway --get-socketpath 2>/dev/null", "r"); if (fp) { - char *line = read_line(fp); + getline(&line, &line_size, fp); pclose(fp); if (line && *line) { return line; } - free(line); } const char *i3sock = getenv("I3SOCK"); if (i3sock) { + free(line); return strdup(i3sock); } fp = popen("i3 --get-socketpath 2>/dev/null", "r"); if (fp) { - char *line = read_line(fp); + getline(&line, &line_size, fp); pclose(fp); if (line && *line) { return line; } - free(line); } + free(line); return NULL; } diff --git a/common/list.c b/common/list.c index ee268c9a..c3e2fe20 100644 --- a/common/list.c +++ b/common/list.c @@ -17,7 +17,7 @@ list_t *create_list(void) { static void list_resize(list_t *list) { if (list->length == list->capacity) { - list->capacity += 10; + list->capacity *= 2; list->items = realloc(list->items, sizeof(void*) * list->capacity); } } @@ -30,15 +30,6 @@ void list_free(list_t *list) { free(list); } -void list_foreach(list_t *list, void (*callback)(void *item)) { - if (list == NULL || callback == NULL) { - return; - } - for (int i = 0; i < list->length; i++) { - callback(list->items[i]); - } -} - void list_add(list_t *list, void *item) { list_resize(list); list->items[list->length++] = item; @@ -57,8 +48,7 @@ void list_del(list_t *list, int index) { } void list_cat(list_t *list, list_t *source) { - int i; - for (i = 0; i < source->length; ++i) { + for (int i = 0; i < source->length; ++i) { list_add(list, source->items[i]); } } @@ -156,3 +146,15 @@ void list_stable_sort(list_t *list, int compare(const void *a, const void *b)) { list_inplace_sort(list, 0, list->length - 1, compare); } } + +void list_free_items_and_destroy(list_t *list) { + if (!list) { + return; + } + + for (int i = 0; i < list->length; ++i) { + free(list->items[i]); + } + list_free(list); +} + diff --git a/common/loop.c b/common/loop.c index 82b80017..295f3a30 100644 --- a/common/loop.c +++ b/common/loop.c @@ -45,10 +45,8 @@ struct loop *loop_create(void) { } void loop_destroy(struct loop *loop) { - list_foreach(loop->fd_events, free); - list_foreach(loop->timers, free); - list_free(loop->fd_events); - list_free(loop->timers); + list_free_items_and_destroy(loop->fd_events); + list_free_items_and_destroy(loop->timers); free(loop->fds); free(loop); } diff --git a/common/meson.build b/common/meson.build index 224a9c3f..4ad872d1 100644 --- a/common/meson.build +++ b/common/meson.build @@ -8,7 +8,6 @@ lib_sway_common = static_library( 'loop.c', 'list.c', 'pango.c', - 'readline.c', 'stringop.c', 'unicode.c', 'util.c' diff --git a/common/readline.c b/common/readline.c deleted file mode 100644 index 58652429..00000000 --- a/common/readline.c +++ /dev/null @@ -1,72 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include "readline.h" -#include "log.h" -#include <stdlib.h> -#include <stdio.h> - -char *read_line(FILE *file) { - size_t length = 0, size = 128; - char *string = malloc(size); - char lastChar = '\0'; - if (!string) { - wlr_log(WLR_ERROR, "Unable to allocate memory for read_line"); - return NULL; - } - while (1) { - int c = getc(file); - if (c == '\n' && lastChar == '\\'){ - --length; // Ignore last character. - lastChar = '\0'; - continue; - } - if (c == EOF || c == '\n' || c == '\0') { - break; - } - if (c == '\r') { - continue; - } - lastChar = c; - if (length == size) { - char *new_string = realloc(string, size *= 2); - if (!new_string) { - free(string); - wlr_log(WLR_ERROR, "Unable to allocate memory for read_line"); - return NULL; - } - string = new_string; - } - string[length++] = c; - } - if (length + 1 == size) { - char *new_string = realloc(string, length + 1); - if (!new_string) { - free(string); - return NULL; - } - string = new_string; - } - string[length] = '\0'; - return string; -} - -char *peek_line(FILE *file, int line_offset, long *position) { - long pos = ftell(file); - size_t length = 0; - char *line = NULL; - for (int i = 0; i <= line_offset; i++) { - ssize_t read = getline(&line, &length, file); - if (read < 0) { - free(line); - line = NULL; - break; - } - if (read > 0 && line[read - 1] == '\n') { - line[read - 1] = '\0'; - } - } - if (position) { - *position = ftell(file); - } - fseek(file, pos, SEEK_SET); - return line; -} diff --git a/common/stringop.c b/common/stringop.c index d2c91c24..8af0d60f 100644 --- a/common/stringop.c +++ b/common/stringop.c @@ -1,4 +1,4 @@ -#define _XOPEN_SOURCE 700 +#define _POSIX_C_SOURCE 200809L #include <stdlib.h> #include <stdio.h> #include <string.h> @@ -9,24 +9,17 @@ #include "string.h" #include "list.h" -const char whitespace[] = " \f\n\r\t\v"; +static const char whitespace[] = " \f\n\r\t\v"; -char *strip_whitespace(char *_str) { - if (*_str == '\0') - return _str; - char *strold = _str; - while (*_str == ' ' || *_str == '\t') { - _str++; +void strip_whitespace(char *str) { + size_t len = strlen(str); + size_t start = strspn(str, whitespace); + memmove(str, &str[start], len + 1 - start); + + if (*str) { + for (len -= start + 1; isspace(str[len]); --len) {} + str[len + 1] = '\0'; } - char *str = strdup(_str); - free(strold); - int i; - for (i = 0; str[i] != '\0'; ++i); - do { - i--; - } while (i >= 0 && (str[i] == ' ' || str[i] == '\t')); - str[i + 1] = '\0'; - return str; } void strip_quotes(char *str) { @@ -97,14 +90,6 @@ list_t *split_string(const char *str, const char *delims) { return res; } -void free_flat_list(list_t *list) { - int i; - for (i = 0; i < list->length; ++i) { - free(list->items[i]); - } - list_free(list); -} - char **split_args(const char *start, int *argc) { *argc = 0; int alloc = 2; diff --git a/common/util.c b/common/util.c index 0caafb39..d66058a6 100644 --- a/common/util.c +++ b/common/util.c @@ -1,4 +1,4 @@ -#define _XOPEN_SOURCE 700 +#define _POSIX_C_SOURCE 200809L #include <assert.h> #include <sys/types.h> #include <sys/stat.h> @@ -13,7 +13,6 @@ #include <xkbcommon/xkbcommon-names.h> #include <wlr/types/wlr_keyboard.h> #include "log.h" -#include "readline.h" #include "util.h" int wrap(int i, int max) { @@ -24,7 +23,8 @@ int numlen(int n) { if (n == 0) { return 1; } - return log10(n) + 1; + // Account for the '-' in negative numbers. + return log10(abs(n)) + (n > 0 ? 1 : 2); } static struct modifier_key { @@ -86,11 +86,12 @@ pid_t get_parent_pid(pid_t child) { char *token = NULL; const char *sep = " "; FILE *stat = NULL; + size_t buf_size = 0; sprintf(file_name, "/proc/%d/stat", child); if ((stat = fopen(file_name, "r"))) { - if ((buffer = read_line(stat))) { + if (getline(&buffer, &buf_size, stat) != -1) { token = strtok(buffer, sep); // pid token = strtok(NULL, sep); // executable name token = strtok(NULL, sep); // state |