Beispiel #1
0
/** NOTE: both IP address and port are in host byte order */
STATIC File * SocketOpen2(IPaddr addr, Port port)
{
    Socket sock = INVALID_SOCKET;
    if (SOCKET_GetTcp(0,&sock)) {
        if (SOCKET_Connect(sock, addr, port)) {
            SocketFile * s = MEM_New(SocketFile);
            if (s) {
                Bool ok;
                StrBuf32 nameBuf;
                STRBUF_InitBufXXX(&nameBuf);
                STRBUF_Format(&nameBuf.sb, TEXT(IPADDR_FORMAT)TEXT_(":%hu"),
                    HOST_IPADDR_FORMAT_ARG(addr),port);

                memset(s, 0, sizeof(*s));
                ok = FILE_Init(&s->file, nameBuf.sb.s, False, &SocketIO);
                STRBUF_Destroy(&nameBuf.sb);

                if (ok) {
                    s->sock = sock;
                    s->eof = False;
                    return &s->file;
                }
            }            
            shutdown(sock, SHUT_RDWR);
        }
        closesocket(sock);
    }
    return NULL;
}
Beispiel #2
0
/**
 * Calls a callback on each entry in the directory.
 * Returns number of directory entries read, 0 if directory 
 * was empty, -1 if error occurs of callback function returns False. 
 * The current and parent directories ("." and "..") are not included 
 * in the count, and callback is not invoked on those directory entries 
 * because these are always valid file names in any directory.
 */
int FILE_List(Str dir, FileListCB cb, void * ctx) 
{
    int n = -1;
    StrBuf64 pattern;
    STRBUF_InitBufXXX(&pattern);
    if (!dir || !dir[0]) dir = TEXT(".");
    if (STRBUF_Copy(&pattern.sb, dir) && 
        STRBUF_Append(&pattern.sb, TEXT("\\*.*"))) {
        WIN32_FIND_DATA data;
        HANDLE hfind = FindFirstFile(pattern.sb.s, &data);
        if (hfind != INVALID_HANDLE_VALUE) {
            Str fname = data.cFileName;
            n = 0;
            do {
                if (StrCmp(fname, TEXT(".")) && 
                    StrCmp(fname, TEXT(".."))) {
                    if (cb && !(*cb)(dir, fname, ctx)) {
                        n = -1;
                        break;
                    }
                    n++;
                }
            } while (FindNextFile(hfind, &data));
            FindClose(hfind);
        }
    }
    STRBUF_Destroy(&pattern.sb);
    return n; 
}
Beispiel #3
0
/**
 * Parses Java version string. Also returns the number of digits in the
 * version string that have been successfully parsed. All non-parsed digits
 * are filled with zeros. The ndigits pointer may be NULL.
 */
Bool JVM_ParseVersion2(Str strVersion, JavaVersion * version, int* ndigits)
{
    int i;
    Str p = strVersion;
    StrBuf16 buf;
    STRBUF_InitBufXXX(&buf);

    memset(version, 0, sizeof(*version));
    for (i=0; i<JAVA_VERSION_SIZE; i++) {
        STRBUF_Clear(&buf.sb);
        while (*p && !IsDigit(*p)) p++;
        if (*p) {
            while (IsDigit(*p)) STRBUF_AppendChar(&buf.sb, *p++);
            if (STRBUF_Length(&buf.sb) > 0) {
                if (PARSE_Int(STRBUF_Text(&buf.sb),&(version->v[i]),10)) {
                    continue;
                }
            }
        }
        break;
    }

    if (ndigits) *ndigits = i;
    STRBUF_Destroy(&buf.sb);
    return (i >= 2);
}
Beispiel #4
0
/**
 * Creates a directory hierarhy.
 */
