summaryrefslogtreecommitdiff
path: root/src/non_critical/_tools.c
blob: 967c820c62faf6f786dd242510fc254ae7ebfc7f (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <string.h>

int check_root() {
    if(getuid() == 0) {
        return 1;
    }
    return 0;
}

int check_pid(int pid) {
    if(getpid() == pid) {
        return 1;
    }
    return 0;
}


char* get_os_name() {
    FILE* file = fopen("/etc/os-release", "r");
    if (file == NULL) {
        perror("fopen");
        return NULL;
    }

    char line[256];
    while (fgets(line, sizeof(line), file)) {
        char* name = strstr(line, "NAME=");
        if (name != NULL) {
            name += 5; // skip "NAME="
            char* end = strchr(name, '\n');
            if (end != NULL) {
                *end = '\0';
            }
            
            // Allocate memory for the result and copy the string
            char* result = malloc(strlen(name) + 1);
            if (result != NULL) {
                strcpy(result, name);
            }
            
            fclose(file);
            return result;
        }
    }

    fclose(file);
    return NULL;
}

char* get_desktop_server() {
    if(getenv("XDG_SESSION_TYPE") != NULL) { 
        return getenv("XDG_SESSION_TYPE");
    } else {
        return NULL; 
    }
}