aboutsummaryrefslogtreecommitdiff
path: root/src/modules/cpu/cpu.c
blob: b02a68f5857d2120b85a87c1838b0f559f01349f (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cpu.h"

/* function: char *string_format(char* string)
 * input: pointer to a null-terminated string (must not be NULL)
 * return: pointer to a newly allocated string containing all space-separated words,
 * 	joined by single spaces; leading/trailing/multiple spaces are ignored.
 * on-error: returns NULL if memory allocation fails
 * misc: the returned pointer must be freed by the caller using free()
 * limits: processes at most 10 words, each up to 49 characters (last '\0')
 */
char *string_format(char* string) {
    // Wörterbuch array [Wort][Zeichenkette]
    char woerter[10][50] = { 0 };
    int wort_index = 0;
    int char_index = 0;

    while (*string != '\0') {
	// add '\0' stringterminator to end of word
	if (*string == ' ' && char_index > 0) {
	    // safty check for array length
	    if (wort_index > 9 || char_index > 49) {
		break;
	    }

	    // next slot is char
	    // add '\0' to end string
	    woerter[wort_index][char_index] = '\0';
	    wort_index++;
	    char_index = 0;
	    string++;
	    continue;
	} 
	// skip extra spaces
	else if (*string == ' ' && char_index == 0) {
	    string++;
	    continue;
	}

	// safty check for array length
	if (char_index >= 49) {
	    break;
	}

	woerter[wort_index][char_index] = *string;
	char_index++;
	string++;
    }

    // terminate last word with '\0'
    if (char_index > 0 && wort_index < 10 && char_index < 49) {
	woerter[wort_index][char_index] = '\0';
	wort_index++;
    }

    /* 
     * return
     */
    char *ptr = NULL;

    // get length of woerter array
    if (wort_index == 0) {
	ptr = NULL;
    } else {
    	int sum = 0;

	for (int i = 0; i < wort_index; i++) {
	    sum += strlen(woerter[i]);
	}
	sum += wort_index - 1; // for spaces
	sum += 1; // for Nulltermination
    

	ptr = (char *)malloc(sum);

	// check for NULL pointer
	if (ptr == NULL) {
	    return NULL;
	}

	//strcpy
	char *ende = ptr;

	for (int i = 0; i < wort_index; i++) {
	    ende = stpcpy(ende, woerter[i]);

	    if (i != wort_index - 1) {
		*(ende++) = ' ';
	    }
	}
    }

    return ptr;
}


int cpu(void) {
    FILE* file = fopen("/proc/cpuinfo", "r");

    //check file existence
    if (!file) {
	perror("/proc/cpuinfo not found");
	return 1;
    }

    char line[256];
    char *model_name = NULL;
    
    //read per line
    while (fgets(line, sizeof(line), file)) {
	char* p = strstr(line, "model name");

	if (p != NULL) {
	    model_name = strchr(p, ':') + 1; //skip :
	    break;
	}
    }

    if (model_name != NULL) {
	char *string = string_format(model_name);

	if (string == NULL) {
	    printf("CPU: Error string not found.\n");
	    free(string);
	    model_name = NULL;
	} else {
	    printf("CPU: %s\n", string);
	    free(string);
	    model_name = NULL;
	}

    } else {
	printf("CPU: Error model_name not found.\n");
    }

    fclose(file);
    return 0;
}