Sign in to follow this  
fathom88

UNIX Programming Question: Check Mount

Recommended Posts

I need to check if a drive has been mounted. I use struct stat Data; bool CD_ROM_Mounted = false; if(stat("/mnt/cdrom", &Data) >= 0) { CD_ROM_Mounted = true; } This bit of code seems to work on the 1st attempt. When the user removes the drive, I make the following call system("umount /mnt/cdrom"); Then I insert a USB HD. I make the same call. if(stat("/mnt/cdrom", &Data) >= 0) { CD_ROM_Mounted = true; } I would expect the stat call to return an error and not set my flag. However, it sets mt CD Rom flag to true even though the folder should have been unmounted. Am I doing something obviously wrong? Should I use a different call to check if a folder is mounted? Thanks.

Share this post


Link to post
Share on other sites
The stat function is probably not what you want. And, anyway, that will return zero so long as the directory (or file, or whatever) exists, whether anything's mounted on it or not. (That stat returns zero doesn't tell you much anyway, you've got to look at the struct stat that it fills in.)

See the stat man page (type "man 2 stat")

But, to find what's mounted, you probably want getmntent, not stat. (so, type "man getmntent")

You probably do not want to read /etc/mtab directly, You probably want to use getmntent() to do it. Beware of system specific differences though, HPUX, SYS V, Digital unix, etc have differences (being a game oriented site, I'm guessing you likely don't care about those too much, probably only linux, Mac OS X, and FreeBSD -- but that's a guess.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this