Bool FILE_MkDir(Str dir) 
{
    Bool ok = False;
    StrBuf64 entry;
    StrBuf* sb = &entry.sb;
    STRBUF_InitBufXXX(&entry);

    if (STRBUF_Copy(sb, dir)) {
        while (sb->len > 0 && FILE_IsFileSeparator(STRBUF_LastChar(sb))) {
            STRBUF_SetLength(sb, sb->len-1);
        }

        /* check if the directory already exists */
        if (FILE_IsDir(STRBUF_Text(sb)) ||
            FILE_CreateDir(STRBUF_Text(sb))) {
            ok = True;

        } else {

            /* directory does not exists, walk the hierarhy */
            int pos = 0;
            int next = 0;
            while ((next = FILE_FindSeparator(dir+pos)) >= 0) {
                STRBUF_Clear(sb);
                if (next == 0) {
                    pos++;
                    continue;
                } else {
                    if (!STRBUF_CopyN(sb,dir,pos+next) || 
                        !FILE_CreateDir(STRBUF_Text(sb))) {
                        break;
                    }
                    pos += next + 1;
                }
            }
                
            /* final test */
            if (STRBUF_Copy(sb, dir)) {
                while (sb->len && FILE_IsFileSeparator(STRBUF_LastChar(sb))) {
                    STRBUF_SetLength(sb, sb->len-1);
                }
                if (FILE_IsDir(STRBUF_Text(sb)) ||
                    FILE_CreateDir(STRBUF_Text(sb))) {
                    ok = True;
                }
            }
        }
    }

    STRBUF_Destroy(sb);
    return ok;
}
Beispiel #5
0
/**
 * FILE_RmDir callback. Recursively destroys the directory contents
 */
STATIC Bool RmDirCB(Str dir, Str fname, void * ctx) 
{
    Bool ok = False;
    StrBuf64 path;
    UNREF(ctx);
    STRBUF_InitBufXXX(&path);
    if (STRBUF_Copy(&path.sb, dir) && 
        STRBUF_AppendChar(&path.sb, FILE_SEPARATOR_CHAR) &&
        STRBUF_Append(&path.sb, fname)) {
        ok = True;
        if (FILE_IsDir(path.sb.s)) {
            FILE_RmDir(path.sb.s, True);
        } else {
            FILE_Delete(path.sb.s);
        }
    }
    STRBUF_Destroy(&path.sb);
    return ok;
}
Beispiel #6
0
/**
 * This routine runs EXPAT parser
 */
Bool XML_ParseStream(File * f, const XMLCallbacks * cb, void * ctx)
{
    XML_Parser parser;
    Bool ok = False;

    /* set up memory management functions */
    XML_Memory_Handling_Suite mem;
    memset(&mem, 0, sizeof(mem));
    mem.malloc_fcn = MEM_Alloc;
    mem.realloc_fcn = MEM_Realloc;
    mem.free_fcn = MEM_Free;
    
    /* create parser */
    parser = XML_ParserCreate_MM(NULL,&mem,NULL);
    if (parser) {
        int bufsize = 4096;
        void * buf = NULL;
        Bool done = False;

        /* initialize EXPAT */
        ExpatContext expat;
        expat.ctx = ctx;
        expat.cb = (*cb);
        STRBUF_Init(&expat.sb);
        BUFFER_Init(&expat.buf);
        VECTOR_Init(&expat.atts, 0, NULL, NULL);
        
        /* initialize the parser */
        XML_SetElementHandler(parser, EXPAT_StartElement, EXPAT_EndElement);
        XML_SetCharacterDataHandler(parser, EXPAT_Characters);
        XML_SetUserData(parser, &expat);

        /* 
         * By obtaining the buffer from Expat with the XML_GetBuffer,
         * we can avoid double copying of the input.
         */
        ok = True;
        while (ok && !done && (buf = XML_GetBuffer(parser,bufsize)) != NULL) {
            int len = -1;
            if (!FILE_Eof(f)) {
                len = FILE_Read(f, buf, bufsize);
            }
            if (len <= 0) {
                done = True;
                len = 0;
            }
            ok = XML_ParseBuffer(parser, len, done);
        }

#if DEBUG
        if (!ok) {
            enum XML_Error code = XML_GetErrorCode(parser);
            int l = XML_GetCurrentLineNumber(parser);
            int c = XML_GetCurrentColumnNumber(parser);
            Str fname = FILE_Name(f);
            const char * msg = XML_ErrorString(code);
            if (!fname) fname = TEXT("<noname>");
            if (!msg) msg = "<no message>";
#  ifdef _WIN32
            TRACE4("EXPAT: %s: line %d, column %d: %hs\n",fname,l,c,msg);
#  else  /* _WIN32 */
            TRACE4("EXPAT: %s: line %d, column %d: %s\n",fname,l,c,msg);
#  endif /* _WIN32 */
        }
#endif /* DEBUG */

        if (!buf) ok = False;

        /* deallocate parser */
        XML_ParserFree(parser);
        STRBUF_Destroy(&expat.sb);
        BUFFER_Destroy(&expat.buf);
        VECTOR_Destroy(&expat.atts);
    }

    return (ok);
}
Beispiel #7
0
/**
 * Discovers all available JVMs. If no JVMs are discovered, returns NULL.
 * In addition to the standard directories, also looks in the additional
 * directories specified by the dirs array. Note that this directory list
 * replaces the default list ("jre","../jre") used by JVM_Find, it does not
 * adds new directories to the list.
 */
