diff --git a/.gitea/workflows/main-build.yaml b/.gitea/workflows/main-build.yaml new file mode 100644 index 0000000..d9a5a78 --- /dev/null +++ b/.gitea/workflows/main-build.yaml @@ -0,0 +1,9 @@ +name: main-build +run-name: actor: ${{ gitea.actor }} +on: [push] + + jobs: + main-compile: + runs-on: ubuntue-latest + steps: + - run: echo "Workflow is running" diff --git a/CMakeLists.txt b/CMakeLists.txt index aac2f38..20ebf51 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,3 @@ -<<<<<<< HEAD CMAKE_MINIMUM_REQUIRED(VERSION 3.12) # Set the project name @@ -9,32 +8,15 @@ find_package(CURL REQUIRED) # add the executable ADD_EXECUTABLE(main -<<<<<<< HEAD - # OSX Files - source/osx/cpu.c - source/osx/ram.c - source/osx/disk.c - source/osx/device.c - - - # Linux Files - source/linux/cpu.c - source/linux/ram.c - source/linux/disk.c - source/linux/device.c - - # Main File source/main.c - + source/lib.c ) TARGET_LINK_LIBRARIES(main PRIVATE CURL::libcurl) TARGET_INCLUDE_DIRECTORIES(main PRIVATE source/lib/osx source/lib/linux) -======= source/main.c - source/lib/osx/cpu.c + source/lib.c ) target_include_directories(main PRIVATE source/lib/osx) ->>>>>>> 7e64275 (maocs now returns cpu name) diff --git a/compile_commands.json b/compile_commands.json deleted file mode 120000 index 25eb4b2..0000000 --- a/compile_commands.json +++ /dev/null @@ -1 +0,0 @@ -build/compile_commands.json \ No newline at end of file diff --git a/source/device.c b/source/device.c deleted file mode 100644 index 1dcc33b..0000000 --- a/source/device.c +++ /dev/null @@ -1,23 +0,0 @@ -/* - * ===================================================================================== - * - * Filename: device.c - * - * Description: - * - * Version: 1.0 - * Created: 07/17/25 23:14:35 - * Revision: none - * Compiler: gcc - * - * Author: YOUR NAME (), - * Organization: - * - * ===================================================================================== - */ -#include - -int main() { - return 0; -} - diff --git a/source/headers/cpu.h b/source/headers/cpu.h deleted file mode 100644 index c4fda8a..0000000 --- a/source/headers/cpu.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef CPU_H -#define CPU_H - -#ifdef __gnu_linux__ - - -void cpu_name(); -void cpu_freq(); -void cpu_temperature(); -void *cpu_i(void *a); - -#endif -#ifdef __APPLE__ - -void *cpu_name(); -void *cpu_threads(); -void *cpu_info(); - - -typedef struct { - - int frequency; - char* name; - int threads; - -} cpu_s; - - -#endif -#endif diff --git a/source/headers/device.h b/source/headers/device.h deleted file mode 100644 index f0d1559..0000000 --- a/source/headers/device.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef DEVICE_H -#define DEVICE_H - -#ifdef __APPLE__ - -typedef struct{ - char* name; -} device_s; - -extern device_s device_info; - -void* get_device_info(); - - -#endif -#endif diff --git a/source/headers/disk.h b/source/headers/disk.h deleted file mode 100644 index 86b7bae..0000000 --- a/source/headers/disk.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef DISK_H -#define DISK_H - -#ifdef __APPLE__ - -void* disk_size(); - -#endif - -#ifdef __gnu_linux__ -typedef struct { - long long size; - short name; -} disk_s; - -#endif -#endif diff --git a/source/headers/info.h b/source/headers/info.h deleted file mode 100644 index e69de29..0000000 diff --git a/source/headers/ram.h b/source/headers/ram.h deleted file mode 100644 index 098e8c2..0000000 --- a/source/headers/ram.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef RAM_H -#define RAM_H - -#ifdef __APPLE__ - -void* size(); -void* av_size(); -void* ram_info(); - -typedef struct { - long double total; - long double available; -} ram_s; - - -#endif -#ifdef __gnu_linux__ - -void size(); -void av_size(); -void *ram_i(void *a); - -#endif -#endif diff --git a/source/lib.c b/source/lib.c new file mode 100644 index 0000000..c9bf91a --- /dev/null +++ b/source/lib.c @@ -0,0 +1,523 @@ +#ifdef __gnu_linux__ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAXC 1024 +#define MAXC_CHAR 256 + +typedef struct { + + int frequency; + char* name; + int threads; + +} cpu_s; + +typedef struct { + + unsigned long mem_size; + +} ram; + +unsigned long get_total(void); +unsigned long get_usage(void); +long device_up_time(void); +void name(); +void temperature(); +void freq(); +void *cpu_i(void *a); +void *ram_i(void *a); +void cpu_name(void); +void cpu_temperature(unsigned short delay); +char* cpu_frequency(void); + +cpu_s _cpu; + +void *cpu_name(){ + int buffer_size = 256; + char cpu_name[buffer_size]; + + printf("Opening the CPU information files"); + FILE *fp = fopen("/proc/cpuinfo", "r"); + if (!fp) + printf("can't open /proc/cpuinfo"); + + char line[buffer_size]; + while (fgets(line, sizeof(line), fp)) + { + if (strncmp(line, "model name", 10) == 0) + { + char *colon = strchr(line, ':'); + if (colon) + { + snprintf(cpu_name, buffer_size, "%s", colon + 2); + cpu_name[strcspn(cpu_name, "\n")] = 0; + int err = fclose(fp); + if (err != 0) + printf("error closing /proc/cpuinfo"); + } + } + } + + _cpu.name = cpu_name; + + return NULL; +} + +char* cpu_frequency() { + char* buffer = malloc(MAXC_CHAR); + + FILE *fp = fopen("/proc/cpuinfo", "r"); + if (!fp) { + printf("can't open /proc/cpuinfo"); + return NULL; + } + + while (fgets(buffer, MAXC_CHAR, fp)) { + if (strstr(buffer, "cpu MHz") != NULL) + { + char *colon = strchr(buffer, ':'); + if (colon) + { + buffer[strcspn(buffer, "\n")] = 0; + snprintf(buffer, MAXC_CHAR, "%s", colon); + } + } + + fclose(fp); + return buffer; + } + return NULL; +} + +void *cpu_i(void *a){ + printf("thread is working"); + + return(NULL); +} + +unsigned long get_total(void){ + + struct sysinfo info; + + if (sysinfo(&info) != 0) { + perror("sysinfo"); + return 1; + } + + long total_ram = info.totalram * info.mem_unit; + return total_ram; + +} + +unsigned long get_usage(void) +{ + struct sysinfo info; + + if (sysinfo(&info) != 0) { + perror("sysinfo"); + return 1; + } + + long total_ram = info.totalram * info.mem_unit; + long free_ram = info.freeram * info.mem_unit; + + return total_ram - free_ram; +} + +void *ram_i(void *a){ + + printf("thread is working"); + return NULL; +} + +void cpu_name(void) +{ + int buffer_size = 256; + char cpu_name[buffer_size]; + + FILE *fp = fopen("/proc/cpuinfo", "r"); + if (!fp) + printf("can't open /proc/cpuinfo"); + + char line[buffer_size]; + while (fgets(line, sizeof(line), fp)) + { + if (strncmp(line, "model name", 10) == 0) + { + char *colon = strchr(line, ':'); + if (colon) + { + snprintf(cpu_name, buffer_size, "%s", colon + 2); + cpu_name[strcspn(cpu_name, "\n")] = 0; + int err = fclose(fp); + if (err != 0) + printf("error closing /proc/cpuinfo"); + + } + } + } + + printf("%s", cpu_name); + snprintf(cpu_name, buffer_size, "%s", cpu_name); +} + +void cpu_temperature(unsigned short delay) +{ + while (1) + { + sleep(delay); + FILE *pf = fopen("/sys/class/thermal/thermal_zone0/temp", "r"); + if (!pf) + printf("error reading /proc/cpuinfo"); + + char buffer[MAXC]; + while (fgets(buffer, sizeof(buffer), pf)) + { + int a = atoi(buffer); + a /= 1000; + printf("%dC\n", a); + fflush(stdout); + } + fclose(pf); + } +} + +char* cpu_frequency(void) { + char* buffer = malloc(MAXC_CHAR); + + FILE *fp = fopen("/proc/cpuinfo", "r"); + if (!fp) { + printf("can't open /proc/cpuinfo"); + return NULL; + } + + while (fgets(buffer, MAXC_CHAR, fp)) { + if (strstr(buffer, "cpu MHz") != NULL) + { + char *colon = strchr(buffer, ':'); + if (colon) + { + buffer[strcspn(buffer, "\n")] = 0; + snprintf(buffer, MAXC_CHAR, "%s", colon); + } + } + + fclose(fp); + return buffer; + } + return NULL; +} + +unsigned long get_total(void){ + + struct sysinfo info; + + if (sysinfo(&info) != 0) { + perror("sysinfo"); + return 1; + } + + long total_ram = info.totalram * info.mem_unit; + return total_ram; + +} + +unsigned long get_usage(void) +{ + struct sysinfo info; + + if (sysinfo(&info) != 0) { + perror("sysinfo"); + return 1; + } + + long total_ram = info.totalram * info.mem_unit; + long free_ram = info.freeram * info.mem_unit; + + return total_ram - free_ram; +} + +long device_up_time(void){ + struct sysinfo info; + if (sysinfo(&info) == -1) + perror("sysinfo"); + + return info.uptime; +} + +#ifdef __APPLE__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define CONVERT_BYTES_TO_GIGABYTES 107374182 +#define D 1073741824 + +typedef struct { + + cpu_s* cpu; + ram_s* ram; + disk_s* disk; + device_s* device; + +} info; + +typedef struct { + unsigned long mem_size; +} ram; + +void* cpu_name(); +void* cpu_thread_count(); +unsigned long get_total(void); +unsigned long get_usage(void); +long device_up_time(void); +float cpu_frequency(void); +char* cpu_name(void); +void size(); +void av_size(); +void* ram_i(); + +ram_s data; +cpu_s cpu; +device_s device_info; + +void *cpu_name() { + + char *name; + size_t len = 0; + + if (sysctlbyname("machdep.cpu.brand_string", NULL, &len, NULL, 0) < 0) + perror("errorn in assigning the size for the cpu name variable\n"); + + name = malloc(len); + + if(sysctlbyname("machdep.cpu.brand_string", name, &len, NULL, 0) < 0){ + perror("error in assigning the value to the cpu name variable\n"); + + free(name); + return NULL; + } + + cpu.name = name; + return NULL; +} + +void *cpu_threads() { + + int count; + size_t len = sizeof(count); + if (sysctlbyname("machdep.cpu.thread_count", &count, &len, NULL, 0) < 0) + perror("error in retrieving the cpu threads count\n"); + + cpu.threads = count; + return NULL; +} + +void *cpu_info() { + + cpu_threads(); + cpu_name(); + + printf("cpu name: %s\ncpu threads: %d\n", cpu.name, cpu.threads); + return NULL; +} + + +void *device_name(){ + + char *name; + size_t size = 0; + + if (sysctlbyname("kern.hostname", NULL, &size, NULL, 0) < 0) + perror("failed retrieving the hostname: \n"); + + name = malloc(size); + + if(sysctlbyname("kern.hostname", name, &size, NULL, 0) < 0){ + perror("failed retrieving the hostname: \n"); + free(name); + return NULL; + } + + device_info.name = name; + return NULL; +} + + +void *device_model(){ + + char *model_name; + size_t size = 0; + + if (sysctlbyname("hw.model", NULL, &size, NULL, 0) < 0) + perror("failed retrieving the hostname: \n"); + + model_name = malloc(size); + + if(sysctlbyname("hw.model", model_name, &size, NULL, 0) < 0){ + perror("failed retrieving the hostname: \n"); + free(model_name); + return NULL; + } + + device_info.model = model_name; + return NULL; + +} + +void *device_up_time() +{ + return NULL; +} + +void *device_os_version() { + + char *os_version; + size_t size = 0; + + if (sysctlbyname("kern.ostype", NULL, &size, NULL, 0) < 0) + perror("failed retrieving the hostname: \n"); + + os_version = malloc(size); + + if(sysctlbyname("kern.ostype", os_version, &size, NULL, 0) < 0){ + perror("failed retrieving the os version: \n"); + free(os_version); + return NULL; + } + + device_info.model = os_version; + return NULL; + + +} + +void *get_device_info() { + device_name(); + device_model(); + printf("device name: %s\ndevice model: %s\n", device_info.name, device_info.model); + return NULL; +} + +void size() { + int64_t size; + size_t len = sizeof(size); + if (sysctlbyname("hw.memsize", &size, &len, NULL, 0) < 0) + perror("error in retrieving the memory size"); + + data.total = size / D; + return NULL; +} + +void av_size() { + int64_t size; + size_t len = sizeof(size); + if (sysctlbyname("hw.memsize_usable", &size, &len, NULL, 0) < 0) + perror("error in retrieving the available memory size"); + + data.available = size / D; + return NULL; +} + +void ram_info() { + size(); + av_size(); + + printf("available ram: %LF\n", data.available); + printf("total ram: %LF\n", data.total); + + return NULL; +} + +void* ram_i(){ + + printf("thread is working"); + return NULL; +} + + +float cpu_frequency(void){ + uint64_t freq = 0; + size_t size = sizeof(freq); + + if (sysctlbyname("hw.cpufrequency", &freq, &size, NULL, 0) < 0) + { + perror("sysctl"); + } + return freq; +} + +char* cpu_name(void){ + + size_t size = 0; + + if (sysctlbyname("machdep.cpu.brand_string", NULL, &size, NULL, 0) < 0) + perror("sysctl"); + + + char *name = malloc(size); + + if(sysctlbyname("machdep.cpu.brand_string", &name, &size, NULL, 0) < 0){ + perror("sysctl"); + free(name); + return NULL; + } + + return name; +} + +long device_up_time(void){ + + struct timeval boottime; + size_t len = sizeof(boottime); + + if (sysctlbyname("kern.boottime", &boottime, len, NULL, 0) == -1){ + perror("sysctl error"); + return 1; + } +} + +unsigned long get_usage(void) { + + struct rusage usage; + if(0 == getrusage(RUSAGE_SELF, &usage)) + return usage.ru_maxrss / CONVERT_BYTES_TO_GIGABYTES ; + else + return 0; +} + +unsigned long get_mem(void){ + + int mib[2]; + size_t size; + uint64_t ram_size; + + mib[0] = CTL_HW; + mib[1] = HW_MEMSIZE; + + size = sizeof(ram_size); + if (sysctl(mib, 2, &ram_size, &size, NULL, 0) == -1) { + perror("sysctl"); + } + + return ram_size; + +} + +#endif diff --git a/source/lib.h b/source/lib.h new file mode 100644 index 0000000..4a72103 --- /dev/null +++ b/source/lib.h @@ -0,0 +1,59 @@ +#ifndef LIB_H +#define LIB_H + +#ifdef __APPLE__ + +typedef struct{ + char* name; +} device_s; + +typedef struct { + + int frequency; + char* name; + int threads; + +} cpu_s; + +typedef struct { + long double total; + long double available; +} ram_s; + +typedef struct { + long long size; + short name; +} disk_s; + +extern device_s device_info; + +void* get_device_info(); +void* disk_size(); +void* size(); +void* av_size(); +void* ram_info(); + + +float cpu_frequency(void); +char* cpu_name(void); +void *cpu_name(); +void *cpu_threads(); +void *cpu_info(); + +#endif + +#ifdef __gnu_linux__ + +void cpu_name(); +void cpu_freq(); +void cpu_temperature(); +void *cpu_i(void *a); +void size(); +void av_size(); +void *ram_i(void *a); +float cpu_freq(void); +char* cpu_name(void); +void cpu_temperature(void); + +#endif + diff --git a/source/lib/linux/cpu.c b/source/lib/linux/cpu.c deleted file mode 100644 index fde8b66..0000000 --- a/source/lib/linux/cpu.c +++ /dev/null @@ -1,97 +0,0 @@ -#ifdef __gnu_linux__ - -#include -#include -#include -#include - -#include -#include -#include - -#define MAXC 1024 -#define MAXC_CHAR 256 - -void cpu_name(void); -void cpu_temperature(unsigned short delay); -char* cpu_frequency(void); - -void cpu_name(void) -{ - int buffer_size = 256; - char cpu_name[buffer_size]; - - FILE *fp = fopen("/proc/cpuinfo", "r"); - if (!fp) - printf("can't open /proc/cpuinfo"); - - char line[buffer_size]; - while (fgets(line, sizeof(line), fp)) - { - if (strncmp(line, "model name", 10) == 0) - { - char *colon = strchr(line, ':'); - if (colon) - { - snprintf(cpu_name, buffer_size, "%s", colon + 2); - cpu_name[strcspn(cpu_name, "\n")] = 0; - int err = fclose(fp); - if (err != 0) - printf("error closing /proc/cpuinfo"); - - } - } - } - - printf("%s", cpu_name); - snprintf(cpu_name, buffer_size, "%s", cpu_name); -} - -void cpu_temperature(unsigned short delay) -{ - while (1) - { - sleep(delay); - FILE *pf = fopen("/sys/class/thermal/thermal_zone0/temp", "r"); - if (!pf) - printf("error reading /proc/cpuinfo"); - - char buffer[MAXC]; - while (fgets(buffer, sizeof(buffer), pf)) - { - int a = atoi(buffer); - a /= 1000; - printf("%dC\n", a); - fflush(stdout); - } - fclose(pf); - } -} - -char* cpu_frequency(void) { - char* buffer = malloc(MAXC_CHAR); - - FILE *fp = fopen("/proc/cpuinfo", "r"); - if (!fp) { - printf("can't open /proc/cpuinfo"); - return NULL; - } - - while (fgets(buffer, MAXC_CHAR, fp)) { - if (strstr(buffer, "cpu MHz") != NULL) - { - char *colon = strchr(buffer, ':'); - if (colon) - { - buffer[strcspn(buffer, "\n")] = 0; - snprintf(buffer, MAXC_CHAR, "%s", colon); - } - } - - fclose(fp); - return buffer; - } - return NULL; -} - -#endif diff --git a/source/lib/linux/cpu.h b/source/lib/linux/cpu.h deleted file mode 100644 index a402cc2..0000000 --- a/source/lib/linux/cpu.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef CPU_H -#define CPU_H - -#ifdef __gnu_linux__ - -float cpu_freq(void); -char* cpu_name(void); -void cpu_temperature(void); - -#endif -#endif diff --git a/source/lib/linux/device.h b/source/lib/linux/device.h deleted file mode 100644 index 7569984..0000000 --- a/source/lib/linux/device.h +++ /dev/null @@ -1,72 +0,0 @@ -/* - * ===================================================================================== - * - * Filename: general.c - * - * Description: Retrieving basic information about the device - * - * Version: 1.0 - * Created: 05/08/2025 22:22:00 - * Revision: none - * Compiler: clang - * - * Author: nasr, - * Organization: synf - * - * ===================================================================================== */ - -// OSX -#ifdef __APPLE__ - -#include -#include -#include - -long device_up_time(void); - -int main(int argc, char** argv) -{ - - return 0; -} - -long device_up_time(void){ - - struct timeval boottime; - size_t len = sizeof(boottime); - - if (sysctlbyname("kern.boottime", &boottime, len, NULL, 0) == -1){ - perror("sysctl error"); - return 1; - } -} - -#endif - -// GNU LINUX -#ifdef __gnu_linux__ - -#include -#include -#include - -long device_up_time(void); - -int main(int argc, char** argv) -{ - printf("The total uptime is (seconds): %lu", device_up_time()); - return 0; -} - -long device_up_time(void) -{ - struct sysinfo info; - if (sysinfo(&info) == -1) - perror("sysinfo"); - - return info.uptime; -} - -#endif - - diff --git a/source/lib/linux/disk.c b/source/lib/linux/disk.c deleted file mode 100644 index aeb83bb..0000000 --- a/source/lib/linux/disk.c +++ /dev/null @@ -1,82 +0,0 @@ -/* - * ===================================================================================== - * - * Filename: disk.c - * - * Description: retrieving disk information from the device - * - * Version: 1.0 - * Created: 04/08/2025 01:33:30 - * Revision: none - * Compiler: gcc - * - * Author: nasr - * Organization: synf - * - * ===================================================================================== - */ - -#include -#include -#include -#include - -<<<<<<< HEAD:source/lib/linux/disk.c - -typedef struct { - long size; -======= -void disk_size(); - - -typedef struct { - long total_disk_size; ->>>>>>> 1095ca1 (added the device file):source/disk.c -} disk; - - -void get_disk() -{ - CFMutableDictionaryRef match_dict = IOServiceMatching("IOMedia"); - if (!match_dict) { - printf("failed to create match directory"); - } - - CFDictionarySetValue(match_dict, CFSTR("Whole"), kCFBooleanTrue); - - io_iterator_t iter; - kern_return_t kr = IOServiceGetMatchingServices(kIOMainPortDefault, match_dict, &iter); - if (kr != KERN_SUCCESS) { - printf("Error matching services"); - } - - io_object_t service; - while ((service = IOIteratorNext(iter))) { - CFStringRef bsdName = IORegistryEntryCreateCFProperty(service, CFSTR("BSD Name"), kCFAllocatorDefault, 0); - if (bsdName) { - char name[1024]; - CFStringGetCString(bsdName, name, sizeof(name), kCFStringEncodingUTF8); - printf("Disk: /dev/%s\n", name); - CFRelease(bsdName); - } - - CFNumberRef sizeRef = IORegistryEntryCreateCFProperty(service, CFSTR("Size"), kCFAllocatorDefault, 0); - if (sizeRef) { - long long size = 0; - CFNumberGetValue(sizeRef, kCFNumberLongLongType, &size); - printf(" Size: %lld bytes\n", size); - CFRelease(sizeRef); - } - - IOObjectRelease(service); - } - - IOObjectRelease(iter); -} - - -int main(int argc, char** argv) -{ - return 0; -} - diff --git a/source/lib/linux/disk.h b/source/lib/linux/disk.h deleted file mode 100644 index e69de29..0000000 diff --git a/source/lib/linux/ram.c b/source/lib/linux/ram.c deleted file mode 100644 index fa54ddb..0000000 --- a/source/lib/linux/ram.c +++ /dev/null @@ -1,145 +0,0 @@ -/* - * ===================================================================================== - * - * Filename: ram.c - * - * Description: retrieve ram information from the device - * - * Version: 1.0 - * Created: 04/08/2025 01:34:33 - * Revision: none - * Compiler: gcc - * - * Author: nasr - * Organization: synf - * - * =====================================================================================*/ - -// OSX -#ifdef __APPLE__ - -#include -#include -#include -#include -#include - -#define CONVERT_BYTES_TO_GIGABYTES 107374182 - -typedef struct { - unsigned long mem_size; -} ram; - -unsigned long get_total(void); -unsigned long get_usage(void); - - -unsigned long get_usage(void) -{ - - struct rusage usage; - if(0 == getrusage(RUSAGE_SELF, &usage)) - return usage.ru_maxrss / CONVERT_BYTES_TO_GIGABYTES ; - else - return 0; -} - -unsigned long get_mem(void){ - - int mib[2]; - size_t size; - uint64_t ram_size; - - mib[0] = CTL_HW; - mib[1] = HW_MEMSIZE; - - size = sizeof(ram_size); - if (sysctl(mib, 2, &ram_size, &size, NULL, 0) == -1) { - perror("sysctl"); - } - - return ram_size; - -} - -int main() -{ - printf("%lu", get_mem()); - return 0; -} - - -#endif - -#ifdef __gnu_linux__ - -#include -#include - -typedef struct -{ - unsigned long mem_size; -} ram; - -unsigned long get_total(void); -unsigned long get_usage(void); - - -int main() -{ - printf("compiled for __gnu_linux__"); - if (argc > 1) - { - if (strcmp(argv[1], "total") == 0) - { - printf("Get the total ram usage"); - while (1) { - - sleep(1); - printf("%s", get_total()); - } - } - - else if (strcmp(argv[1], "name") == 0) - { - printf("Get the ram usage"); - printf("%lu", get_usage()); - } - } - else - printf("no arguments passed, try again with : frequency, temperature or name"); - return 0; - return 0; -} - -unsigned long get_total(void){ - - struct sysinfo info; - - if (sysinfo(&info) != 0) { - perror("sysinfo"); - return 1; - } - - long total_ram = info.totalram * info.mem_unit; - return total_ram; - -} - -unsigned long get_usage(void) -{ - struct sysinfo info; - - if (sysinfo(&info) != 0) { - perror("sysinfo"); - return 1; - } - - long total_ram = info.totalram * info.mem_unit; - long free_ram = info.freeram * info.mem_unit; - - return total_ram - free_ram; - } - -#endif - diff --git a/source/lib/linux/ram.h b/source/lib/linux/ram.h deleted file mode 100644 index e69de29..0000000 diff --git a/source/lib/osx/cpu.c b/source/lib/osx/cpu.c deleted file mode 100644 index 0a49e14..0000000 --- a/source/lib/osx/cpu.c +++ /dev/null @@ -1,49 +0,0 @@ -#ifdef __APPLE__ - -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -float cpu_frequency(void); -char* cpu_name(void); - -float cpu_frequency(void) -{ - uint64_t freq = 0; - size_t size = sizeof(freq); - - if (sysctlbyname("hw.cpufrequency", &freq, &size, NULL, 0) < 0) - { - perror("sysctl"); - } - return freq; -} - -char* cpu_name(void){ - - size_t size = 0; - - if (sysctlbyname("machdep.cpu.brand_string", NULL, &size, NULL, 0) < 0) - perror("sysctl"); - - - char *name = malloc(size); - - if(sysctlbyname("machdep.cpu.brand_string", &name, &size, NULL, 0) < 0){ - perror("sysctl"); - free(name); - return NULL; - } - - return name; -} - -#endif - diff --git a/source/lib/osx/cpu.h b/source/lib/osx/cpu.h deleted file mode 100644 index 0845a56..0000000 --- a/source/lib/osx/cpu.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef CPU_H -#define CPU_H - -#ifdef __APPLE__ - -float cpu_frequency(void); -char* cpu_name(void); - -#endif -#endif diff --git a/source/lib/osx/device.c b/source/lib/osx/device.c deleted file mode 100644 index e69de29..0000000 diff --git a/source/lib/osx/device.h b/source/lib/osx/device.h deleted file mode 100644 index e69de29..0000000 diff --git a/source/lib/osx/disk.c b/source/lib/osx/disk.c deleted file mode 100644 index e69de29..0000000 diff --git a/source/lib/osx/disk.h b/source/lib/osx/disk.h deleted file mode 100644 index e69de29..0000000 diff --git a/source/lib/osx/ram.c b/source/lib/osx/ram.c deleted file mode 100644 index e69de29..0000000 diff --git a/source/lib/osx/ram.h b/source/lib/osx/ram.h deleted file mode 100644 index e69de29..0000000 diff --git a/source/lib/windows/cpu.c b/source/lib/windows/cpu.c deleted file mode 100644 index e69de29..0000000 diff --git a/source/lib/windows/cpu.h b/source/lib/windows/cpu.h deleted file mode 100644 index e69de29..0000000 diff --git a/source/lib/windows/device.c b/source/lib/windows/device.c deleted file mode 100644 index e69de29..0000000 diff --git a/source/lib/windows/device.h b/source/lib/windows/device.h deleted file mode 100644 index e69de29..0000000 diff --git a/source/lib/windows/disk.c b/source/lib/windows/disk.c deleted file mode 100644 index e69de29..0000000 diff --git a/source/lib/windows/disk.h b/source/lib/windows/disk.h deleted file mode 100644 index e69de29..0000000 diff --git a/source/lib/windows/ram.c b/source/lib/windows/ram.c deleted file mode 100644 index e69de29..0000000 diff --git a/source/lib/windows/ram.h b/source/lib/windows/ram.h deleted file mode 100644 index e69de29..0000000 diff --git a/source/lib_linux.c b/source/lib_linux.c deleted file mode 100644 index 3ffd937..0000000 --- a/source/lib_linux.c +++ /dev/null @@ -1,255 +0,0 @@ -#ifdef __gnu_linux__ - -#include -#include -#include -#include -#include -#include -#include -#include - -#define MAXC 1024 -#define MAXC_CHAR 256 - -typedef struct { - - int frequency; - char* name; - int threads; - -} cpu_s; - -typedef struct { - - unsigned long mem_size; - -} ram; - -unsigned long get_total(void); -unsigned long get_usage(void); -long device_up_time(void); -void name(); -void temperature(); -void freq(); -void *cpu_i(void *a); -void *ram_i(void *a); -void cpu_name(void); -void cpu_temperature(unsigned short delay); -char* cpu_frequency(void); - -cpu_s _cpu; - -void *cpu_name(){ - int buffer_size = 256; - char cpu_name[buffer_size]; - - printf("Opening the CPU information files"); - FILE *fp = fopen("/proc/cpuinfo", "r"); - if (!fp) - printf("can't open /proc/cpuinfo"); - - char line[buffer_size]; - while (fgets(line, sizeof(line), fp)) - { - if (strncmp(line, "model name", 10) == 0) - { - char *colon = strchr(line, ':'); - if (colon) - { - snprintf(cpu_name, buffer_size, "%s", colon + 2); - cpu_name[strcspn(cpu_name, "\n")] = 0; - int err = fclose(fp); - if (err != 0) - printf("error closing /proc/cpuinfo"); - } - } - } - - _cpu.name = cpu_name; - - return NULL; -} - -char* cpu_frequency() { - char* buffer = malloc(MAXC_CHAR); - - FILE *fp = fopen("/proc/cpuinfo", "r"); - if (!fp) { - printf("can't open /proc/cpuinfo"); - return NULL; - } - - while (fgets(buffer, MAXC_CHAR, fp)) { - if (strstr(buffer, "cpu MHz") != NULL) - { - char *colon = strchr(buffer, ':'); - if (colon) - { - buffer[strcspn(buffer, "\n")] = 0; - snprintf(buffer, MAXC_CHAR, "%s", colon); - } - } - - fclose(fp); - return buffer; - } - return NULL; -} - -void *cpu_i(void *a){ - printf("thread is working"); - - return(NULL); -} - -unsigned long get_total(void){ - - struct sysinfo info; - - if (sysinfo(&info) != 0) { - perror("sysinfo"); - return 1; - } - - long total_ram = info.totalram * info.mem_unit; - return total_ram; - -} - -unsigned long get_usage(void) -{ - struct sysinfo info; - - if (sysinfo(&info) != 0) { - perror("sysinfo"); - return 1; - } - - long total_ram = info.totalram * info.mem_unit; - long free_ram = info.freeram * info.mem_unit; - - return total_ram - free_ram; -} - -void *ram_i(void *a){ - - printf("thread is working"); - return NULL; -} - -void cpu_name(void) -{ - int buffer_size = 256; - char cpu_name[buffer_size]; - - FILE *fp = fopen("/proc/cpuinfo", "r"); - if (!fp) - printf("can't open /proc/cpuinfo"); - - char line[buffer_size]; - while (fgets(line, sizeof(line), fp)) - { - if (strncmp(line, "model name", 10) == 0) - { - char *colon = strchr(line, ':'); - if (colon) - { - snprintf(cpu_name, buffer_size, "%s", colon + 2); - cpu_name[strcspn(cpu_name, "\n")] = 0; - int err = fclose(fp); - if (err != 0) - printf("error closing /proc/cpuinfo"); - - } - } - } - - printf("%s", cpu_name); - snprintf(cpu_name, buffer_size, "%s", cpu_name); -} - -void cpu_temperature(unsigned short delay) -{ - while (1) - { - sleep(delay); - FILE *pf = fopen("/sys/class/thermal/thermal_zone0/temp", "r"); - if (!pf) - printf("error reading /proc/cpuinfo"); - - char buffer[MAXC]; - while (fgets(buffer, sizeof(buffer), pf)) - { - int a = atoi(buffer); - a /= 1000; - printf("%dC\n", a); - fflush(stdout); - } - fclose(pf); - } -} - -char* cpu_frequency(void) { - char* buffer = malloc(MAXC_CHAR); - - FILE *fp = fopen("/proc/cpuinfo", "r"); - if (!fp) { - printf("can't open /proc/cpuinfo"); - return NULL; - } - - while (fgets(buffer, MAXC_CHAR, fp)) { - if (strstr(buffer, "cpu MHz") != NULL) - { - char *colon = strchr(buffer, ':'); - if (colon) - { - buffer[strcspn(buffer, "\n")] = 0; - snprintf(buffer, MAXC_CHAR, "%s", colon); - } - } - - fclose(fp); - return buffer; - } - return NULL; -} - -unsigned long get_total(void){ - - struct sysinfo info; - - if (sysinfo(&info) != 0) { - perror("sysinfo"); - return 1; - } - - long total_ram = info.totalram * info.mem_unit; - return total_ram; - -} - -unsigned long get_usage(void) -{ - struct sysinfo info; - - if (sysinfo(&info) != 0) { - perror("sysinfo"); - return 1; - } - - long total_ram = info.totalram * info.mem_unit; - long free_ram = info.freeram * info.mem_unit; - - return total_ram - free_ram; -} - -long device_up_time(void){ - struct sysinfo info; - if (sysinfo(&info) == -1) - perror("sysinfo"); - - return info.uptime; -} - diff --git a/source/linux/lib.c b/source/linux/lib.c deleted file mode 100644 index 9c1e3ab..0000000 --- a/source/linux/lib.c +++ /dev/null @@ -1,173 +0,0 @@ -#ifdef __gnu_linux__ - -#include -#include -#include -#include -#include -#include -#include -#include - -#define MAXC 1024 -#define MAXC_CHAR 256 - -typedef struct { - - int frequency; - char* name; - int threads; - -} cpu_s; - -void name(); -void temperature(); -void freq(); -void *cpu_i(void *a); - -cpu_s _cpu; - -void* cpu_name() -{ - int buffer_size = 256; - char cpu_name[buffer_size]; - - printf("Opening the CPU information files"); - FILE *fp = fopen("/proc/cpuinfo", "r"); - if (!fp) - printf("can't open /proc/cpuinfo"); - - char line[buffer_size]; - while (fgets(line, sizeof(line), fp)) - { - if (strncmp(line, "model name", 10) == 0) - { - char *colon = strchr(line, ':'); - if (colon) - { - snprintf(cpu_name, buffer_size, "%s", colon + 2); - cpu_name[strcspn(cpu_name, "\n")] = 0; - int err = fclose(fp); - if (err != 0) - printf("error closing /proc/cpuinfo"); - } - } - } - - _cpu.name = cpu_name; - - return NULL; -} - -void cpu_temperature(bool on, int delay){ - - while (1) - { - sleep(delay); - FILE *pf = fopen("/sys/class/thermal/thermal_zone0/temp", "r"); - if (!pf) - printf("error reading /proc/cpuinfo"); - - char buffer[MAXC]; - while (fgets(buffer, sizeof(buffer), pf)) - { - int a = atoi(buffer); - a /= 1000; - printf("%dC\n", a); - fflush(stdout); - } - fclose(pf); - } -} - -char* cpu_frequency() { - char* buffer = malloc(MAXC_CHAR); - - FILE *fp = fopen("/proc/cpuinfo", "r"); - if (!fp) { - printf("can't open /proc/cpuinfo"); - return NULL; - } - - while (fgets(buffer, MAXC_CHAR, fp)) { - if (strstr(buffer, "cpu MHz") != NULL) - { - char *colon = strchr(buffer, ':'); - if (colon) - { - buffer[strcspn(buffer, "\n")] = 0; - snprintf(buffer, MAXC_CHAR, "%s", colon); - } - } - - fclose(fp); - return buffer; - } - return NULL; -} - -void *cpu_i(void *a){ - printf("thread is working"); - - return(NULL); -} - -#endif -#ifdef __gnu_linux__ - -#include -#include -#include -#include -#include -#include - - -typedef struct -{ - unsigned long mem_size; -} ram; - -unsigned long get_total(void); -unsigned long get_usage(void); -void *ram_i(void *a); - - -unsigned long get_total(void){ - - struct sysinfo info; - - if (sysinfo(&info) != 0) { - perror("sysinfo"); - return 1; - } - - long total_ram = info.totalram * info.mem_unit; - return total_ram; - -} - -unsigned long get_usage(void) -{ - struct sysinfo info; - - if (sysinfo(&info) != 0) { - perror("sysinfo"); - return 1; - } - - long total_ram = info.totalram * info.mem_unit; - long free_ram = info.freeram * info.mem_unit; - - return total_ram - free_ram; - } - -void *ram_i(void *a){ - - printf("thread is working"); - return NULL; -} - - - -#endif diff --git a/source/main.c b/source/main.c index 521e721..83b3408 100644 --- a/source/main.c +++ b/source/main.c @@ -1,80 +1,78 @@ #include -#include "headers/cpu.h" -#include "headers/ram.h" -#include "headers/disk.h" -#include "headers/device.h" #include #include +#include +#include +#include + +cJSON convert_to_json(info info) { + + cJSON *root = cJSON_CreateObject(); + cJSON_AddStringToObject(root, "cpu", info.cpu->name); + cJSON_AddNumberToObject(root, "ram", info.ram->total); + cJSON_AddStringToObject(root, "disk", info.disk->name); + cJSON_AddStringToObject(root, "device", info.device->name); + + return *root; +} void handler(char *url, cJSON object) { - CURL *curl; - CURLcode res; + CURL *curl; + CURLcode res; - curl_global_init(CURL_GLOBAL_ALL); - curl = curl_easy_init(); + curl_global_init(CURL_GLOBAL_ALL); + curl = curl_easy_init(); - if (curl) { - curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, object); + if (curl) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, object); - res = curl_easy_perform(curl); - if (res != CURLE_OK) - fprintf(stderr, "curl_easy_perform() failed: %s\n", - curl_easy_strerror(res)); + res = curl_easy_perform(curl); + if (res != CURLE_OK) + fprintf(stderr, "curl_easy_perform() failed: %s\n", + curl_easy_strerror(res)); - curl_easy_cleanup(curl); - } - curl_global_cleanup(); + curl_easy_cleanup(curl); + } + curl_global_cleanup(); } void setup_mt() { - pthread_t cpu_t,ram_t; // disk_t , device_t; + pthread_t cpu_t,ram_t; // disk_t , device_t; - pthread_create(&cpu_t, NULL, cpu_i, NULL); - pthread_create(&ram_t, NULL, ram_i, NULL); -// pthread_create(&disk_t, NULL, disk_i, NULL); -// pthread_create(&device_t, NULL, device_i, NULL); + pthread_create(&cpu_t, NULL, cpu_i, NULL); + pthread_create(&ram_t, NULL, ram_i, NULL); + // pthread_create(&disk_t, NULL, disk_i, NULL); + // pthread_create(&device_t, NULL, device_i, NULL); - pthread_join(cpu_t, NULL); - pthread_join(ram_t, NULL); - // pthread_join(disk_t, NULL); - // pthread_join(device_t, NULL); + pthread_join(cpu_t, NULL); + pthread_join(ram_t, NULL); + // pthread_join(disk_t, NULL); + // pthread_join(device_t, NULL); } #ifdef __APPLE__ int main(int argc, char** argv) { - setup_mt(); - handler("api.nsrddyn.com"); + setup_mt(); + handler("api.nsrddyn.com"); - return 0; + return 0; } #endif #ifdef __gnu_linux -void setup_mt(){ - pthread_t cpu; - pthread_t ram; - pthread_t disk; - pthread_t device; - - pthread_create(&cpu, NULL, cpu_i, NULL); - pthread_create(&ram, NULL, ram_i, NULL); - pthread_create(&disk, NULL, disk_i, NULL); - pthread_create(&device, NULL, device_i, NULL); - -} int main(int argc, char** argv) { - setup_mt(); + setup_mt(); - return 0; + return 0; } #endif diff --git a/source/osx/lib.c b/source/osx/lib.c deleted file mode 100644 index d083a04..0000000 --- a/source/osx/lib.c +++ /dev/null @@ -1,238 +0,0 @@ - -#ifdef __APPLE__ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "cpu.h" - -void* cpu_name(); -void* cpu_thread_count(); - -cpu_s cpu; - -void* cpu_name(){ - - char *name; - size_t len = 0; - - if (sysctlbyname("machdep.cpu.brand_string", NULL, &len, NULL, 0) < 0) - perror("errorn in assigning the size for the cpu name variable\n"); - - name = malloc(len); - - if(sysctlbyname("machdep.cpu.brand_string", name, &len, NULL, 0) < 0){ - perror("error in assigning the value to the cpu name variable\n"); - - free(name); - return NULL; - } - - cpu.name = name; - return NULL; -} - -void* cpu_threads(){ - - int count; - size_t len = sizeof(count); - if (sysctlbyname("machdep.cpu.thread_count", &count, &len, NULL, 0) < 0) - perror("error in retrieving the cpu threads count\n"); - - cpu.threads = count; - return NULL; -} - -void* cpu_info(){ - cpu_threads(); - cpu_name(); - - printf("cpu name: %s\ncpu threads: %d\n", cpu.name, cpu.threads); - return NULL; -} - -#endif - -#ifdef __APPLE__ - -#include -#include -#include -#include -#include - -device_s device_info; - -void* device_name(){ - - char *name; - size_t size = 0; - - if (sysctlbyname("kern.hostname", NULL, &size, NULL, 0) < 0) - perror("failed retrieving the hostname: \n"); - - name = malloc(size); - - if(sysctlbyname("kern.hostname", name, &size, NULL, 0) < 0){ - perror("failed retrieving the hostname: \n"); - free(name); - return NULL; - } - - device_info.name = name; - return NULL; -} - - -void* device_model(){ - - char *model_name; - size_t size = 0; - - if (sysctlbyname("hw.model", NULL, &size, NULL, 0) < 0) - perror("failed retrieving the hostname: \n"); - - model_name = malloc(size); - - if(sysctlbyname("hw.model", model_name, &size, NULL, 0) < 0){ - perror("failed retrieving the hostname: \n"); - free(model_name); - return NULL; - } - - device_info.model = model_name; - return NULL; - -} - -void* device_up_time(){ - - - - return NULL; -} - -void* device_os_version(){ - - char *os_version; - size_t size = 0; - - if (sysctlbyname("kern.ostype", NULL, &size, NULL, 0) < 0) - perror("failed retrieving the hostname: \n"); - - os_version = malloc(size); - - if(sysctlbyname("kern.ostype", os_version, &size, NULL, 0) < 0){ - perror("failed retrieving the os version: \n"); - free(os_version); - return NULL; - } - - device_info.model = os_version; - return NULL; - - -} - -void* get_device_info(){ - device_name(); - device_model(); - printf("device name: %s\ndevice model: %s\n", device_info.name, device_info.model); - return NULL; -} - -#endif - - -#include -#include "modules/cpu.h" -#include "modules/ram.h" -#include "modules/disk.h" -#include "modules/device.h" -#include - -typedef struct { - - cpu_s* cpu; - ram_s* ram; - disk_s* disk; - device_s* device; - -} info; - - -cJSON convert_to_json(info info){ - - cJSON *root = cJSON_CreateObject(); - cJSON_AddStringToObject(root, "cpu", info.cpu->name); - cJSON_AddNumberToObject(root, "ram", info.ram->total); - cJSON_AddStringToObject(root, "disk", info.disk->name); - cJSON_AddStringToObject(root, "device", info.device->name); - - return *root; -} - - -#ifdef __APPLE__ - -#include -#include -#include "ram.h" -#include -#include -#include -#include - -void size(); -void av_size(); -void* ram_i(); - - -ram_s data; - -#define D 1073741824 - - -void size() { - int64_t size; - size_t len = sizeof(size); - if (sysctlbyname("hw.memsize", &size, &len, NULL, 0) < 0) - perror("error in retrieving the memory size"); - - data.total = size / D; - return NULL; -} - -void av_size() { - int64_t size; - size_t len = sizeof(size); - if (sysctlbyname("hw.memsize_usable", &size, &len, NULL, 0) < 0) - perror("error in retrieving the available memory size"); - - data.available = size / D; - return NULL; -} - -void ram_info() { - size(); - av_size(); - - printf("available ram: %LF\n", data.available); - printf("total ram: %LF\n", data.total); - - return NULL; -} - -void* ram_i(){ - - printf("thread is working"); - return NULL; -} - -#endif