-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_drives.c
46 lines (38 loc) · 997 Bytes
/
list_drives.c
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
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int starts_with(char *str, char *prefix) {
if(strncmp(str, prefix, strlen(prefix)) == 0)
return 1;
return 0;
}
int print_formatted(char *str) {
char* device = strtok(str, " ");
char* mount_point = strtok(NULL, " ");
char* fs_type = strtok(NULL, " ");
printf("File system type: %s\nDevice: %s\nMounted on: %s\n\n", fs_type, device, mount_point);
}
void read_file(char *filename) {
FILE* fp = fopen(filename, "r");
if(fp == NULL) {
fprintf(stderr, "Error opening file.\n");
exit(EXIT_FAILURE);
}
char* line = NULL;
size_t len = 0;
ssize_t read;
while((read = getline(&line, &len, fp)) != -1) {
if(starts_with(line, "/dev/"))
print_formatted(line);
}
fclose(fp);
if(line)
free(line);
}
int main() {
read_file("/proc/mounts");
return EXIT_SUCCESS;
}