Skip to content

Commit 94ae033

Browse files
committed
refactored for readability
1 parent 64b3ac7 commit 94ae033

File tree

1 file changed

+40
-106
lines changed

1 file changed

+40
-106
lines changed

kfc.c

Lines changed: 40 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#include <time.h>
1010
#include <unistd.h>
1111

12-
#define VERSION "0.1.2"
13-
1412
#define SEQUENCE "printf \"\
1513
\\033]4;0;#$(echo $color00)\\033\\ \
1614
\\033]4;1;#$(echo $color01)\\033\\ \
@@ -57,40 +55,33 @@ static char *mode = "dark"; /* selected mode string */
5755
static char *sval = NULL; /* selected palette string */
5856

5957
static void
60-
find_palettes(void)
61-
{
62-
if (access("/usr/share/kfc/palettes", F_OK) == 0)
63-
{
58+
find_palettes(void) {
59+
if (access("/usr/share/kfc/palettes", F_OK) == 0) {
6460
seq = "/usr/share/kfc/palettes";
6561
}
66-
else if (access("palettes", F_OK) == 0)
67-
{
62+
else if (access("palettes", F_OK) == 0) {
6863
seq = "palettes";
6964
}
70-
else
71-
{
65+
else {
7266
fprintf(stderr, "Palette source directory not found\n");
7367
exit(1);
7468
}
7569
}
7670

7771
static void
78-
select_palette(char *sel)
79-
{
72+
select_palette(char *sel) {
8073
char *evar, *eval, *line;
8174
FILE *fp = fopen(sel, "r");
8275
size_t len = 255;
8376

8477
line = malloc(sizeof(char) * len);
8578

86-
if (fp == NULL)
87-
{
79+
if (fp == NULL) {
8880
fprintf(stderr, "Selected palette does not exist\n");
8981
exit(1);
9082
}
9183

92-
while (fgets(line, len, fp) != NULL)
93-
{
84+
while (fgets(line, len, fp) != NULL) {
9485
evar = strtok(line, "=");
9586
eval = strtok(NULL, "=");
9687
setenv(evar, eval, 1);
@@ -106,53 +97,41 @@ select_palette(char *sel)
10697
}
10798

10899
static void
109-
list_palette(char *sel)
110-
{
100+
list_palette(char *sel) {
111101
struct dirent **de;
112102
int n = scandir(sel, &de, NULL, alphasort);
113103

114-
while (n--)
115-
{
116-
if (de[n]->d_type == 8)
117-
{
118-
printf("%s/%s\n", mode, de[n]->d_name);
119-
}
104+
while (n--) {
105+
if (de[n]->d_type == 8) printf("%s/%s\n", mode, de[n]->d_name);
120106
}
121107

122108
free(de);
123109
}
124110

125111
static void
126-
random_palette(char *sel)
127-
{
112+
random_palette(char *sel) {
128113
struct dirent **de;
129114
int n = scandir(sel, &de, NULL, alphasort);
130115

131116
srand(time(0));
132117
int i = (rand() % (n - 0 + 1)) + 0;
133118

134-
while (n--)
135-
{
136-
if (i == n)
137-
{
138-
sval = de[n]->d_name;
139-
}
119+
while (n--) {
120+
if (i == n) sval = de[n]->d_name;
140121
}
141122

142123
free(de);
143124
}
144125

145126
static void
146-
print_palette(void)
147-
{
127+
print_palette(void) {
148128
FILE *fp = fopen(conf, "r");
149129
size_t len = 255;
150130
char *line;
151131

152132
line = malloc(sizeof(char) * len);
153133

154-
if (fp == NULL)
155-
{
134+
if (fp == NULL) {
156135
fprintf(stderr, "No palette set or config file missing\n");
157136
exit(1);
158137
}
@@ -165,48 +144,26 @@ print_palette(void)
165144
printf(PALETTE);
166145
}
167146

168-
static void
169-
usage(void)
170-
{
171-
fprintf(stderr, "\
172-
usage: kfc [-L] [-r|-s palette] [-l|-p|-v]\n \
173-
-L Set light themes (modifier for -s/-r)\n \
174-
-r Select a random palette (dark theme by default)\n \
175-
-s palette Select a palette (dark theme by default)\n \
176-
-l List all palettes (dark themes by default)\n \
177-
-p Print current palette\n \
178-
-v Show version information\n");
179-
exit(1);
180-
}
181-
182147
int
183-
main(int argc, char **argv)
184-
{
185-
extern char *optarg;
186-
extern int optind, optopt;
148+
main(int argc, char **argv) {
187149
int cval;
188-
int rflag = 0;
189-
int lflag = 0;
190-
int pflag = 0;
150+
int rflag = 0, lflag = 0, pflag = 0;
191151
size_t len;
192152
char *sel, *tmp;
193153

194-
if (argc == 1)
195-
{
154+
if (argc == 1) {
196155
fprintf(stderr, "No argument(s) provided\n");
197-
exit(1);
156+
return 1;
198157
}
199158

200159
tmp = getenv("XDG_CONFIG_HOME");
201160

202-
if (tmp != NULL)
203-
{
161+
if (tmp != NULL) {
204162
len = strlen(tmp) + sizeof("/kfc/current");
205163
conf = malloc(sizeof(char) * len);
206164
snprintf(conf, len, "%s", tmp);
207165
}
208-
else
209-
{
166+
else {
210167
puts("XDG_CONFIG_HOME is not defined. Defaulting to HOME/.config");
211168
tmp = getenv("HOME");
212169
len = strlen(tmp) + sizeof("/.config/kfc/current");
@@ -216,66 +173,44 @@ main(int argc, char **argv)
216173

217174
strcat(conf, "/kfc");
218175

219-
if (mkdir(conf, 0777) == 0)
220-
{
176+
if (mkdir(conf, 0777) == 0) {
221177
puts("Created 'kfc' directory in XDG_CONFIG_HOME");
222178
}
223179

224180
strcat(conf, "/current");
225181
find_palettes();
226182

227-
while ((cval = getopt(argc, argv, "rlLpvs:" )) != -1)
228-
{
229-
switch (cval)
230-
{
231-
case 'v':
232-
puts(VERSION);
233-
break;
234-
case 'L':
235-
mode = "light";
236-
break;
237-
case 'l':
238-
lflag++;
239-
break;
240-
case 'r':
241-
rflag++;
242-
break;
183+
while ((cval = getopt(argc, argv, "rlLpvs:" )) != -1) {
184+
switch (cval) {
185+
case 'v': puts("kfc 0.1.2"); break;
186+
case 'L': mode = "light"; break;
187+
case 'l': lflag++; break;
188+
case 'r': rflag++; break;
189+
case 'p': pflag++; break;
243190
case 's':
244-
if (rflag)
245-
{
246-
fprintf(stderr, "Cannot specify -r with -s\n");
247-
usage();
248-
}
249-
sval = optarg;
250-
break;
251-
case 'p':
252-
pflag++;
253-
break;
254-
case ':':
255-
fprintf(stderr, "Option -%c requires an operand\n", optopt);
256-
usage();
257-
break;
258-
case '?':
259-
fprintf(stderr, "Unrecognized option: -%c\n", optopt);
260-
usage();
261-
break;
191+
if (rflag) {
192+
fprintf(stderr, "Cannot specify -r with -s\n");
193+
return 1;
194+
}
195+
sval = optarg;
196+
break;
197+
case '?': return 1;
262198
}
263199
}
264200

265201
len = strlen(seq)
266202
+ sizeof("/")
267203
+ strlen(mode)
268204
+ (sval ? strlen(sval) + 1 : 0);
205+
269206
sel = malloc(sizeof(char) * len);
270207

271-
if (lflag)
272-
{
208+
if (lflag) {
273209
snprintf(sel, len, "%s/%s", seq, mode);
274210
list_palette(sel);
275211
}
276212

277-
if (rflag)
278-
{
213+
if (rflag) {
279214
snprintf(sel, len, "%s/%s", seq, mode);
280215
random_palette(sel);
281216
len = strlen(sel)
@@ -284,8 +219,7 @@ main(int argc, char **argv)
284219
sel = (char *)realloc(sel, sizeof(char) * len);
285220
}
286221

287-
if (sval)
288-
{
222+
if (sval) {
289223
snprintf(sel, len, "%s/%s/%s", seq, mode, sval);
290224
select_palette(sel);
291225
}

0 commit comments

Comments
 (0)