-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathenvironment.c
135 lines (124 loc) · 2.46 KB
/
environment.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// SPDX-License-Identifier: GPL-2.0-only
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <unistd.h>
#include "theme.h"
static void
rtrim(char **s)
{
size_t len = strlen(*s);
if (!len) {
return;
}
char *end = *s + len - 1;
while (end >= *s && isspace(*end)) {
end--;
}
*(end + 1) = '\0';
}
static char *
string_strip(char *s)
{
rtrim(&s);
while (isspace(*s)) {
s++;
}
return s;
}
static char *
get_value(char *line, const char *key)
{
if (!line || !*line || line[0] == '#') {
return NULL;
}
char *p = strchr(line, '=');
if (!p) {
return NULL;
}
*p = '\0';
if (!!strcmp(key, string_strip(line))) {
return NULL;
}
char *value = string_strip(++p);
return value ? value : NULL;
}
void
environment_get(char *buffer, size_t size, const char *key)
{
char filename[4096];
snprintf(filename, sizeof(filename), "%s/%s", getenv("HOME"), ".config/labwc/environment");
char *value = NULL;
char *line = NULL;
size_t len = 0;
FILE *stream = fopen(filename, "r");
if (!stream) {
return;
}
while (getline(&line, &len, stream) != -1) {
char *p = strrchr(line, '\n');
if (p) {
*p = '\0';
}
value = get_value(line, key);
if (value) {
snprintf(buffer, size, "%s", value);
break;
}
}
free(line);
fclose(stream);
}
void
environment_set(const char *key, const char *value)
{
if (!key || !*key) {
return;
}
if (!value || !*value) {
return;
}
/* set cursor for labwc - should cover 'replace' or 'append' */
char xcur[4096] = {0};
strcpy(xcur, key);
strcat(xcur, "=");
char filename[4096];
char bufname[4096];
char *home = getenv("HOME");
snprintf(filename, sizeof(filename), "%s/%s", home, ".config/labwc/environment");
snprintf(bufname, sizeof(bufname), "%s/%s", home, ".config/labwc/buf");
FILE *fe = fopen(filename, "r");
FILE *fw = fopen(bufname, "a");
if ((fe == NULL) || (fw == NULL)) {
perror("Unable to open file!");
return;
}
char chunk[128];
while (fgets(chunk, sizeof(chunk), fe) != NULL) {
if (strstr(chunk, xcur) != NULL) {
continue;
} else {
fprintf(fw, "%s", chunk);
}
}
fclose(fe);
if (value) {
fprintf(fw, "%s\n", strcat(xcur, value));
}
fclose(fw);
rename(bufname, filename);
}
void
environment_set_num(const char *key, int value)
{
char buffer[255];
snprintf(buffer, 255, "%d", value);
environment_set(key, buffer);
}