LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2007, 05:15 AM   #1
son_t
Member
 
Registered: Sep 2006
Posts: 49

Rep: Reputation: 15
getting disks in C


Hello

Could anyone tell me how I go about getting a list of disks on a system in C?

I am just reading a disk in a raw way on Debian Linux like this:
Code:
int inCc=0, start=0;
int inFd=0, readsize=16;

unsigned char *diskin = (char *)malloc(readsize);

char *inFile = "/dev/hdc";
lseek(inFd,start,SEEK_SET);
inCc = read(inFd,diskin,readsize);
Do I just generate strings "/dev/hda", "/dev/hdc", "/dev/hde", etc and see if they exist?

On Solaris I can use 'iostat' to return the disk lables 'c0t0d0', etc but on Debian (after installing sysstat) it gives me 'ide/host0/bus1/target0/lun0/disc' etc... How do I get it to give me hda, hdc, etc?

TIA

Last edited by son_t; 02-12-2007 at 05:16 AM.
 
Old 02-12-2007, 06:18 AM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,897

Rep: Reputation: 578Reputation: 578Reputation: 578Reputation: 578Reputation: 578Reputation: 578
'blkid' does what you want, I believe.
 
Old 02-12-2007, 07:19 AM   #3
son_t
Member
 
Registered: Sep 2006
Posts: 49

Original Poster
Rep: Reputation: 15
Thanks, but the attached disk might not be a block device (a non Linux formatted disk).
 
Old 02-13-2007, 03:03 AM   #4
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,897

Rep: Reputation: 578Reputation: 578Reputation: 578Reputation: 578Reputation: 578Reputation: 578
blkid will show non-linux filesystems also. fdisk -l will show non-formatted partitions as well. If no recognized partitions are present it will still show the full physical device. Note that it may help to include support for other types of partition tables/maps in your kernel.
 
Old 02-14-2007, 02:50 AM   #5
son_t
Member
 
Registered: Sep 2006
Posts: 49

Original Poster
Rep: Reputation: 15
Thanks for the further reply.

I have 2 HDDs on my Debian system. One is the systems disk /dev/hda with various partitions and one 'really raw' (say pull the HDD out of a PVR or similar) HDD '/dev/hdc' to which I can read and write to with no problems.

Neither blkid or fdisk -l shows it:
/dev/hda1: LABEL="/" UUID="626f345b-0fb7-48b7-9326-9427e1d168e7" SEC_TYPE="ext2" TYPE="ext3"
/dev/hda5: TYPE="swap"

and fdisk -l returns nothing (not even the /dev/hda device).
 
Old 02-14-2007, 04:37 AM   #6
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,897

Rep: Reputation: 578Reputation: 578Reputation: 578Reputation: 578Reputation: 578Reputation: 578
How about lspci?
What kind of filesystem is the /dev/hdc formatted with? And what kind of partiton table/map does it use(what's a PVR)?
 
Old 03-13-2007, 07:05 AM   #7
son_t
Member
 
Registered: Sep 2006
Posts: 49

Original Poster
Rep: Reputation: 15
A PVR is a Personal Video Recorder, a TiVo is an example of a PVR and my particular one has its own FS.

So is this the only way I can detect if the HDD is present?

Code:
void main()
{
        char *disk_arr[] = {"/dev/hda","/dev/hdb","/dev/hdc","/dev/hdd","/dev/hde","/dev/sda","/dev/sdb","/dev/sdc","/dev/sdd","/dev/sde"};
        char *disk=NULL;
        int dcnt=10;
        int i=0;
 
        while(!disk && i<dcnt)
        {
                if(checkdisk(disk_arr[i]))
                        disk=newString(disk_arr[i]);
                i++;
        }
}
Any other suggestion or improvement on this code?

Last edited by son_t; 03-13-2007 at 07:13 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Installation Disks not recognized as official disks when booting with floppy coal-fire-ice Mandriva 13 12-09-2004 04:29 PM
What disks do I need Zuggy Linux - General 1 11-06-2004 02:59 PM
Which disks? desolator Linux - Newbie 8 12-01-2003 03:38 PM
Which disks? cap'n leaky Debian 1 11-01-2003 05:41 AM
New disks b0b0 Linux - General 1 11-25-2001 02:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration