Complete.Org: Mailing Lists: Archives: discussion: September 1999:
[aclug-L] Re:

[aclug-L] Re:

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: aclug-L@xxxxxxxxxxxx
Subject: [aclug-L] Re:
From: Tom Hull <thull@xxxxxxxxxxx>
Date: Mon, 20 Sep 1999 14:36:43 -0500
Reply-to: aclug-L@xxxxxxxxxxxx

gLaNDix wrote:
> ok, thanks...  i'll take a look at this!
> jesse

The opendir/readdir version of the code looks something like this:

    #include <sys/types.h>
    #include <dirent.h>
    #include <limits.h>
    #include <string.h>

    char *directory_name;
    DIR *dir_structp;
    struct dirent *dir_entp;
    char path_buf[PATH_MAX];

    if ((dir_structp = opendir(directory_name)) == 0) {
        /* complain and exit or die */
    while (dir_entp = readdir(dir_structp)) {
        char *p;
        if (p = (strrchr(dir_entp->d_name, '.')) {
            if (!strcmp(p, ".mp3x")) {
                sprintf(path_buf, "%s/%s", directory_name, dir_entp->d_name);
                /* use pathname in path_buf has needed */

This, of course, has a slight performance advantage over forking a shell and
running /bin/ls under popen(3). The other thing you get is complete control
over error messages -- ls will write to stderr if the requested directory
does not exist, and you have no control over that (other than to throw a
"2>/dev/null" into the popen command line).

One possibly nice thing that you get from using ls is that the output will
be sorted. Sorting requires that you buffer the pathnames until you get all
of them. This adds a bit of complexity, but if you're buffering them anyway,
sorting is just a qsort(3) call.

 * Tom Hull -- mailto:thull@xxxxxxxxxxx or thull@xxxxxxxxxx

[Prev in Thread] Current Thread [Next in Thread]