get physical hdd's list in c

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

Answers (5)

Double T
Double T

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

Nitinkumar Ambekar
Nitinkumar Ambekar

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

Hooben.Wong
Hooben.Wong

Reputation: 1

Maybe you can reference fdisk's source code. Follow this website: ftp://ftp.gnu.org/gnu/fdisk

Upvotes: 0

Uku Loskit
Uku Loskit

Reputation: 42040

The simplest way would be simply read and parse /proc/partitions.

Upvotes: 0

Chris Eberle
Chris Eberle

Reputation: 48775

Use libsysfs, the recommended way to query the kernel about attached devices of all kinds.

Upvotes: 1

Related Questions