Question

glob.c does not recognize app directory as directory:

const char* cpath = env->GetStringUTFChars(jpath, 0);
debug("testing converting for [%s]", cpath);

// 
const char *patnext = cpath;
size_t limit;
wchar_t *bufnext, *bufend, patbuf[MAXPATHLEN];
mbstate_t mbs;
wchar_t wc;
size_t clen;

bufnext = patbuf;
bufend = bufnext + MAXPATHLEN - 1;

int len = 0;
while (bufend - bufnext >= MB_CUR_MAX) {
    clen = mbrtowc(&wc, patnext, MB_LEN_MAX, &mbs);

    debug("clen = %i", clen);

    if (clen == (size_t)-1 || clen == (size_t)-2) {
        env->ReleaseStringUTFChars(jpath, cpath);
        return JNI_FALSE;
    }
    else if (clen == 0)
        break;

    *bufnext++ = wc;
    patnext += clen;
    len += clen;
}
for (int i=0; i<len; i++) {
    debug("%c", patbuf[i]);
}
glob((const char *)patbuf, GLOB_MARK, NULL, &glob_results);
debug("glob returned %i\n", glob_results.gl_pathc);

returns 0 but 1 is expected.

03-03 23:58:30.274: ERROR/NATIVE_LIB(3506): /
03-03 23:58:30.274: ERROR/NATIVE_LIB(3506): d
03-03 23:58:30.274: ERROR/NATIVE_LIB(3506): a
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): t
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): a
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): /
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): d
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): a
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): t
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): a
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): /
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): c
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): o
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): m
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): .
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): e
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): x
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): a
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): m
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): p
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): l
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): e
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): .
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): W
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): i
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): d
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): e
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): C
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): h
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): a
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): r
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): D
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): e
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): m
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): o
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): /
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): f
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): i
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): l
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): e
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): s
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): /
03-03 23:58:30.284: ERROR/NATIVE_LIB(3506): glob returned 0

Test project on Github: https://github.com/4ntoine/WideCharDemo

How can it be fixed? Any walk-around?

No correct solution

OTHER TIPS

Your patbuf buffer is an array of wchar_t items, you cannot simply type-cast its address to a 'const char*'. glob() expects to be passed the address of a real 8-bit string (e.g. a UTF-8 one).

Also, you never zero-terminate your patbuf buffer, which in itself is another serious bug.

You should instead convert the path into a zero-terminated UTF-8 string, and pass its address to glob().

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top