lint(tidy): some clang-tidy work

This commit is contained in:
2025-06-10 21:43:01 +02:00
parent 6975c265f0
commit a846c1b4c9
16 changed files with 265 additions and 205 deletions

View File

@@ -19,37 +19,42 @@ static const struct source_dir sources[] = {
{WAYLAND, "/usr/share/wayland-sessions"},
};
static struct session __new_session(enum session_type type,
static struct session new_session(enum session_type type,
char* name,
const char* exec,
const char* tryexec) {
struct session __session;
__session.type = type;
strcln(&__session.name, name);
strcln(&__session.exec, exec);
strcln(&__session.tryexec, tryexec);
struct session session;
session.type = type;
strcln(&session.name, name);
strcln(&session.exec, exec);
strcln(&session.tryexec, tryexec);
return __session;
return session;
}
static struct Vector* cb_sessions = NULL;
// NOTE: commented printf's here would be nice to have debug logs if I ever
// implement it
#define LN_NAME 0b0001
#define LN_EXEC 0b0010
#define LN_TEXEC 0b0100
#define LN_ALL (LN_NAME | LN_EXEC | LN_TEXEC)
static enum session_type session_type;
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
static int fn(const char* fpath, const struct stat* sb, int typeflag) {
if (sb == NULL || !S_ISREG(sb->st_mode)) return 0;
if (!S_ISREG(sb->st_mode)) return 0;
/*printf("gonna open %s\n", fpath);*/
// printf("gonna open %s\n", fpath);
FILE* fd = fopen(fpath, "r");
if (fd == NULL) {
perror("fopen");
fprintf(stderr, "error opening file (r) '%s'\n", fpath);
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
(void)fprintf(stderr, "error opening file (r) '%s'\n", fpath);
return 0;
}
u_char found = 0;
size_t alloc_size = sb->st_blksize;
char* name_buf = NULL;
char* exec_buf = NULL;
@@ -57,38 +62,35 @@ static int fn(const char* fpath, const struct stat* sb, int typeflag) {
// This should be made a specific function
// Emm, if anything goes wrong just free the inner loop and `break;` fd and
// the rest is handled after
while (true) {
char* buf = malloc(sb->st_blksize);
ssize_t read_size = getline(&buf, &alloc_size, fd);
if (read_size == -1) {
free(buf);
break;
}
uint read;
char* buf = NULL;
size_t alloc_size = 0;
size_t read_size;
while ((read_size = getline(&buf, &alloc_size, fd)) != -1) {
char* key = malloc(read_size + sizeof(char));
if (key == NULL) {
free(buf);
// TODO: more sophisticated error handling??
break;
}
char* value = malloc(read_size + sizeof(char));
if (value == NULL) {
free(buf);
free(key);
// TODO: more sophisticated error handling??
break;
}
value[0] = '\0'; // I'm not sure if sscanf would null this string out
if ((read = sscanf(buf, "%[^=]=%[^\n]\n", key, value)) != 0) {
// NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
if (sscanf(buf, "%[^=]=%[^\n]\n", key, value) == 2) {
if (strcmp(key, "Name") == 0) {
found &= 0b001;
found &= LN_EXEC;
if(name_buf != NULL) free(name_buf);
name_buf = realloc(value, strlen(value) + sizeof(char));
} else if (strcmp(key, "Exec") == 0) {
found &= 0b010;
found &= LN_EXEC;
if(exec_buf != NULL) free(exec_buf);
exec_buf = realloc(value, strlen(value) + sizeof(char));
} else if (strcmp(key, "TryExec") == 0) {
found &= 0b100;
found &= LN_TEXEC;
if(tryexec_buf != NULL) free(tryexec_buf);
tryexec_buf = realloc(value, strlen(value) + sizeof(char));
} else {
free(value);
@@ -97,19 +99,17 @@ static int fn(const char* fpath, const struct stat* sb, int typeflag) {
free(value);
}
free(key);
free(buf);
if (found == 0b111) break;
// if (found == LN_ALL) break;
}
/*printf("\nend parsing...\n");*/
// Generic handling of exit
fclose(fd);
if(buf != NULL) free(buf);
(void)fclose(fd);
// printf("\nend parsing...\n");
// just add this to the list
if (name_buf != NULL && exec_buf != NULL) {
struct session* session_i = malloc(sizeof(struct session));
*session_i = __new_session(session_type, name_buf, exec_buf,
*session_i = new_session(session_type, name_buf, exec_buf,
tryexec_buf == NULL ? "" : tryexec_buf);
vec_push(cb_sessions, session_i);
}
@@ -122,9 +122,10 @@ static int fn(const char* fpath, const struct stat* sb, int typeflag) {
}
// This code is designed to be run purely single threaded
#define LIKELY_BOUND_SESSIONS 8
struct Vector get_avaliable_sessions() {
struct Vector sessions = vec_new();
vec_reserve(&sessions, 8);
vec_reserve(&sessions, LIKELY_BOUND_SESSIONS);
cb_sessions = &sessions;
for (size_t i = 0; i < (sizeof(sources) / sizeof(sources[0])); i++) {