diff options
Diffstat (limited to 'common/util.c')
-rw-r--r-- | common/util.c | 68 |
1 files changed, 13 insertions, 55 deletions
diff --git a/common/util.c b/common/util.c index 561b3804..f4588b57 100644 --- a/common/util.c +++ b/common/util.c | |||
@@ -1,4 +1,5 @@ | |||
1 | #define _XOPEN_SOURCE 700 | 1 | #define _XOPEN_SOURCE 700 |
2 | #include <assert.h> | ||
2 | #include <sys/types.h> | 3 | #include <sys/types.h> |
3 | #include <sys/stat.h> | 4 | #include <sys/stat.h> |
4 | #include <unistd.h> | 5 | #include <unistd.h> |
@@ -139,60 +140,17 @@ bool parse_boolean(const char *boolean, bool current) { | |||
139 | return false; | 140 | return false; |
140 | } | 141 | } |
141 | 142 | ||
142 | char* resolve_path(const char* path) { | 143 | enum wlr_direction opposite_direction(enum wlr_direction d) { |
143 | struct stat sb; | 144 | switch (d) { |
144 | ssize_t r; | 145 | case WLR_DIRECTION_UP: |
145 | int i; | 146 | return WLR_DIRECTION_DOWN; |
146 | char *current = NULL; | 147 | case WLR_DIRECTION_DOWN: |
147 | char *resolved = NULL; | 148 | return WLR_DIRECTION_UP; |
148 | 149 | case WLR_DIRECTION_RIGHT: | |
149 | if(!(current = strdup(path))) { | 150 | return WLR_DIRECTION_LEFT; |
150 | return NULL; | 151 | case WLR_DIRECTION_LEFT: |
151 | } | 152 | return WLR_DIRECTION_RIGHT; |
152 | for (i = 0; i < 16; ++i) { | ||
153 | if (lstat(current, &sb) == -1) { | ||
154 | goto failed; | ||
155 | } | ||
156 | if((sb.st_mode & S_IFMT) != S_IFLNK) { | ||
157 | return current; | ||
158 | } | ||
159 | if (!(resolved = malloc(sb.st_size + 1))) { | ||
160 | goto failed; | ||
161 | } | ||
162 | r = readlink(current, resolved, sb.st_size); | ||
163 | if (r == -1 || r > sb.st_size) { | ||
164 | goto failed; | ||
165 | } | ||
166 | resolved[r] = '\0'; | ||
167 | free(current); | ||
168 | current = strdup(resolved); | ||
169 | free(resolved); | ||
170 | resolved = NULL; | ||
171 | } | 153 | } |
172 | 154 | assert(false); | |
173 | failed: | 155 | return 0; |
174 | free(resolved); | ||
175 | free(current); | ||
176 | return NULL; | ||
177 | } | ||
178 | |||
179 | bool sway_dir_to_wlr(enum movement_direction dir, enum wlr_direction *out) { | ||
180 | switch (dir) { | ||
181 | case MOVE_UP: | ||
182 | *out = WLR_DIRECTION_UP; | ||
183 | break; | ||
184 | case MOVE_DOWN: | ||
185 | *out = WLR_DIRECTION_DOWN; | ||
186 | break; | ||
187 | case MOVE_LEFT: | ||
188 | *out = WLR_DIRECTION_LEFT; | ||
189 | break; | ||
190 | case MOVE_RIGHT: | ||
191 | *out = WLR_DIRECTION_RIGHT; | ||
192 | break; | ||
193 | default: | ||
194 | return false; | ||
195 | } | ||
196 | |||
197 | return true; | ||
198 | } | 156 | } |