Skip to content

Instantly share code, notes, and snippets.

@sulincix
Created December 1, 2024 00:33
Show Gist options
  • Save sulincix/d88fc773c132f9b01e420ea6bba3e84b to your computer and use it in GitHub Desktop.
Save sulincix/d88fc773c132f9b01e420ea6bba3e84b to your computer and use it in GitHub Desktop.
List All Available filesystems in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** list_filesystems(size_t* len){
char buffer[1024];
FILE* fs = fopen("/proc/filesystems","r");
if(fs == NULL){
perror("Failed to open file");
}
char** ret = malloc(sizeof(char*));
ret[0] = NULL;
size_t i = 0;
while(fgets(buffer, sizeof(buffer), fs) != NULL){
if(strncmp(buffer, "nodev", 4)){
buffer[strlen(buffer)-1] = '\0';
ret = realloc(ret, sizeof(ret)+sizeof(char*)*2);
ret[i] = strdup(buffer+1);
i++;
}
}
*len = i;
return ret;
}
void main(){
size_t *len;
char** fss = list_filesystems(len);
for(size_t i = 0; i < *len;i++){
puts(fss[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment