This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
forked from Lixxia/i3lock
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.c
61 lines (55 loc) · 1.44 KB
/
util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <wordexp.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <err.h>
#include "util.h"
int verify_hex(const char *arg, char *colortype, char *varname) {
/* Skip # if present */
int offset = 0;
if (arg[0] == '#') {
offset++;
}
if (strlen(arg+offset) != 6 ||
sscanf(arg+offset, "%06[0-9a-fA-F]", colortype) != 1) {
errx(EXIT_FAILURE, "%s is invalid, it must be given in 3-byte hexadecimal format: rrggbb\n", varname);
return 1;
}
return 0;
}
char* expand_path(char* path) {
wordexp_t we;
/* Expand tokens like ~ to a full path */
wordexp(path, &we, 0);
if (we.we_wordc < 1) {
/* Return the original string, if expansion failed */
return path;
}
/* Return the expanded path */
char *heapcopy = strdup(we.we_wordv[0]);
wordfree(&we);
return heapcopy;
}
char* trim(const char* str) {
if (str == NULL) {
return NULL;
}
int len = strlen(str);
int left = 0, right = len-1;
/* Find left trimming offset */
for (; left < len; ++left) {
if (str[left] != ' ' && str[left] != '\n' && str[left] != '\r' &&
str[left] != '\t' && str[left] != '\f') {
break;
}
}
/* Find right trimming offset */
for (; right > left; --right) {
if (str[right] != ' ' && str[right] != '\n' && str[right] != '\r' &&
str[right] != '\t' && str[right] != '\f') {
break;
}
}
/* Return the trimmed substring */
return strndup(str+left, right-left+1);
}