I was trying to make a program to copy files from the documents folder on my Linux machine into the documents folder onto a USB with Windows on it. So far, it only creates files without copying any actual data to them, but that's not the problem I'm trying to solve right now. The code looks like this (there are no real errors, more information is written under the code):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
int main()
{
DIR *folder;
struct dirent *entry;
int files = 0;
folder = opendir("/home/ubuntumate/Documents");
if(folder == NULL)
{
perror("Unable to read directory");
return(1);
}
while( (entry=readdir(folder)) )
{
if(entry->d_type == DT_REG)
{
files ;
char newfilename[100];
snprintf(newfilename, sizeof(newfilename), "/media/ubuntumate/7494348594344C3C/Users/John/Documents/%s", entry->d_name);
FILE *clone = fopen(newfilename, "w");
fclose(clone);
}
}
closedir(folder);
return(0);
}
You may have noticed that I hard-coded the media's location, including both the username and the media's id. (my username is "ubuntumate" because I'm on a virtual machine running Ubuntu MATE). The question I'm asking is how do i get a list of all the medias attached to the machine, so that this program can still work with a different user, and on a different USB that also contains windows?
I'll just keep working on the program with this part hard-coded until I can find a solution, so it's not too urgent. Thanks in advance :)
CodePudding user response:
Never mind, I found an answer. I can just list all the folders in the media/[username] folder. There are a lot of tutorials on getting the username so that one wont be a problem, cuz i can just search it up.
CodePudding user response:
You can check whether a block device is removable under the sys
hierarchy. E.g. something like
$ stat /dev/sdc|grep Device
Device: 6h/6d Inode: 347 Links: 1 Device type: 8,20
From here you see the device is major number 8, minor 20. Then check
$ cat /sys/dev/block/8\:32/removable
0