Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- int util_len(void *ptr)
- {
- int j = 0;
- uint8_t *ptr_w = (uint8_t *)ptr;
- while(ptr_w[j] != 0)
- {
- j++;
- }
- return j;
- }
- int util_zero(void *ptr, uint32_t size)
- {
- uint8_t *ptr_w = (uint8_t *)ptr;
- uint32_t j;
- for(j = 0; j < size; j++)
- {
- ptr_w[j] = 0;
- }
- }
- int util_exists(void *ptr, void *ptr2, uint32_t ptr_size, uint32_t ptr2_size)
- {
- uint8_t *ptr_w = (uint8_t *)ptr;
- uint8_t *ptr2_w = (uint8_t *)ptr2;
- uint32_t j;
- int ptr2_pos = 0;
- if(ptr2_size > ptr_size) return -1;
- for(j = 0; j < ptr_size; j++)
- {
- if(ptr_w[j] == ptr2_w[ptr2_pos])
- {
- #ifdef DEBUG_UTILS
- printf("(util_exists) ptr_w[%d] == ptr2_w[%d]: success\r\n", j, ptr2_pos);
- #endif
- ptr2_pos++;
- if(ptr2_pos == ptr2_size)
- {
- #ifdef DEBUG_UTILS
- printf("(util_exists) success found exist match! starts at %d\r\n", j-ptr2_pos+1);
- #endif
- return j-ptr2_pos+1;
- }
- continue;
- }
- else if(ptr2_pos > 0)
- {
- #ifdef DEBUG_UTILS
- printf("(util_exists) ptr_w[%d] != ptr2_w[%d]: failure reset continue\r\n", j, ptr2_pos);
- #endif
- ptr2_pos = 0;
- }
- else
- {
- #ifdef DEBUG_UTILS
- printf("(util_exists) ptr_w[%d] != ptr2_w[%d]: failure continue\r\n", j, ptr2_pos);
- #endif
- continue;
- }
- }
- if(ptr2_pos == ptr2_size)
- {
- return j-ptr2_pos+1;
- }
- return -1;
- }
- static double get_bogos(void)
- {
- uint8_t *recvbuffer = NULL;
- uint16_t recvbuffer_len = 0;
- FILE *fp = fopen("/proc/cpuinfo", "r");
- if(!fp)
- {
- #ifdef DEBUG
- printf("(get_bogos) Failed to open /proc/cpuinfo");
- #endif
- return -1;
- }
- recvbuffer = malloc(1024);
- util_zero(recvbuffer, 1024);
- while(fgets(recvbuffer, 1024, fp) != NULL)
- {
- int pos = 0;
- int value_location = 0;
- int ret = 0;
- for(pos = 0; pos < 1024; )
- {
- if(recvbuffer[pos] == 0)
- {
- recvbuffer_len = pos;
- break;
- }
- if(recvbuffer[pos] == '\r' || recvbuffer[pos] == '\n')
- {
- recvbuffer[pos] = 0;
- continue;
- }
- pos++;
- }
- #ifdef DEBUG
- printf("(get_bogos) read line \"%s\" - with len %d/%d\r\n", recvbuffer, recvbuffer_len, util_len(recvbuffer));
- #endif
- //bogomips
- uint8_t *find_buf = malloc(8);
- util_zero(find_buf, 8);
- find_buf[0] = 'b';//b
- find_buf[1] = 'o';//bo
- find_buf[2] = 'g';//bog
- find_buf[3] = 'o';//bogo
- find_buf[4] = 'm';
- find_buf[5] = 'i';
- find_buf[6] = 'p';
- find_buf[7] = 's';
- ret = util_exists(recvbuffer, find_buf, recvbuffer_len, 8);
- if(ret >= 0)// ret = begining of where bogomips is located
- {
- for(pos = ret+8; pos < 1024; pos++)
- {
- if(recvbuffer[pos] == ':')
- {
- double bogos = 0;
- value_location = pos+2;
- bogos = strtod(recvbuffer+value_location, NULL);
- util_zero(find_buf, 8);
- free(find_buf);
- find_buf = NULL;
- util_zero(recvbuffer, 1024);
- free(recvbuffer);
- recvbuffer = NULL;
- return bogos;
- }
- }
- }
- util_zero(recvbuffer, 1024);
- util_zero(find_buf, 8);
- free(find_buf);
- find_buf = NULL;
- }
- #ifdef DEBUG
- printf("(get_bogos) Failed to find string \"bogomips\" in /proc/cpuinfo!\r\n");
- #endif
- util_zero(recvbuffer, 1024);
- free(recvbuffer);
- recvbuffer = NULL;
- return -1;
- }
- static uint8_t get_cores(void)
- {
- uint8_t cores = 0;
- uint8_t *recvbuffer = NULL;
- uint16_t recvbuffer_len = 0;
- FILE *fp = fopen("/proc/cpuinfo", "r");
- if(!fp)
- {
- #ifdef DEBUG
- printf("(get_cores) Failed to open /proc/cpuinfo");
- #endif
- return 0;
- }
- recvbuffer = malloc(1024);
- util_zero(recvbuffer, 1024);
- while(fgets(recvbuffer, 1024, fp) != NULL)
- {
- int pos = 0;
- int value_location = 0;
- int ret = 0;
- for(pos = 0; pos < 1024; )
- {
- if(recvbuffer[pos] == 0)
- {
- recvbuffer_len = pos;
- break;
- }
- if(recvbuffer[pos] == '\r' || recvbuffer[pos] == '\n')
- {
- recvbuffer[pos] = 0;
- continue;
- }
- pos++;
- }
- #ifdef DEBUG
- printf("(get_cores) read line \"%s\" - with len %d/%d\r\n", recvbuffer, recvbuffer_len, util_len(recvbuffer));
- #endif
- uint8_t *find_buf = malloc(9);
- util_zero(find_buf, 9);
- //processor
- find_buf[0] = 'p';
- find_buf[1] = 'r';
- find_buf[2] = 'o';
- find_buf[3] = 'c';
- find_buf[4] = 'e';
- find_buf[5] = 's';
- find_buf[6] = 's';
- find_buf[7] = 'o';
- find_buf[8] = 'r';
- ret = util_exists(recvbuffer, find_buf, recvbuffer_len, 9);
- if(ret >= 0)// ret = begining of where bogomips is located
- {
- cores++;
- }
- util_zero(recvbuffer, 1024);
- util_zero(find_buf, 8);
- free(find_buf);
- find_buf = NULL;
- }
- util_zero(recvbuffer, 1024);
- free(recvbuffer);
- recvbuffer = NULL;
- return cores;
- }
- static uint32_t get_mem(void)
- {
- uint8_t *recvbuffer = NULL;
- uint16_t recvbuffer_len = 0;
- FILE *fp = fopen("/proc/meminfo", "r");
- if(!fp)
- {
- #ifdef DEBUG
- printf("(get_mem) Failed to open /proc/meminfo");
- #endif
- return -1;
- }
- recvbuffer = malloc(1024);
- util_zero(recvbuffer, 1024);
- while(fgets(recvbuffer, 1024, fp) != NULL)
- {
- int pos = 0;
- int value_location = 0;
- int ret = 0;
- for(pos = 0; pos < 1024; )
- {
- if(recvbuffer[pos] == 0)
- {
- recvbuffer_len = pos;
- break;
- }
- if(recvbuffer[pos] == '\r' || recvbuffer[pos] == '\n')
- {
- recvbuffer[pos] = 0;
- continue;
- }
- pos++;
- }
- #ifdef DEBUG
- printf("(get_mem) read line \"%s\" - with len %d/%d\r\n", recvbuffer, recvbuffer_len, util_len(recvbuffer));
- #endif
- uint8_t *find_buf = malloc(8);
- util_zero(find_buf, 8);
- //MemTotal
- find_buf[0] = 'M';
- find_buf[1] = 'e';
- find_buf[2] = 'm';
- find_buf[3] = 'T';
- find_buf[4] = 'o';
- find_buf[5] = 't';
- find_buf[6] = 'a';
- find_buf[7] = 'l';
- ret = util_exists(recvbuffer, find_buf, recvbuffer_len, 8);
- if(ret >= 0)// ret = begining of where bogomips is located
- {
- uint8_t found_colon = 0;
- for(pos = ret+8; pos < 1024; pos++)
- {
- if(recvbuffer[pos] != ' ' && found_colon == 1)
- {
- uint32_t mem_kb = 0;
- value_location = pos;
- mem_kb = atoi(recvbuffer+value_location);
- util_zero(find_buf, 8);
- free(find_buf);
- find_buf = NULL;
- util_zero(recvbuffer, 1024);
- free(recvbuffer);
- recvbuffer = NULL;
- return mem_kb/1000;
- }
- if(recvbuffer[pos] == ':')
- {
- found_colon = 1;
- continue;
- }
- }
- }
- util_zero(recvbuffer, 1024);
- util_zero(find_buf, 8);
- free(find_buf);
- find_buf = NULL;
- }
- #ifdef DEBUG
- printf("(get_bogos) Failed to find string \"bogomips\" in /proc/cpuinfo!\r\n");
- #endif
- util_zero(recvbuffer, 1024);
- free(recvbuffer);
- recvbuffer = NULL;
- return -1;
- }
- int main()
- {
- printf("Got %lf bogomips\r\n", get_bogos());
- printf("Got %d cores\r\n", get_cores());
- printf("Got %d mb ram\r\n", get_mem());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement