views:

291

answers:

3

I know that standard C doesn't give me any ability to do anything with folders, but I would like a fairly portable and cross-platform way to access folders. At this time, all I need to do is make a folder, check if a folder exists, and possibly delete a folder. I can forsee needing to read files from a folder in the near future, but that's not a very pressing need.

Anyway, I was wondering if there was a good cross-platform C library for working with directories. In an absolute pinch I can probably roll my own to work on POSIX and Windows, but I was wondering if there were any good ones already out there. I've been considering GLib or the Apache Portable Runtime, but both of those come with a lot more stuff than I really need, and I'd like to keep this fairly lightweight. I've also considered using the internals of a popular scripting language, like Perl or Python, but that also seems like a lot of overhead just for directory functions.

If anyone has anything to add to this list that I should look into, or wants to make a good case for one of the options I've already listed, please tell me. I don't want to sound like I'm asking for code, but if you posted a simple function like int direxist(char *dirname) that returned true if the directory exists and false otherwise, just to illustrate the API for your library of choice, that would be really awesome, and I imagine not too hard. If you want to advocate using POSIX/rolling my own, do that too, because I'm a sucker for learning new stuff like this by doing it myself.

Just to make sure, I want C, not C++. I'm sure boost is good, but I'm not interested in C++ solutions.

+3  A: 

I would jump on the APR bandwagon. It does give you a lot more than directory access, but it is the best multi-platform C library that I've used. Chances are that you will find yourself needing some of the other components of it in the future anyway, so you might as well have them handy.

The other option is to implement the POSIX API set over Win32 and just write everything in POSIX. The bonus here is that the Windows is quickly becoming the only modern OS that does not include a POSIX runtime implementation.

D.Shawley
+1  A: 

I've been considering GLib or the Apache Portable Runtime, but both of those come with a lot more stuff than I really need, and I'd like to keep this fairly lightweight.

It's quite probable that GLib will already be installed (on GNU/Linux, at least). The only problem is that it will add a dependency.

I've also considered using the internals of a popular scripting language, like Perl or Python, but that also seems like a lot of overhead just for directory functions.

I would rather use Python in the first place, and possibly use C for specific parts of code.

>>> def direxist(dirname):
...     return os.path.isdir(dirname)
... 
>>> direxist('/')
True
>>> direxist('/home')
True
>>> direxist('/home/bastien/Petites leçons de typographie.pdf')
False

About writing your own C function, it would go something like this:

#include <stdio.h>

#ifdef _WIN32
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif

int direxist(const char* dirname)
{
#ifdef _WIN32
    /* ... */
#else
    struct stat fileinfo;
    int ret = -1;

    if (stat(dirname, &fileinfo) == -1)
    {
        perror("direxist");
    }
    else
    {
        if (S_ISDIR(fileinfo.st_mode))
        {
            ret = 1;
        }
        else
        {
            ret = 0;
        }
    }

    return ret;
#endif
}

int
main (void)
{
    printf("%d\n", direxist("/"));

    return 0;
}

I don't how to do it with Win32, so you'll to find that yourself.

However, I would strongly recommend using an external library. You don't go far with just the C library or by reinventing the wheel.

Bastien Léonard
I'm on OS X, so no free GLib for me. I sometimes feel like OS X is the redheaded stepchild of OS portability - despite being pretty close to POSIX, no one really thinks much about it. I don't want to use Python because I don't know Python (yet) and I would rather write it in C (personal preference). I don't mind adding a dependency, but I'd like to know my options first.
Chris Lutz
@Chris: if GTK+ is works on OS X, I'm pretty sure that GLib works as well. My guess is that this package contains GLib: http://www.gtk-osx.org/.
Bastien Léonard
I checked - that package contains GTK+ and GLib, but it installs under Xcode folders, and I usually work via the command line, so it's a bit inconvenient. I'll see what I can do though.
Chris Lutz
A: 

I think you should use APR or something in the same vein. Designing a POSIX API and then implement it for windows does not work well in my experience (which is quite limited I must confess).

File IO, and related semantics are just too different, so you have to design your API to deal with windows right away. There are also different constraints on what can be put in the public API. One example of such a problem is the python C API for file handling. It was clearly designed with a POSIX POV, and it is very difficult to use it on windows because of things like sharing C runtimes objects, etc...

David Cournapeau