Thread: how to list available drives?

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    3

    Question how to list available drives?

    Hi All!

    I found tons of readings/FAQs about how to open, list files, directories, but I was not able to find info about, how the drive listing is working in C. I want to write a file open dialog for a GLUT application, that's why I need to implement drive listing.
    I would really appreciate your help if you could show me links, examples, or give advice.

    Thx!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What OS[es] do you plan for your program to run under?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    3
    I'm writing it under windows, but I'd like to make it as portable as possible.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    I don't think it is possible to write a portable program that lists the hard drive. Say you have a hard drive and a cd-rom. On windows, the hard drive might be listed as the c drive and the cd-rom the d drive. On linux, both the hard drive and cd-rom are mounted under a central virtual filesystem. The windows filesystem is decentralized, the linux filesystem is centralized.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Well. I don't really know how it works under linux, but on Windows, you can use the GetLogicalDriveStrings API.

    Code:
    TCHAR buf[256];
    ::GetLogicalDriveStrings(sizeof buf, buf);
    Then, the buf will contain data like

    Code:
    "A:\\\0C:\\\0D:\\\0" ...
     'A', ':', '\\', '\0', 'C', ':', '\\', '\0' ...
    That being, drive letter, colon, backslash, null byte.

    Edit: I guess you could figure out the same information using the http://msdn.microsoft.com/library/de...tdrivetype.asp GetLogicalDrives API.
    Last edited by Tonto; 07-22-2006 at 05:36 PM.

  6. #6
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    under linux the best way would probably to look in /proc .. and there are several things that would be possibilities.. on mine i have diskstats (which is what df reads from) and then there is also mounts ..

    i cant say for sure how uniform everything in /proc will be across distros, but i would think that the two i mentioned should be pretty well the same on anything..

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    3
    Thanks for the useful tips guys/girls!

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Under linux you might be able to use the output of the mount command or /etc/mtab. But Linux doesn't have drive letters.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    im pretty sure mount reads from /proc/mounts, so it might be easier to just open /proc/mounts..

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're right, I think it does.

    But anyway, /proc/mounts might look something like this:
    Code:
    user@0[~]$ cat /proc/mounts
    rootfs / rootfs rw 0 0
    /dev/root / ext3 rw,noatime 0 0
    none /proc proc rw,nodiratime 0 0
    none /dev/pts devpts rw 0 0
    none /proc/bus/usb usbfs rw 0 0
    sysfs /sys sysfs rw 0 0
    tmpfs /dev/shm tmpfs rw 0 0
    /dev/hdd1 /home ext3 rw,noatime 0 0
    user@0[~]$
    It gives you the mounted filesystems, but not the drive letters. Your file dialog would have to be different under linux.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    yeh that would be something to consider, the fact that there is no "C:" in linux. and you wouldnt want to have one in your program, or you would lose credibility pretty fast. (theyre all going to laugh at you )

    the best thing to do would be to list them by the mount points, /, /home, /usr/local etc etc..

    /proc/diskstats would be something else to look at.. im not sure what the numbers represent though.. inodes maybe.. they dont correspond to output from `df`.. but im sure you could find the conversion somewhere..

    here is what my diskstats reads..
    Code:
       3    0 hda 1470003 58786 22797118 4243356 1293616 3150921 35557576 129138256 0 5106396 133381912
       3    1 hda1 371135 3809772 1329141 10633128
       3    2 hda2 0 0 0 0
       3    5 hda5 138694 1422704 348427 2787416
       3    6 hda6 236041 3302410 734559 5876360
       3    7 hda7 576833 5830992 999998 7999984
       3    8 hda8 61620 2486648 118177 945416
       3    9 hda9 144568 5944528 914407 7315256
       3   10 hda10 1 8 2 16
       3   64 hdb 99308 6053900 24612840 624224 0 0 0 0 0 546568 624224
       8    0 sda 458548 398384 6547494 3327688 382776 1707309 16523112 216613044 0 4050900 220214980
       8    1 sda1 0 0 0 0
       8    2 sda2 0 0 0 0
       8    5 sda5 432802 432802 0 0
       8    6 sda6 424153 6114668 2090277 16523112

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Why don't you give the user the option to type in a directory name. You could chdir() to the directory to see if it's accessable. That way, under linux, you won't have any drive letters, and under windows, the user can just type "E:" if their system has a drive E.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  2. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  3. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  4. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM