Reputation: 815
People, I need get the list of hard disk connected in C language on Linux system:
Example, running a program on a computer with 2 IDE disks and 1 SATA disk connected.
./a.out
Out required:
/dev/hda
/dev/hdb
/dev/sda
help?
Upvotes: 1
Views: 4787
Reputation: 1
Command Line
"ls /sys/block/"
will return output as:
sda sdb sdc
From there, you could creat a script that pipes it to a file, then read in that file as an array or linked list to manipulate the data however you see fit (such as adding /dev/ in front of all device names in the list).
Upvotes: 0
Reputation: 1009
FILE *fp = popen("fdisk -l | grep \"Disk /\" | awk '{print $2};' | sed 's/://'", "r");
while(fgets(path, sizeof(path) -1,fp) != NULL)
//your code
pclose(fp);
Upvotes: 1
Reputation: 1
Maybe you can reference fdisk's source code. Follow this website: ftp://ftp.gnu.org/gnu/fdisk
Upvotes: 0
Reputation: 42040
The simplest way would be simply read and parse /proc/partitions
.
Upvotes: 0
Reputation: 48775
Use libsysfs, the recommended way to query the kernel about attached devices of all kinds.
Upvotes: 1