Results 1 to 8 of 8

Thread: getfsstat not working in ubuntu(but works in other os)

  1. #1
    Join Date
    Dec 2007
    Location
    Romania
    Beans
    88
    Distro
    Kubuntu

    getfsstat not working in ubuntu(but works in other os)

    Hello... this is a new one:
    this code works in FreeBSD, Solaris, Mac OS X and even RHEL Linux.
    On this page http://manpages.ubuntu.com/manpages/....2freebsd.html they say that getfsstat is a standard c library. I researched it a bit and it turns out that it should work in ubuntu.
    It is very important to the project that i am working on to get getfsstat to work properly, and i think the only problem with this example to run well is the fact that somewhere ubuntu can't find, or lacks a proper library.
    Thank you.

    Code:
    #include <sys/param.h>
    #include <sys/mount.h>
     
    #include <errno.h>
    #include <stdio.h>
    #include <stdlib.h>
     
    #define MAX_FS 128
     
    void
    print_mounted_fs(void)
    {
            struct statfs buf[MAX_FS];
            int fs_count;
            int i;
     
            fs_count = getfsstat(NULL, 0, MNT_NOWAIT);
            if (fs_count == -1) {
                    fprintf(stderr, "Error: %d\n", errno);
                    exit(1);
            }
            printf("%d file systems found.", fs_count);
     
            getfsstat(buf, fs_count * sizeof(buf[0]), MNT_NOWAIT);
     
            printf("\n");
            for (i = 0; i < fs_count; ++i) {
                    printf("%s\n", buf[i].f_mntonname);
            }
    }
     
    int
    main(void)
    {
            print_mounted_fs();
     
            return 0;
    }

  2. #2
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: getfsstat not working in ubuntu(but works in other os)

    Quote Originally Posted by AndreiMe View Post
    On this page http://manpages.ubuntu.com/manpages/....2freebsd.html they say that getfsstat is a standard c library.
    No, certainly not as this is a system-type dependent function. In fact, the manual page you refer to just states that it appeared in BSD 4.4 first. Note that this is a manual page imported from FreeBSD which is packaged for Ubuntu for convenience, but does not reflect Ubuntu's actual behaviour.

    If you want to use Linux-compatible functions, have a look here, maybe that's useful for you:
    http://linux.die.net/man/2/statvfs

  3. #3
    Join Date
    Dec 2007
    Location
    Romania
    Beans
    88
    Distro
    Kubuntu

    Re: getfsstat not working in ubuntu(but works in other os)

    it's very strange because it is considered standard library in c.... oh well, i'll look into it. do you have something like an example you can share?

  4. #4
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: getfsstat not working in ubuntu(but works in other os)

    Quote Originally Posted by AndreiMe View Post
    do you have something like an example you can share?
    Erm, no, sorry, I just looked it up. Note that the "standard C library" is not necessarily the same under FreeBSD and Linux (in Linux, the GNU LibC is used). Watch out for functions that are part of the POSIX standard if you want compatibility. However, for such a system-dependent purpose I doubt that there are any.
    Last edited by Zugzwang; July 30th, 2009 at 03:02 PM.

  5. #5
    Join Date
    Dec 2007
    Location
    Romania
    Beans
    88
    Distro
    Kubuntu

    Re: getfsstat not working in ubuntu(but works in other os)

    i'm making an app in qt cross platform and i need to have a way to show get the mounted partitions.
    so is there something like
    ifndef WIN32
    endif
    but for bsd/linux?

  6. #6
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: getfsstat not working in ubuntu(but works in other os)

    Quote Originally Posted by AndreiMe View Post
    i'm making an app in qt cross platform and i need to have a way to show get the mounted partitions.
    so is there something like
    ifndef WIN32
    endif
    but for bsd/linux?
    I'm sure there is - Look up the GNU compiler manual for details. You can however always use different Makefiles for the individual platforms to circumvent the problem.

    For finding out how to get your data in Linux, you might want to have a look at the source code of the "df" utility which is part of the "coreutils" package: http://packages.ubuntu.com/jaunty/coreutils

    It will however be simpler just to parse the output of that program for your purpose.

  7. #7
    Join Date
    Dec 2007
    Location
    Romania
    Beans
    88
    Distro
    Kubuntu

    Re: getfsstat not working in ubuntu(but works in other os)

    thank you for the swift help, it is really helpful.
    now if you can(or someone else) help me figure this out it would be great.
    here's what i have:
    Code:
    #ifndef WIN32
    void
    fileHandler::print_mounted_fs(void)
    {
         struct statfs FSBuf;
            int fs_count;
           fs_count = statfs("/", &FSBuf);
           qDebug() << "file systems found: " <<fs_count;//this outputs in 
                                                       //the debug view
       
    }
    #endif
    this returns 0. i think it is doing just what it should do, because the root is 1 filesystem and so on. but my quest is to find all mounted media, in a similar way to how the function i supplied up there does inside the unix platform.
    i know that the two functions are different and behave different but there has to be a way in which i can get all the mounted drives.
    and for the other thing... i am using the ifndef win32 but due to the fact that it should work on multiple platforms i have to find a way in which to separate the platforms.
    thank you.

  8. #8
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: getfsstat not working in ubuntu(but works in other os)

    Quote Originally Posted by AndreiMe View Post
    ... but there has to be a way in which i can get all the mounted drives.
    As already stated, look into the source code of the "df" tool which does precisely that.

    Quote Originally Posted by AndreiMe View Post
    and for the other thing... i am using the ifndef win32 but due to the fact that it should work on multiple platforms i have to find a way in which to separate the platforms.
    thank you.
    As already stated, use different Makefiles, i.e., put the platform-dependent code into separate source files and use different Makefiles to include only the code your need for the respective platform.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •