diff --git a/build/cpu b/build/cpu new file mode 100755 index 0000000..ba6063d Binary files /dev/null and b/build/cpu differ diff --git a/build/cpu_linux b/build/cpu_linux new file mode 100755 index 0000000..3fef48b Binary files /dev/null and b/build/cpu_linux differ diff --git a/build/disk b/build/disk new file mode 100755 index 0000000..16d6264 Binary files /dev/null and b/build/disk differ diff --git a/build/general b/build/general new file mode 100755 index 0000000..2657e2c Binary files /dev/null and b/build/general differ diff --git a/build/ram b/build/ram new file mode 100755 index 0000000..b70dc6d Binary files /dev/null and b/build/ram differ diff --git a/source/cpu.c b/source/cpu.c new file mode 100644 index 0000000..e5ff8c4 --- /dev/null +++ b/source/cpu.c @@ -0,0 +1,209 @@ +/* + * ===================================================================================== + * + * Filename: cpuc.c + * + * Description: Retrieving cpu information from device + * + * Version: 1.0 + * Created: 04/08/2025 01:00:21 + * Revision: none + * Compiler: gcc + * + * Author: nasr, + * Organization: synf + * + * ===================================================================================== */ + + +// OSX +#ifdef __APPLE__ + +#include +#include +#include +#include + +#include +#include +#include +#include + +uint64_t get_cpu_freq(void) +{ + uint64_t freq = 0; + size_t size = sizeof(freq); + + if (sysctlbyname("hw.cpufrequency", &freq, &size, NULL, 0) < 0) + { + perror("sysctl"); + } + return freq; +} + +char* get_cpu_name(void) +{ + char* cpu_name; + size_t size = sizeof(cpu_name); + if (sysctlbyname("ker.hostname", &cpu_name, &size, NULL, 0) < 0) + perror("sysctl"); + + return cpu_name; +} + +void get_cpu_temperature(void) +{ + uint16_t cpu_temperature; + size_t size = sizeof(cpu_temperature); + +} + +int main(int argc, char **argv) +{ + + printf("compiled for __APPLE__"); + if (argc > 1) + { + if (strcmp(argv[1], "frequency") == 0) + printf("argument received"); + } + printf("%llu", get_cpu_freq()); + return 0; +} + +#endif + +// GNU/Linux +#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); + +int main(int argc, char **argv) +{ + printf("compiled for __gnu_linux__"); + if (argc > 1) + { + if (strcmp(argv[1], "frequency") == 0) + { + printf("starting the process of getting the CPU frequency\n"); + while (1) { + + sleep(1); + printf("%s", cpu_frequency()); + } + } + + else if (strcmp(argv[1], "name") == 0) + { + printf("starting the process of getting the CPU name\n"); + cpu_name(); + } + else if (strcmp(argv[1], "temperature") == 0) + { + printf("CPU temperature:\n"); + cpu_temperature(1); + } + } + else + printf("no arguments passed, try again with : frequency, temperature or name"); + return 0; +} + +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); + // dont know what the snprintf is doing here but removing it gives a segmentation fault + // so im keeping it here :) +} + +void cpu_temperature(unsigned short delay) +{ + while (1) + { + sleep(delay); + FILE *pf = fopen("/sys/class/thermal/thermal_zone0/temp", "r"); + // error handling in case of not being able to open /sys/class/thermal/.. + 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/disk.c b/source/disk.c new file mode 100644 index 0000000..0608eb2 --- /dev/null +++ b/source/disk.c @@ -0,0 +1,84 @@ +/* + * ===================================================================================== + * + * 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 + +void disk_size(); +char* disk_partitions(); + +typedef struct { + + char* partition_name; + unsigned long parition_size; + +} partition; + +typedef struct { + partition* paritions; + long total_disk_size; +} 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/general.c b/source/general.c new file mode 100644 index 0000000..7569984 --- /dev/null +++ b/source/general.c @@ -0,0 +1,72 @@ +/* + * ===================================================================================== + * + * 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/ram.c b/source/ram.c new file mode 100644 index 0000000..fa54ddb --- /dev/null +++ b/source/ram.c @@ -0,0 +1,145 @@ +/* + * ===================================================================================== + * + * 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 +