Пример #1
0
ut_fileOut
ut_fileOutNew(
    const os_char *name,
    const os_char *mode)
{
    ut_fileOut stream;
    os_char *fname;
    os_char * filename;
    
    stream = os_malloc((size_t)OS_SIZEOF(ut_fileOut));
    if (ut_outputdir) {
        fname = os_malloc(strlen(ut_outputdir) + strlen(os_fileSep()) + strlen(name) + 1);
        os_sprintf(fname, "%s%s%s", ut_outputdir, os_fileSep(), name);
    } else {
        fname = os_strdup(name);
    }
    filename = os_fileNormalize(fname); 
    stream->file = fopen(filename, mode);
    os_free(fname);
    os_free(filename);

    if (stream->file == NULL) {
        os_free(stream);
        stream = NULL;
    }
    return stream;
}
Пример #2
0
void
idl_openJavaPackage (
    idl_scope scope,
    const char *name)
{
    os_char *fname = NULL;
    os_char *package_file = NULL;
    os_char *package = NULL;

    package_file = idl_scopeStackJava(scope, os_fileSep(), name);
    fname = os_malloc(strlen (package_file) + strlen (".java") + 1);
    os_strcpy(fname, package_file);
    os_strcat(fname, ".java");
    if (idl_createDir(fname)) {
	    idl_fileSetCur(idl_fileOutNew (fname, "w"));
        if (idl_fileCur() == NULL) {
            idl_fileOpenError(fname);
        }
        /* Add package header if applicable */
        package = idl_scopeStackJava(scope, ".", NULL);
        if (package != NULL && strlen (package) == 0) {
            os_free (package);
            package = NULL;
        }
        if (package != NULL) {
            idl_fileOutPrintf(idl_fileCur(), "package %s;\n", package);
            idl_fileOutPrintf(idl_fileCur(), "\n");
        }
    } else {
	    idl_fileSetCur(NULL);
    }
    os_free(package);
    os_free(package_file);
    os_free(fname);
}
Пример #3
0
char *
os_locate(
    const char *name,
    os_int32 permission)
{
    char *result = NULL;
    const char *fsep;
    char *path;
    char *fullName;
    os_result osr;
    std_splitList dirs;
    int dirsSize;
    char *curDir;
    int i;

    if (name) {
        fsep = os_fileSep();
        /* If the command contains an absolute or relative path,
         * only check the permissions, otherwise search the file
         * in the PATH environment */

        if ((*name == '.') ||
            (strncmp(name, fsep, strlen(fsep)) == 0)) {
            osr = os_access(name, permission);
            if (osr == os_resultSuccess) {
                result = os_strdup(name);
            }
        } else {
            /* No relative path in name, so walk over the
             * whole path */
            path = os_getenv(PATH_ENVVAR);
            dirs = std_splitListNew(path, OS_PATHSEPCHAR);
            dirsSize = std_splitListSize(dirs);
            for (i=0; (i<dirsSize) && !result; i++) {
                curDir = std_splitListGet(dirs, i);
                fullName = (char *)os_malloc(
                   strlen(curDir) + strlen(fsep) + strlen(name) + 1);
                if (fullName) {
                    os_strcpy(fullName, curDir);
                    os_strcat(fullName, fsep);
                    os_strcat(fullName, name);
                    /* Check file permissions. Do not have to check if file
                     * exists, since permission check fails when the file
                     * does not exist. */
                   osr = os_access(fullName, permission);
                   if (osr == os_resultSuccess) {
                       result = fullName;
                   } else {
                       os_free(fullName);
                   }
                }
            }
            std_splitListFree(dirs);
        }
    }
    return result;
}
Пример #4
0
static int
idl_createDir (
    os_char* fname)
{
    char *pathName;
    os_result statRes;
    struct os_stat statbuf;
    char* outdir;
    os_char* stackScope;
    os_char* token;
    const os_char* osSep;

    /* to allow nested modules which may potentially lead to very long paths,
     * pathName is not allocated fixed-size on stack
     * but dynamically on heap
     */
    pathName = os_malloc(1);
    pathName[0] = '\0';
    outdir = idl_dirOutCur();
    if(outdir){
        pathName = os_realloc(pathName, strlen(outdir) + strlen(os_fileSep()) + 1);
        if (pathName == NULL) {
            printf("Memory allocation failure when creating idl directory\n");
            return 0;
        }
        os_sprintf(pathName, "%s%s", outdir, os_fileSep());
    }
    /* make sure that we replace the os file seperator with a simple character
     * like '/', this will allow us to parse easier later.
     */
    osSep = os_fileSep();
    stackScope = idl_genJavaHelperSubstitute(fname, osSep, "/", NULL);

    if(stackScope[0] != '\0'){ /* strlen(stackScope) > 0 */
        do
        {
            token = strchr(stackScope, '/');
            /* make sure this is not the last part of the file name, for example
             * for the file name org/opensplice/foo.java we only want to create
             * directories for org and opensplice, the foo.java part is not a
             * directory. So we can simply state if no '/' char can be
             * found then we must be at the end part of the file name!
             */
            if(token)
            {
                *token = '\0';
                token++;
                /* reallocate pathName to include stackScope */
                pathName = os_realloc(pathName, strlen(pathName) + strlen(stackScope) + 1);
                if (pathName == NULL) {
                    printf("Memory allocation failure when trying to create idl directory %s, \
                            probably the path has grown too long\n", stackScope);
                    return 0;
                }
                os_strcat (pathName, stackScope);
                stackScope = token;
                statRes = os_stat (pathName, &statbuf);
                if (statRes == os_resultFail) {
                    /* @todo                                                      */
                    /* Assume the file does not exist. On some platforms          */
                    /* a check to see if errno == ENOENT would be more conclusive */
                    /* That fails on WIN32 however because stat is not fully      */
                    /* compatible. Only an os_stat implementation can solve that. */

                    os_mkdir(pathName, 0777);
                    statRes = os_stat(pathName, &statbuf);
                } else {
                    if (!OS_ISDIR(statbuf.stat_mode)) {
                        printf ("File %s already exists, but is not a directory\n", pathName);
                        os_free(pathName);
                        return 0;
                    }
                }
                if (statRes == os_resultFail) {
                    printf ("Error when creating directory %s\n", pathName);
                    os_free(pathName);
                    return 0;
                }
                /* reallocate pathName to include os_fileSep() */
                pathName = os_realloc(pathName, strlen(pathName) + strlen(stackScope) + strlen(os_fileSep()) + 1);
                if (pathName == NULL) {
                    printf("Memory allocation failure when trying to add the \
                            file separator to idl directory %s", stackScope);
                    return 0;
                }
                os_strcat (pathName, os_fileSep());
            }
        } while(token);