-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.c
More file actions
185 lines (158 loc) · 5.62 KB
/
utils.c
File metadata and controls
185 lines (158 loc) · 5.62 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "utils.h"
#include "glib.h"
#include <libnm/NetworkManager.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Section Data Structures
RenderString *InitRenderString() {
RenderString *renderString = malloc(sizeof(RenderString));
if (!renderString) {
printf("LOG: Error: Could not allocate RenderString struct\n");
return NULL;
}
renderString->length = 0;
renderString->size = sizeof(char) * RENDER_LIST_SIZE;
renderString->string = (char *)malloc(renderString->size);
if (!renderString->string) {
printf("LOG: Could not allocate memory for RenderString->string\n");
free(renderString);
return NULL;
}
return renderString;
}
RenderList *InitRenderList() {
RenderList *renderList = malloc(sizeof(RenderList));
if (!renderList) {
printf("LOG: Error: Could not allocate RenderList struct\n");
return NULL;
}
renderList->size = SIZE_MAX;
renderList->length = 0;
renderList->List = malloc(sizeof(char *) * SIZE_MAX);
renderList->callbackArray = malloc(sizeof(onValidate) * MAX_ENTRIES);
if (!renderList->List || !renderList->callbackArray) {
printf("LOG: Error: Could not allocate memory for List or callbackArray\n");
free(renderList->List);
free(renderList->callbackArray);
free(renderList);
return NULL;
}
printf("LOG: Created RenderList\n");
return renderList;
}
void AddRenderEntry(char *ssid, int strength, bool active,
SessionContext *SessionContext, onValidate callback) {
char strengthBuffer[4];
if (SessionContext->RenderString->length >=
SessionContext->RenderString->size) {
printf("LOG: Buffer Overflow reallocating size\n");
int newSize = SessionContext->RenderString->size * 2;
if (newSize <= SIZE_MAX) {
char *temp = realloc(SessionContext->RenderString->string, newSize);
if (temp) {
SessionContext->RenderString->string = temp;
SessionContext->RenderString->size = newSize;
printf("LOG: Reallocated successfully size to RenderString %zu\n",
SessionContext->RenderString->size);
} else {
printf("LOG: Failed to reallocate RenderString\n");
return;
}
}
}
if (SessionContext->RenderList && callback) {
if (SessionContext->RenderList->length >=
SessionContext->RenderList->size) {
printf("LOG: Buffer Overflow reallocating size for RenderList\n");
int newSize = SessionContext->RenderList->size * 2;
if (newSize <= SIZE_MAX) {
char **temp =
realloc(SessionContext->RenderList->List, newSize * sizeof(char *));
if (temp) {
SessionContext->RenderList->List = temp;
SessionContext->RenderList->size = newSize;
printf("LOG: Reallocated successfully size to RenderList %d\n",
newSize);
} else {
printf("LOG: Failed to reallocate RenderList\n");
return;
}
}
}
SessionContext->RenderList->List[SessionContext->RenderList->length] = ssid;
SessionContext->RenderList
->callbackArray[SessionContext->RenderList->length++] = callback;
}
if (active) {
SessionContext->RenderString
->string[SessionContext->RenderString->length++] = '=';
SessionContext->RenderString
->string[SessionContext->RenderString->length++] = '=';
SessionContext->RenderString
->string[SessionContext->RenderString->length++] = ' ';
} else {
SessionContext->RenderString
->string[SessionContext->RenderString->length++] = ' ';
SessionContext->RenderString
->string[SessionContext->RenderString->length++] = ' ';
SessionContext->RenderString
->string[SessionContext->RenderString->length++] = ' ';
}
for (int i = 0; i < strlen(ssid); i++) {
SessionContext->RenderString
->string[SessionContext->RenderString->length++] = ssid[i];
}
SessionContext->RenderString->string[SessionContext->RenderString->length++] =
' ';
if (strength != -1) {
// clamp 0–100
if (strength < 0)
strength = 0;
if (strength > 100)
strength = 100;
// pick one of 5 glyphs by strength %
// 0–24 → 0 bars, 25–49 → 1 bar, 50–74 → 2 bars, 75–99 → 3 bars, 100 → 4
// bars
const char *glyph = (strength < 25) ? ""
: (strength < 50) ? ""
: (strength < 75) ? ""
: (strength < 100) ? ""
: "";
// append the UTF-8 bytes of that glyph into your render‐string
for (const char *p = glyph; *p != '\0'; ++p) {
SessionContext->RenderString
->string[SessionContext->RenderString->length++] = *p;
}
}
SessionContext->RenderString->string[SessionContext->RenderString->length++] =
'\n';
}
NMClient *CreateClient() {
NMClient *client;
// request a synchronous call to NetworkManager over ipc socket
client = nm_client_new(NULL, NULL);
if (client != NULL) {
// version info check
const char *versionInfo = nm_client_get_version(client);
printf("%s \n", versionInfo);
return client;
}
else {
printf("NetworkManager not running\n");
return NULL;
}
}
void Terminate(SessionContext *global_ctx) {
g_print("LOG: Closing IPC Connection \n");
g_print("LOG: Deallocating memory...\n");
free(global_ctx->RenderString->string);
free(global_ctx->RenderString);
free(global_ctx->RenderList->List);
free(global_ctx->RenderList->callbackArray);
free(global_ctx->RenderList);
free(global_ctx->Input);
g_print("LOG: Memory was Deallocated Successfully\n");
g_print("LOG: Closing GUI\n");
}