Skip to content

Commit 7befa95

Browse files
committed
refactor: move some generic util function to utils file
1 parent 69f79dd commit 7befa95

3 files changed

Lines changed: 50 additions & 40 deletions

File tree

panel/include/utils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,19 @@
44
#include <stddef.h>
55

66
void msleep(long msec);
7+
78
void padding(int count);
9+
810
char* create_padding_str(int count);
11+
912
void string_limit(char* str, size_t max_chars);
13+
1014
void debug_config(void);
15+
1116
bool includes(const char** array, int len, const char* item);
17+
1218
char* get_color(const char* color_name);
19+
20+
char* find_binary(const char* binary_name);
21+
22+
bool check_binaries(const char** binaries, size_t count);

panel/modules/arch_updates.c

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,9 @@
22

33
#include "modules/modules.h"
44
#include "utils.h"
5-
#include <linux/limits.h>
65
#include <stdio.h>
76
#include <stdlib.h>
87
#include <string.h>
9-
#include <unistd.h>
10-
11-
static inline char* find_binary(const char* binary_name)
12-
{
13-
char* paths_system = getenv("PATH");
14-
if (paths_system == NULL) {
15-
return NULL;
16-
}
17-
18-
char* paths = strdup(paths_system);
19-
20-
char* saveptr;
21-
char* path = strtok_r(paths, ":", &saveptr);
22-
while (path != NULL) {
23-
char full_path[PATH_MAX];
24-
snprintf(full_path, PATH_MAX, "%s/%s", path, binary_name);
25-
if (access(full_path, X_OK) == 0) {
26-
free(paths);
27-
return strdup(full_path);
28-
}
29-
path = strtok_r(NULL, ":", &saveptr);
30-
}
31-
32-
free(paths);
33-
return NULL;
34-
}
35-
36-
bool check_binaries(const char** binaries, size_t count)
37-
{
38-
for (size_t i = 0; i < count; i++) {
39-
char* binary_path = find_binary(binaries[i]);
40-
if (binary_path) {
41-
free(binary_path);
42-
return true;
43-
}
44-
}
45-
46-
return false;
47-
}
488

499
static inline void set_updates(ModuleState* state, const char* count_string)
5010
{

panel/src/config/utils.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#define _GNU_SOURCE
22
#include "config/utils.h"
3+
#include <linux/limits.h>
34
#include <stdio.h>
45
#include <stdlib.h>
56
#include <string.h>
7+
#include <unistd.h>
68

79
StringArray* create_string_array(void)
810
{
@@ -114,3 +116,41 @@ void string_array_from_csv(StringArray* array, const char* csv)
114116

115117
free(csv_copy);
116118
}
119+
120+
char* find_binary(const char* binary_name)
121+
{
122+
char* paths_system = getenv("PATH");
123+
if (paths_system == NULL) {
124+
return NULL;
125+
}
126+
127+
char* paths = strdup(paths_system);
128+
129+
char* saveptr;
130+
char* path = strtok_r(paths, ":", &saveptr);
131+
while (path != NULL) {
132+
char full_path[PATH_MAX];
133+
snprintf(full_path, PATH_MAX, "%s/%s", path, binary_name);
134+
if (access(full_path, X_OK) == 0) {
135+
free(paths);
136+
return strdup(full_path);
137+
}
138+
path = strtok_r(NULL, ":", &saveptr);
139+
}
140+
141+
free(paths);
142+
return NULL;
143+
}
144+
145+
bool check_binaries(const char** binaries, size_t count)
146+
{
147+
for (size_t i = 0; i < count; i++) {
148+
char* binary_path = find_binary(binaries[i]);
149+
if (binary_path) {
150+
free(binary_path);
151+
return true;
152+
}
153+
}
154+
155+
return false;
156+
}

0 commit comments

Comments
 (0)