views:

20

answers:

0

Hi,

I'm seeing some weird behaviour when using wordexp() in a minimal C program when started within Xcode. I cannot reproduce this by starting the compiled binary from the command line.

#include <stdio.h>
#include <stdlib.h>
#include <wordexp.h>
#include <errno.h>
#include <assert.h>

int main (int argc, const char * argv[])
{
    int         wordexpResult;
    wordexp_t   words;
    char*       origPath = "~";

    wordexpResult = wordexp(origPath, &words, 0);

    printf("wordexpResult = %i\n", wordexpResult);

    switch (wordexpResult)
    {
        case 0:
            break;

        case WRDE_BADCHAR:
            fprintf(stderr, "BADCHAR\n");
            exit(EXIT_FAILURE);

        case WRDE_BADVAL:
            fprintf(stderr, "BADVAL\n");
            exit(EXIT_FAILURE);

        case WRDE_CMDSUB:
            fprintf(stderr, "CMDSUB\n");
            exit(EXIT_FAILURE);

        case WRDE_NOSPACE:
            fprintf(stderr, "NOSPACE\n");
            exit(EXIT_FAILURE);

        case WRDE_SYNTAX:
            fprintf(stderr, "SYNTAX\n");
            exit(EXIT_FAILURE);

        default:
            fprintf(stderr, "Unrecognized value: %d\n", wordexpResult);
            exit(EXIT_FAILURE);
    }

    assert(words.we_wordc != 0);

    wordfree(&words);

    return 0;
}

Sometimes (about one in five runs), the assertion evaluates to false, even though wordexp() always returns 0 (i.e. none of the cases in switch() are executed). This means that wordexp() does not return an error, but it does not expand the tilde either. How can that be?

Luckily, I don't have to rely on wordexp right now, but I'd still be interested in what is going on here. I mean... there's no multi threading, no varying input data, nothing. Any ideas?

This is running on Mac OS X 10.6.4, Xcode 3.2.4.

Cheers,

Marco