• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <errno.h>
8 
9 
10 #define BUFFER_SIZE 4096
11 char buffer[BUFFER_SIZE];
12 
13 #define CPUINFO_PATH "/proc/cpuinfo"
14 
main(int argc,char ** argv)15 int main(int argc, char** argv) {
16 	int file = open(CPUINFO_PATH, O_RDONLY);
17 	if (file == -1) {
18 		fprintf(stderr, "Error: failed to open %s: %s\n", CPUINFO_PATH, strerror(errno));
19 		exit(EXIT_FAILURE);
20 	}
21 
22 	/* Only used for error reporting */
23 	size_t position = 0;
24 	char* data_start = buffer;
25 	ssize_t bytes_read;
26 	do {
27 		bytes_read = read(file, buffer, BUFFER_SIZE);
28 		if (bytes_read < 0) {
29 			fprintf(stderr, "Error: failed to read file %s at position %zu: %s\n",
30 				CPUINFO_PATH, position, strerror(errno));
31 			exit(EXIT_FAILURE);
32 		}
33 
34 		position += (size_t) bytes_read;
35 		if (bytes_read > 0) {
36 			fwrite(buffer, 1, (size_t) bytes_read, stdout);
37 		}
38 	} while (bytes_read != 0);
39 
40 	if (close(file) != 0) {
41 		fprintf(stderr, "Error: failed to close %s: %s\n", CPUINFO_PATH, strerror(errno));
42 		exit(EXIT_FAILURE);
43 	}
44 	return EXIT_SUCCESS;
45 }
46