aboutsummaryrefslogtreecommitdiff
path: root/darwin.c
blob: 2d9b3f6a15e538afcdbe9a5017e30d351e7507fb (plain) (blame)
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
#if defined(__APPLE__)
#include "common.h"

char *get_sys_product_darwin() {
    static char model[100] = {0};
    size_t len;

    len = sizeof(model);
    sysctlbyname("hw.model", model, &len, NULL, 0);
    return model;
}

ssize_t get_sys_memory() {
    size_t mem_size;
    size_t len;

    mem_size= 0;
    len = sizeof(mem_size);
    sysctlbyname("hw.memsize", &mem_size, &len, NULL, 0);

    return mem_size / 1024;
}

int get_sys_os_dist(char **name, char **version) {
    size_t version_len;
    char data[255] = {0};

    version_len = sizeof(version);
    sysctlbyname("kern.osproductversion", data, &version_len, NULL, 0);
    *name = strdup("MacOS");
    *version = strdup(data);
    return 0;
}

struct Block_Device **get_block_devices(size_t *total) {
    struct Block_Device **result;
    size_t devices_total;
    size_t count;
    struct statfs *mnt;
    int mnt_flags;

    if (!(devices_total = getmntinfo(&mnt, mnt_flags))) {
        perror("Unable to read mount points");
        return NULL;
    }

    result = calloc(devices_total + 1, sizeof(*result));
    if (!result) {
        perror("Unable to allocate device array");
        return NULL;
    }

    count = 0;
    char path_prev[PATH_MAX] = {0};
    for (size_t i = 0; i < devices_total; i++) {
        if (strncmp(mnt[i].f_mntfromname, "/dev/", 5) != 0) {
            continue;
        }
        result[count] = calloc(1, sizeof(*result[0]));
        if (!result[count]) {
            perror("Unable to allocate mount point record");
            exit(1);
        }
        char path_temp[PATH_MAX] = {0};
        size_t dnum = 0;
        sscanf(&mnt[i].f_mntfromname[5], "disk%zus%*s", &dnum);
        sprintf(path_temp, "disk%zu", dnum);
        if (strlen(path_prev) && !strcmp(path_prev, path_temp)) {
            continue;
        }
        strcpy(path_prev, path_temp);
        result[count]->path = strdup(path_temp);
        result[count]->model = strdup("Unnamed");
        result[count]->size = (mnt[i].f_bsize * mnt[i].f_blocks) / 1024;
        count++;
    }
    if (devices_total != count) {
        *total = count;
    } else {
        *total = devices_total;
    }
    return result;
}
#endif