JVMSet * JVM_Find2(const Str dirs[], int n)
{
    JVMSet * jvms = MEM_New(JVMSet);
    if (jvms) {
        memset(jvms, 0, sizeof(*jvms));
        if (VECTOR_Init(&jvms->found, 0, JVM_VectorEquals, JVM_VectorFree)) {

            /* Look for JVMs in the Windows registry */
            JVM_Discover(jvms);

            /* Look for JVMs in the additional directories */
            if (n > 0) {
                int i;
                StrBuf sb,sb2;
                Char* baseDir = NULL;
                STRBUF_Init(&sb);
                STRBUF_Init(&sb2);
                TRACE("JNILIB: checking special directories\n");
                for (i=0; i<n; i++) {
                    Str javaHome = NULL;
                    JvmPathType pathType = JVM_GetPathType(dirs[i]);
                    if (pathType == JvmPathRelative) {
                        LPTSTR filePath;
                        TRACE1("JNILIB: relative path: %s\n",dirs[i]);
                        if (baseDir) {
                            STRBUF_Copy(&sb, baseDir);
                        } else {
                            int separator;
                            JVM_GetModuleFileName(NULL,&sb);
                            STRBUF_Replace(&sb, '/', '\\');
                            separator = STRBUF_LastIndexOf(&sb,'\\');
                            STRBUF_SetLength(&sb,separator+1);
                            baseDir = STRBUF_Dup(&sb);
                            if (!baseDir) continue;
                            TRACE1("JNILIB: base dir: %s\n",baseDir);
                        }
                        STRBUF_Append(&sb, dirs[i]);
                        STRBUF_Replace(&sb, '/', '\\');
                        STRBUF_Alloc(&sb2, STRBUF_Length(&sb));
                        sb2.len = GetFullPathName(STRBUF_Text(&sb), 
                            sb2.alloc, sb2.s, &filePath);
                        ASSERT(sb2.len && sb2.s[0]);
                        javaHome = STRBUF_Text(&sb2);
                    } else if (pathType == JvmPathAbsolute) {
                        TRACE1("JNILIB: absolute path: %s\n",dirs[i]);
                        javaHome = dirs[i];
                    } else if (pathType == JvmPathSystem) {
                        /* directory on the system drive */
                        TRACE1("JNILIB: system path: %s\n",dirs[i]);
                        STRBUF_Alloc(&sb,GetSystemDirectory(NULL,0)+1);
                        STRBUF_SetLength(&sb,GetSystemDirectory(sb.s,sb.alloc));
                        STRBUF_Clear(&sb2);
                        STRBUF_AppendChar(&sb2,STRBUF_CharAt(&sb,0));
                        STRBUF_AppendChar(&sb2,':');
                        STRBUF_Append(&sb2, dirs[i]);
                        javaHome = STRBUF_Text(&sb2);
                    } else {
                        TRACE1("JNILIB: invalid path: %s\n",dirs[i]);
                        continue;
                    }
                    if (javaHome) {
                        TRACE1("JNILIB: Java home: %s\n",javaHome);
                        if (FILE_IsDir(javaHome)) {
                            JVM* jvm = JVM_CreateDirContext(javaHome);
                            if (jvm) {
                                jvm->flags |= JVM_FLAG_SPECIAL;
                                if (JVM_Add(jvms, jvm) && !jvms->specialVM) {
                                    jvms->specialVM = jvm;
                                }
                            }
                        } else {
                            TRACE1("JNILIB: no such directory: %s\n",javaHome);
                        }
                    }
                }
                MEM_Free(baseDir);
                STRBUF_Destroy(&sb);
                STRBUF_Destroy(&sb2);
            }

            /* Did we find anything? */
            if (!VECTOR_IsEmpty(&jvms->found)) {
                JVM_Sort(jvms, JVM_DefaultSort);
                TRACE1("JNILIB: found %d JVM(s)\n",VECTOR_Size(&jvms->found));
                return jvms;
            }
            TRACE("JNILIB: found no JVMs\n, sorry");
            VECTOR_Destroy(&jvms->found);
        }
        MEM_Free(jvms);
    }
    return NULL;
}
Beispiel #8
0
void DIR_ItrDestroy(DirIterator * di)
{
    STRBUF_Destroy(&di->dirName);
    STRBUF_Destroy(&di->fileName);
}