Added the source and build files I've wwritten up till now.
This commit is contained in:
parent
d70738f3d5
commit
29fc5bbb3f
BIN
build/cpu_linux
Executable file
BIN
build/cpu_linux
Executable file
Binary file not shown.
BIN
build/disk
Executable file
BIN
build/disk
Executable file
Binary file not shown.
BIN
build/general
Executable file
BIN
build/general
Executable file
Binary file not shown.
209
source/cpu.c
Normal file
209
source/cpu.c
Normal file
@ -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 <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/sysctl.h>
|
||||||
|
|
||||||
|
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 <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#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
|
||||||
84
source/disk.c
Normal file
84
source/disk.c
Normal file
@ -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 <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <IOKit/IOKitLib.h>
|
||||||
|
#include <CoreFoundation/CoreFoundation.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
72
source/general.c
Normal file
72
source/general.c
Normal file
@ -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 <sys/types.h>
|
||||||
|
#include <sys/sysctl.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
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 <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/sysinfo.h>
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
145
source/ram.c
Normal file
145
source/ram.c
Normal file
@ -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 <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/sysctl.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#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 <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user