1

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?

1

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I will check it out.. but I found 'ls /sys/block/' and I think it is a good solution. I need the more standart way. Thank!
1
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); 

3 Comments

This requires root on my system
@natinusala, Use sudo before fdisk if user is present in sudoers list.
It was just to point this out, if you want the code to be portable you shouldn't rely on code needing a root permission
0

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

1 Comment

This is the way what I don't want to do :D
0

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

Comments

0

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).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.