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