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 @@ | |||
7 | #include <sys/un.h> | 7 | #include <sys/un.h> |
8 | #include <unistd.h> | 8 | #include <unistd.h> |
9 | #include "ipc-client.h" | 9 | #include "ipc-client.h" |
10 | #include "readline.h" | ||
11 | #include "log.h" | 10 | #include "log.h" |
12 | 11 | ||
13 | static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; | 12 | static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; |
@@ -18,28 +17,30 @@ char *get_socketpath(void) { | |||
18 | if (swaysock) { | 17 | if (swaysock) { |
19 | return strdup(swaysock); | 18 | return strdup(swaysock); |
20 | } | 19 | } |
20 | char *line = NULL; | ||
21 | size_t line_size = 0; | ||
21 | FILE *fp = popen("sway --get-socketpath 2>/dev/null", "r"); | 22 | FILE *fp = popen("sway --get-socketpath 2>/dev/null", "r"); |
22 | if (fp) { | 23 | if (fp) { |
23 | char *line = read_line(fp); | 24 | getline(&line, &line_size, fp); |
24 | pclose(fp); | 25 | pclose(fp); |
25 | if (line && *line) { | 26 | if (line && *line) { |
26 | return line; | 27 | return line; |
27 | } | 28 | } |
28 | free(line); | ||
29 | } | 29 | } |
30 | const char *i3sock = getenv("I3SOCK"); | 30 | const char *i3sock = getenv("I3SOCK"); |
31 | if (i3sock) { | 31 | if (i3sock) { |
32 | free(line); | ||
32 | return strdup(i3sock); | 33 | return strdup(i3sock); |
33 | } | 34 | } |
34 | fp = popen("i3 --get-socketpath 2>/dev/null", "r"); | 35 | fp = popen("i3 --get-socketpath 2>/dev/null", "r"); |
35 | if (fp) { | 36 | if (fp) { |
36 | char *line = read_line(fp); | 37 | getline(&line, &line_size, fp); |
37 | pclose(fp); | 38 | pclose(fp); |
38 | if (line && *line) { | 39 | if (line && *line) { |
39 | return line; | 40 | return line; |
40 | } | 41 | } |
41 | free(line); | ||
42 | } | 42 | } |
43 | free(line); | ||
43 | return NULL; | 44 | return NULL; |
44 | } | 45 | } |
45 | 46 | ||
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) { | |||
17 | 17 | ||
18 | static void list_resize(list_t *list) { | 18 | static void list_resize(list_t *list) { |
19 | if (list->length == list->capacity) { | 19 | if (list->length == list->capacity) { |
20 | list->capacity += 10; | 20 | list->capacity *= 2; |
21 | list->items = realloc(list->items, sizeof(void*) * list->capacity); | 21 | list->items = realloc(list->items, sizeof(void*) * list->capacity); |
22 | } | 22 | } |
23 | } | 23 | } |
@@ -30,15 +30,6 @@ void list_free(list_t *list) { | |||
30 | free(list); | 30 | free(list); |
31 | } | 31 | } |
32 | 32 | ||
33 | void list_foreach(list_t *list, void (*callback)(void *item)) { | ||
34 | if (list == NULL || callback == NULL) { | ||
35 | return; | ||
36 | } | ||
37 | for (int i = 0; i < list->length; i++) { | ||
38 | callback(list->items[i]); | ||
39 | } | ||
40 | } | ||
41 | |||
42 | void list_add(list_t *list, void *item) { | 33 | void list_add(list_t *list, void *item) { |
43 | list_resize(list); | 34 | list_resize(list); |
44 | list->items[list->length++] = item; | 35 | list->items[list->length++] = item; |
@@ -57,8 +48,7 @@ void list_del(list_t *list, int index) { | |||
57 | } | 48 | } |
58 | 49 | ||
59 | void list_cat(list_t *list, list_t *source) { | 50 | void list_cat(list_t *list, list_t *source) { |
60 | int i; | 51 | for (int i = 0; i < source->length; ++i) { |
61 | for (i = 0; i < source->length; ++i) { | ||
62 | list_add(list, source->items[i]); | 52 | list_add(list, source->items[i]); |
63 | } | 53 | } |
64 | } | 54 | } |
@@ -156,3 +146,15 @@ void list_stable_sort(list_t *list, int compare(const void *a, const void *b)) { | |||
156 | list_inplace_sort(list, 0, list->length - 1, compare); | 146 | list_inplace_sort(list, 0, list->length - 1, compare); |
157 | } | 147 | } |
158 | } | 148 | } |
149 | |||
150 | void list_free_items_and_destroy(list_t *list) { | ||
151 | if (!list) { | ||
152 | return; | ||
153 | } | ||
154 | |||
155 | for (int i = 0; i < list->length; ++i) { | ||
156 | free(list->items[i]); | ||
157 | } | ||
158 | list_free(list); | ||
159 | } | ||
160 | |||
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) { | |||
45 | } | 45 | } |
46 | 46 | ||
47 | void loop_destroy(struct loop *loop) { | 47 | void loop_destroy(struct loop *loop) { |
48 | list_foreach(loop->fd_events, free); | 48 | list_free_items_and_destroy(loop->fd_events); |
49 | list_foreach(loop->timers, free); | 49 | list_free_items_and_destroy(loop->timers); |
50 | list_free(loop->fd_events); | ||
51 | list_free(loop->timers); | ||
52 | free(loop->fds); | 50 | free(loop->fds); |
53 | free(loop); | 51 | free(loop); |
54 | } | 52 | } |
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( | |||
8 | 'loop.c', | 8 | 'loop.c', |
9 | 'list.c', | 9 | 'list.c', |
10 | 'pango.c', | 10 | 'pango.c', |
11 | 'readline.c', | ||
12 | 'stringop.c', | 11 | 'stringop.c', |
13 | 'unicode.c', | 12 | 'unicode.c', |
14 | 'util.c' | 13 | '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 @@ | |||
1 | #define _POSIX_C_SOURCE 200809L | ||
2 | #include "readline.h" | ||
3 | #include "log.h" | ||
4 | #include <stdlib.h> | ||
5 | #include <stdio.h> | ||
6 | |||
7 | char *read_line(FILE *file) { | ||
8 | size_t length = 0, size = 128; | ||
9 | char *string = malloc(size); | ||
10 | char lastChar = '\0'; | ||
11 | if (!string) { | ||
12 | wlr_log(WLR_ERROR, "Unable to allocate memory for read_line"); | ||
13 | return NULL; | ||
14 | } | ||
15 | while (1) { | ||
16 | int c = getc(file); | ||
17 | if (c == '\n' && lastChar == '\\'){ | ||
18 | --length; // Ignore last character. | ||
19 | lastChar = '\0'; | ||
20 | continue; | ||
21 | } | ||
22 | if (c == EOF || c == '\n' || c == '\0') { | ||
23 | break; | ||
24 | } | ||
25 | if (c == '\r') { | ||
26 | continue; | ||
27 | } | ||
28 | lastChar = c; | ||
29 | if (length == size) { | ||
30 | char *new_string = realloc(string, size *= 2); | ||
31 | if (!new_string) { | ||
32 | free(string); | ||
33 | wlr_log(WLR_ERROR, "Unable to allocate memory for read_line"); | ||
34 | return NULL; | ||
35 | } | ||
36 | string = new_string; | ||
37 | } | ||
38 | string[length++] = c; | ||
39 | } | ||
40 | if (length + 1 == size) { | ||
41 | char *new_string = realloc(string, length + 1); | ||
42 | if (!new_string) { | ||
43 | free(string); | ||
44 | return NULL; | ||
45 | } | ||
46 | string = new_string; | ||
47 | } | ||
48 | string[length] = '\0'; | ||
49 | return string; | ||
50 | } | ||
51 | |||
52 | char *peek_line(FILE *file, int line_offset, long *position) { | ||
53 | long pos = ftell(file); | ||
54 | size_t length = 0; | ||
55 | char *line = NULL; | ||
56 | for (int i = 0; i <= line_offset; i++) { | ||
57 | ssize_t read = getline(&line, &length, file); | ||
58 | if (read < 0) { | ||
59 | free(line); | ||
60 | line = NULL; | ||
61 | break; | ||
62 | } | ||
63 | if (read > 0 && line[read - 1] == '\n') { | ||
64 | line[read - 1] = '\0'; | ||
65 | } | ||
66 | } | ||
67 | if (position) { | ||
68 | *position = ftell(file); | ||
69 | } | ||
70 | fseek(file, pos, SEEK_SET); | ||
71 | return line; | ||
72 | } | ||
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 @@ | |||
1 | #define _XOPEN_SOURCE 700 | 1 | #define _POSIX_C_SOURCE 200809L |
2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
3 | #include <stdio.h> | 3 | #include <stdio.h> |
4 | #include <string.h> | 4 | #include <string.h> |
@@ -9,24 +9,17 @@ | |||
9 | #include "string.h" | 9 | #include "string.h" |
10 | #include "list.h" | 10 | #include "list.h" |
11 | 11 | ||
12 | const char whitespace[] = " \f\n\r\t\v"; | 12 | static const char whitespace[] = " \f\n\r\t\v"; |
13 | 13 | ||
14 | char *strip_whitespace(char *_str) { | 14 | void strip_whitespace(char *str) { |
15 | if (*_str == '\0') | 15 | size_t len = strlen(str); |
16 | return _str; | 16 | size_t start = strspn(str, whitespace); |
17 | char *strold = _str; | 17 | memmove(str, &str[start], len + 1 - start); |
18 | while (*_str == ' ' || *_str == '\t') { | 18 | |
19 | _str++; | 19 | if (*str) { |
20 | for (len -= start + 1; isspace(str[len]); --len) {} | ||
21 | str[len + 1] = '\0'; | ||
20 | } | 22 | } |
21 | char *str = strdup(_str); | ||
22 | free(strold); | ||
23 | int i; | ||
24 | for (i = 0; str[i] != '\0'; ++i); | ||
25 | do { | ||
26 | i--; | ||
27 | } while (i >= 0 && (str[i] == ' ' || str[i] == '\t')); | ||
28 | str[i + 1] = '\0'; | ||
29 | return str; | ||
30 | } | 23 | } |
31 | 24 | ||
32 | void strip_quotes(char *str) { | 25 | void strip_quotes(char *str) { |
@@ -97,14 +90,6 @@ list_t *split_string(const char *str, const char *delims) { | |||
97 | return res; | 90 | return res; |
98 | } | 91 | } |
99 | 92 | ||
100 | void free_flat_list(list_t *list) { | ||
101 | int i; | ||
102 | for (i = 0; i < list->length; ++i) { | ||
103 | free(list->items[i]); | ||
104 | } | ||
105 | list_free(list); | ||
106 | } | ||
107 | |||
108 | char **split_args(const char *start, int *argc) { | 93 | char **split_args(const char *start, int *argc) { |
109 | *argc = 0; | 94 | *argc = 0; |
110 | int alloc = 2; | 95 | 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 @@ | |||
1 | #define _XOPEN_SOURCE 700 | 1 | #define _POSIX_C_SOURCE 200809L |
2 | #include <assert.h> | 2 | #include <assert.h> |
3 | #include <sys/types.h> | 3 | #include <sys/types.h> |
4 | #include <sys/stat.h> | 4 | #include <sys/stat.h> |
@@ -13,7 +13,6 @@ | |||
13 | #include <xkbcommon/xkbcommon-names.h> | 13 | #include <xkbcommon/xkbcommon-names.h> |
14 | #include <wlr/types/wlr_keyboard.h> | 14 | #include <wlr/types/wlr_keyboard.h> |
15 | #include "log.h" | 15 | #include "log.h" |
16 | #include "readline.h" | ||
17 | #include "util.h" | 16 | #include "util.h" |
18 | 17 | ||
19 | int wrap(int i, int max) { | 18 | int wrap(int i, int max) { |
@@ -24,7 +23,8 @@ int numlen(int n) { | |||
24 | if (n == 0) { | 23 | if (n == 0) { |
25 | return 1; | 24 | return 1; |
26 | } | 25 | } |
27 | return log10(n) + 1; | 26 | // Account for the '-' in negative numbers. |
27 | return log10(abs(n)) + (n > 0 ? 1 : 2); | ||
28 | } | 28 | } |
29 | 29 | ||
30 | static struct modifier_key { | 30 | static struct modifier_key { |
@@ -86,11 +86,12 @@ pid_t get_parent_pid(pid_t child) { | |||
86 | char *token = NULL; | 86 | char *token = NULL; |
87 | const char *sep = " "; | 87 | const char *sep = " "; |
88 | FILE *stat = NULL; | 88 | FILE *stat = NULL; |
89 | size_t buf_size = 0; | ||
89 | 90 | ||
90 | sprintf(file_name, "/proc/%d/stat", child); | 91 | sprintf(file_name, "/proc/%d/stat", child); |
91 | 92 | ||
92 | if ((stat = fopen(file_name, "r"))) { | 93 | if ((stat = fopen(file_name, "r"))) { |
93 | if ((buffer = read_line(stat))) { | 94 | if (getline(&buffer, &buf_size, stat) != -1) { |
94 | token = strtok(buffer, sep); // pid | 95 | token = strtok(buffer, sep); // pid |
95 | token = strtok(NULL, sep); // executable name | 96 | token = strtok(NULL, sep); // executable name |
96 | token = strtok(NULL, sep); // state | 97 | token = strtok(NULL, sep); // state |