Exemplo n.º 1
0
/*--------------------------------------------------------------------------*/
char *createtempfilename(const char *prefix, BOOL bShortFormat)
{
    char *tempfilename = NULL;
#ifdef _MSC_VER
    wchar_t *wcprefix = to_wide_string(prefix);
    wchar_t *wcresult = createtempfilenameW(wcprefix, bShortFormat);

    tempfilename = wide_string_to_UTF8(wcresult);

    if (wcresult)
    {
        FREE(wcresult);
        wcresult = NULL;
    }
    if (wcresult)
    {
        FREE(wcresult);
        wcresult = NULL;
    }
#else
    char *TmpDir = getTMPDIR();
    if (TmpDir)
    {
        char TempFileName[PATH_MAX];
        sprintf(TempFileName, "%s/%sXXXXXX", TmpDir, prefix);
        int fd = mkstemp(TempFileName);
        if (fd != -1)
        {
            close(fd);
        }
        tempfilename = strdup(TempFileName);
    }
#endif
    return tempfilename;
}
Exemplo n.º 2
0
void clearTMPDIR()
{
    char * tmpdir = getTMPDIR();
    Parser parser;
    parser.releaseTmpFile();
    removedir(tmpdir);
    FREE(tmpdir);
}
Exemplo n.º 3
0
static FILE *getfile(PVStr(path),PCStr(mode),int fifo){
	const char *tmpdir;
	IStr(dgtmp,1024);
	CStr(npath,1024);
	int max = 64;
	FILE *fp;
	int fi;
	int fx;

	/*
	sprintf(path,"/tmp/sftpgw.%d.%d",getpid(),time(NULL));
	 */
	tmpdir = "/tmp";
	if( DELEGATE_getEnv("TMPDIR") )
	if( tmpdir = getTMPDIR() ){
		DEBUG("--SFTP TMPDIR=%s\n",tmpdir);
	}
	sprintf(dgtmp,"%s/dg-sftpgw",tmpdir);
	if( !fileIsdir(dgtmp) ){
		if( mkdirRX(dgtmp) != 0 ){
		}
	}
	sprintf(path,"%s/sftpgw-%d.%d",dgtmp,getpid(),itime(NULL));
	if( fifo ){
		if( Mkfifo(path,0600) != 0 ){
			DEBUG("--SFTP cant create FIFO: %s\n",path);
			return 0;
		}
		fp = NULL;
	}else{
		if( (fp = fopen(path,mode)) == 0 ){
			DEBUG("--SFTP cant create FILE: %s\n",path);
			return 0;
		}
	}
	fx = trand1(max);
	for( fi = 0; fi < max; fi++ ){
		/*
		sprintf(npath,"/tmp/sftpgw.%02x",fx);
		 */
		sprintf(npath,"%s/sftpgw-%02x",dgtmp,fx);
		if( rename(path,npath) == 0 ){
			DEBUG("--SFTP TEMP FILE: %s\n",npath);
			strcpy(path,npath);
			return fp;
		}
		fx = (fx + 1) % max;
	}
	DEBUG("--SFTP ERROR: can't rename %s\n",path);
	return fp;
}
Exemplo n.º 4
0
int NIS_inactive(PCStr(ypdomain),int set){ /* v9.9.12 new-140823d */
	IStr(path,256);
	const char *tmpdir;

	if( (tmpdir = getTMPDIR()) == 0 )
		tmpdir = "/tmp";
	sprintf(path,"%s/%s/nis-inactive-%s",tmpdir,RES_HC_DIR,ypdomain);
	if( set ){
		File_touch(path,time(0));
		return 1;
	}
	if( File_is(path) ){
		if( time(0) - File_mtime(path) < RES_HC_EXPIRE ){
			return 1;
		}
	} 
	return 0;
}
Exemplo n.º 5
0
static FILE *hosts_cache(int byname,PCStr(nameaddr),PCStr(mode),PVStr(cpath))
{	unsigned int hidx;
	CStr(cdirs,1024);
	CStr(cdirg,1024);
	CStr(cdir1,1024);
	FILE *fp;
	const char *tmpdir;
	CStr(resid,64);
	CStr(conf,2048);
	CStr(idfile,1024);
	FILE *cfp;

	if( cache_cantopen && time(0) < cache_cantopen+RES_HC_EXPIRE )
		return NULL;
	cache_cantopen = 0;
	RES_confid(AVStr(resid));

	if( (tmpdir = getTMPDIR()) == 0 )
		tmpdir = "/tmp";

	hidx = FQDN_hash(nameaddr);
	if( isFullpath(RES_HC_DIR) )
		strcpy(cdirs,RES_HC_DIR);
	else	sprintf(cdirs,"%s/%s",tmpdir,RES_HC_DIR);
	sprintf(cdirg,"%s/%s",cdirs,resid);
	sprintf(cdir1,"%s/%s",cdirg,byname?"byname":"byaddr");

	if( byname != 1 ){
		strcat(cdir1,"6");
	}

	sprintf(cpath,"%s/%02x",cdir1,hidx%32);
	if( *mode == 'r' )
		debug(DBG_ANY,"lookup cache: %s\n",cpath);
	else	debug(DBG_ANY,"create cache: %s\n",cpath);

	fp = fopen(cpath,mode);

	if( fp == NULL && *mode != 'r' ){
		mkdirShared(tmpdir,0);
		mkdirShared(cdirs,0);
		if( mkdirShared(cdirg,0) == 0 ){
			RES_getconf(AVStr(conf));
			sprintf(idfile,"%s/config",cdirg);
			cfp = fopen(idfile,"w");
			if( cfp == NULL ){
				debug(DBG_FORCE,"CACHE can't open: %s (%d)\n",
					idfile,errno);
				return NULL;
			}
			fprintf(cfp,"created by uid=%d pid=%d time=%d\n",
				getuid(),getpid(),itime(0));
			fprintf(cfp,"configuration:\n");
			fputs(conf,cfp);
			fclose(cfp);
			chmodIfShared(idfile,0644);
		}
		mkdirShared(cdir1,0);

		if( (fp = fopen(cpath,mode)) == NULL ){
			cache_cantopen = time(0);
			debug(DBG_FORCE,"CACHE cannot create: %s\n",cpath);
		}
	}
	if( fp != NULL && *mode != 'r' )
		chmodIfShared(cpath,0666);

	return fp;
}
Exemplo n.º 6
0
/*--------------------------------------------------------------------------*/
int sci_xls_open(char *fname, unsigned long fname_len)
{
#undef IN
#define max_char_xls_open 256
    int i = 0, m1 = 0, n1 = 0, l1 = 0, l2 = 0, one = 1, fd = 0, f_swap = 0;
    int ierr = 0, ns = 0, result = 0;
    double res;
    char **sst = NULL;
    char **Sheetnames = NULL;
    int *Abspos = NULL;
    int nsheets = 0;
    char *filename_IN = NULL;
    char TMP[max_char_xls_open];

    char sep[2];
    char *TMPDIR = NULL;

#ifdef _MSC_VER
    sep[0] = '\\';
#else
    sep[0] = '/';
#endif
    sep[1] = '\0';

    CheckLhs(4, 4);
    CheckRhs(1, 1);

    if (VarType(1) != sci_strings)
    {
        Scierror(999, "%s: Invalid type of input argument: String expected.", fname);
        return 0;
    }

    /*  checking variable file */
    GetRhsVar(1, STRING_DATATYPE, &m1, &n1, &l1);

    filename_IN = expandPathVariable(cstk(l1));
    if (filename_IN)
    {
        /* bug 5615 */
        /* remove blank characters @ the end */
        int len = (int)strlen(filename_IN);
        int i = 0;

        if (len >= 1)
        {
            for (i = len - 1; i >= 0; i--)
            {
                if (filename_IN[i] == ' ')
                {
                    filename_IN[i] = '\0';
                }
                else
                {
                    break;
                }
            }
        }

        if (!FileExist(filename_IN))
        {
            Scierror(999, _("The file %s does not exist.\n"), filename_IN);
            return 0;
        }
    }

    TMPDIR = getTMPDIR();
    strcpy(TMP, TMPDIR);
    if (TMPDIR)
    {
        FREE(TMPDIR);
        TMPDIR = NULL;
    }

    strcat(TMP, sep);
    strcat(TMP, xls_basename(filename_IN));
    result = ripole(filename_IN, TMP, 0, 0);
    if (result != OLE_OK)
    {
        if (result == OLEER_NO_INPUT_FILE)
        {
            Scierror(999, _("The file %s does not exist.\n"), filename_IN);
        }
        else if (result == OLEER_NOT_OLE_FILE ||
                 result == OLEER_INSANE_OLE_FILE ||
                 result == OLEER_LOADFAT_BAD_BOUNDARY || result == OLEER_MINIFAT_READ_FAIL || result == OLEER_PROPERTIES_READ_FAIL)
        {
            Scierror(999, _("%s: File %s is not an ole2 file.\n"), fname, filename_IN);
            if (filename_IN)
            {
                FREE(filename_IN);
                filename_IN = NULL;
            }
        }
        else if (result == -1)
        {
            Scierror(999, _("%s: Cannot open file %s.\n"), fname, filename_IN);
            if (filename_IN)
            {
                FREE(filename_IN);
                filename_IN = NULL;
            }
        }
        return 0;
    }
    strcat(TMP, sep);
    strcat(TMP, "Workbook");
    C2F(mopen) (&fd, TMP, "rb", &f_swap, &res, &ierr);
    if (ierr != 0)
    {
        Scierror(999, _("%s: There is no xls stream in the ole2 file %s.\n"), fname, filename_IN);
        if (filename_IN)
        {
            FREE(filename_IN);
            filename_IN = NULL;
        }
        return 0;
    }

    if (filename_IN)
    {
        FREE(filename_IN);
        filename_IN = NULL;
    }

    CreateVar(Rhs + 1, MATRIX_OF_INTEGER_DATATYPE, &one, &one, &l2);
    *istk(l2) = fd;             /* logical unit */

    xls_open(&ierr, &fd, &sst, &ns, &Sheetnames, &Abspos, &nsheets);
    /*return *err:
     * 0 = OK
     * 1 = not an OLE file
     * 2 = no Workbook included
     * 3 = memory allocation problem
     * 4 = incorrect file
     * 5 = not a BIFF8 xls file
     */
    switch (ierr)
    {
        case 0:
            /* OK */
            break;

        case 1:
            Scierror(999, _("%s: Not an ole2 file.\n"), fname);
            return 0;

        case 2:
            Scierror(999, _("%s: The file has no Workbook directory.\n"), fname);
            return 0;

        case 3:
            Scierror(999, _("%s: No more memory.\n"), fname);
            return 0;

        case 4:
            Scierror(990, _("%s: Incorrect or corrupted file.\n"), fname);
            return 0;

        case 5:
            Scierror(999, _("%s: Only BIFF8 file format is handled.\n"), fname);
            return 0;

        default:
            break;

    }

    if (ns != 0)
    {
        /* Create a typed list to return the properties */
        CreateVarFromPtr(Rhs + 2, MATRIX_OF_STRING_DATATYPE, &one, &ns, sst);
        freeArrayOfString(sst, ns);
    }
    else
    {
        CreateVar(Rhs + 2, MATRIX_OF_DOUBLE_DATATYPE, &ns, &ns, &l2);
    }

    if (nsheets != 0)
    {
        /* Create a typed list to return the properties */
        CreateVarFromPtr(Rhs + 3, MATRIX_OF_STRING_DATATYPE, &one, &nsheets, Sheetnames);
        freeArrayOfString(Sheetnames, nsheets);

        CreateVar(Rhs + 4, MATRIX_OF_DOUBLE_DATATYPE, &one, &nsheets, &l2);
        for (i = 0; i < nsheets; i++)
        {
            *stk(l2 + i) = Abspos[i];
        }
        if (Abspos)
        {
            FREE(Abspos);
            Abspos = NULL;
        }
    }
    else
    {
        CreateVar(Rhs + 3, MATRIX_OF_DOUBLE_DATATYPE, &nsheets, &nsheets, &l2);
        CreateVar(Rhs + 4, MATRIX_OF_DOUBLE_DATATYPE, &nsheets, &nsheets, &l2);
    }

    LhsVar(1) = Rhs + 1;
    LhsVar(2) = Rhs + 2;
    LhsVar(3) = Rhs + 3;
    LhsVar(4) = Rhs + 4;

    PutLhsVar();

    return 0;
}
Exemplo n.º 7
0
/*--------------------------------------------------------------------------*/
int sci_dos(char *fname, void* pvApiCtx)
{
    SciErr sciErr;
    int *piAddressVarOne = NULL;
    int iType1	= 0;
    wchar_t *pStVarOne = NULL;

    char **Output = NULL;
    int numberoflines = 0;
    BOOL ECHOMODE = FALSE;

    CheckRhs(1, 2);
    CheckLhs(1, 3);

    if (Rhs == 2)
    {
        int *piAddressVarTwo = NULL;
        int m2 = 0, n2 = 0;
        int iType2 = 0;
        char *pStVarTwo = NULL;
        int lenStVarTwo = 0;

        sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
            return 0;
        }

        sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
            return 0;
        }

        if (iType2  != sci_strings )
        {
            Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 2);
            return 0;
        }

        sciErr = getMatrixOfString(pvApiCtx, piAddressVarTwo, &m2, &n2, &lenStVarTwo, NULL);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
            return 0;
        }

        if ( (m2 != n2) && (n2 != 1) )
        {
            Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname, 2);
            return 0;
        }

        pStVarTwo = (char*)MALLOC(sizeof(char) * (lenStVarTwo + 1));
        if (pStVarTwo)
        {
            sciErr = getMatrixOfString(pvApiCtx, piAddressVarTwo, &m2, &n2, &lenStVarTwo, &pStVarTwo);
            if (sciErr.iErr)
            {
                printError(&sciErr, 0);
                Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
                return 0;
            }

            if ( strcmp(pStVarTwo, "-echo") )
            {
                FREE(pStVarTwo);
                pStVarTwo = NULL;
                Scierror(999, _("%s: Wrong value for input argument #%d: '%s' expected.\n"), fname, 2, "-echo");
                return 0;
            }
            else
            {
                ECHOMODE = TRUE;
            }
        }
        else
        {
            Scierror(999, _("%s: No more memory.\n"), fname);
            return 0;
        }
    }

    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
        return 0;
    }

    sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
        return 0;
    }

    if (iType1  != sci_strings )
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 1);
        return 0;
    }

    if (isScalar(pvApiCtx, piAddressVarOne) == FALSE)
    {
        Scierror(999, _("%s: Wrong size for input argument #%d: string expected.\n"), fname, 1);
        return 0;
    }

    if (getAllocatedSingleWideString(pvApiCtx, piAddressVarOne, &pStVarOne))
    {
        printError(&sciErr, 0);
        Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
        return 0;
    }

    double exitCode = 0.;
    BOOL DetachProcessOption = FALSE;
    BOOL *StatusExit = NULL;

    DetachProcessOption = DetectDetachProcessInCommandLine(pStVarOne);
    exitCode = (double)spawncommand(pStVarOne, DetachProcessOption);
    freeAllocatedSingleWideString(pStVarOne);

    StatusExit = (BOOL*)MALLOC(sizeof(BOOL));

    if (DetachProcessOption)
    {
        if (strlen((const char *)(pipeSpawnErr.OutputBuffer)))
        {
            /* StdErr will be "Output" */
            *StatusExit = FALSE;
            Output = CreateOuput(&pipeSpawnErr, DetachProcessOption);
            numberoflines = pipeSpawnErr.NumberOfLines;
        }
        else
        {
            /* StdOut will be "Output" */
            *StatusExit = TRUE;
            Output = CreateOuput(&pipeSpawnOut, DetachProcessOption);
            numberoflines = pipeSpawnOut.NumberOfLines;
        }
    }
    else
    {
        char FileTMPDir[PATH_MAX + 16];
        BOOL bConvert = FALSE;

        char *TMPDirLong = getTMPDIR();
        char *TMPDirShort = getshortpathname(TMPDirLong, &bConvert);

        sprintf(FileTMPDir, "%s\\DOS.OK", TMPDirLong);
        FREE(TMPDirLong);
        TMPDirLong = NULL;
        FREE(TMPDirShort);
        TMPDirShort = NULL;

        if (FileExist(FileTMPDir))
        {
            DeleteFile(FileTMPDir);
            /* StdOut will be "Output" */
            *StatusExit = TRUE;
            Output = CreateOuput(&pipeSpawnOut, DetachProcessOption);
            numberoflines = pipeSpawnOut.NumberOfLines;
        }
        else
        {
            /* StdErr will be "Output" */
            *StatusExit = FALSE;
            Output = CreateOuput(&pipeSpawnErr, DetachProcessOption);
            numberoflines = pipeSpawnErr.NumberOfLines;
        }
    }

    if (ECHOMODE)
    {
        PrintOuput(Output, numberoflines);
    }

    if (Lhs == 1)
    {
        int m_out = 1, n_out = 1;
        sciErr = createMatrixOfBoolean(pvApiCtx, Rhs + 1, m_out, n_out, StatusExit);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            return 0;
        }

        LhsVar(1) = Rhs + 1;
    }
    else
    {
        int m_out2 = 1;
        int n_out2 = 1;

        if (Output && Output[0])
        {
            int m_out1 = numberoflines;
            int n_out1 = 1;
            sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, m_out1, n_out1, Output);
        }
        else
        {
            /* returns [] */
            int m_out1 = 0;
            int n_out1 = 0;
            sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m_out1, n_out1, NULL);
        }

        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            return 0;
        }

        LhsVar(1) = Rhs + 1;

        sciErr = createMatrixOfBoolean(pvApiCtx, Rhs + 2, m_out2, n_out2, StatusExit);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            return 0;
        }

        LhsVar(2) = Rhs + 2;
    }

    if (Lhs > 2)
    {
        int m_out3 = 1, n_out3 = 1;
        sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 3, m_out3, n_out3, &exitCode);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
            Scierror(999, _("%s: Memory allocation error.\n"), fname);
            return 0;
        }

        LhsVar(3) = Rhs + 3;
    }
    if (StatusExit)
    {
        FREE(StatusExit);
        StatusExit = NULL;
    }

    freeArrayOfString(Output, numberoflines);

    ClosePipeInfo(pipeSpawnOut);
    ClosePipeInfo(pipeSpawnErr);

    PutLhsVar();

    return 0;
}
Exemplo n.º 8
0
/** \brief parse the given file command */
void ParserSingleInstance::parse(const char *command)
{
    size_t len = strlen(command);

    yylloc.first_line = yylloc.last_line = 1;
    yylloc.first_column = yylloc.last_column = 1;
#ifdef _MSC_VER
    char szFile[MAX_PATH];
    char* pstTmpDIr = getTMPDIR();
    os_sprintf(szFile, "%s\\%s", pstTmpDIr, "command.temp");
    FREE(pstTmpDIr);
    if (fileLocker)
    {
        fclose(fileLocker);
    }

    errno_t err;
    err = fopen_s(&yyin, szFile, "w");
    if (err)
    {
        ParserSingleInstance::setExitStatus(Parser::Failed);
        ParserSingleInstance::resetErrorMessage();
        wchar_t szError[bsiz];
        wchar_t* wszFile = to_wide_string(szFile);
        os_swprintf(szError, bsiz, _W("%ls: Cannot open file %ls.\n").c_str(), L"parser", wszFile);
        FREE(wszFile);
        appendErrorMessage(szError);
        return;
    }

    fwrite(command, sizeof(char), len, yyin);
    fclose(yyin);
    fopen_s(&yyin, szFile, "r");
#endif

#ifdef __APPLE__
    char szFile[PATH_MAX];
    char* pstTmpDIr = "/tmp";
    sprintf(szFile, "%s/%s", getTMPDIR(), "command.temp");
    //FREE(pstTmpDIr);
    if (fileLocker)
    {
        fclose(fileLocker);
    }
    yyin = fopen(szFile, "w");
    fwrite(command, 1, len, yyin);
    fclose(yyin);
    yyin = fopen(szFile, "r");
#endif


#ifndef _MSC_VER
#ifndef __APPLE__
    yyin = fmemopen((void*)command, len, "r");
#endif
#endif

    ParserSingleInstance::disableStrictMode();
    ParserSingleInstance::setFileName(L"prompt");
    ParserSingleInstance::setTree(nullptr);
    ParserSingleInstance::setExitStatus(Parser::Succeded);
    ParserSingleInstance::resetControlStatus();
    ParserSingleInstance::resetErrorMessage();

    yyparse();

    fclose(yyin);
#ifdef _MSC_VER
    DeleteFileA(szFile);
#endif

#ifdef _MSC_VER
    //reopen a file to prevents max file opened.
    fopen_s(&fileLocker, szFile, "w");
#endif
#ifdef __APPLE__
    fileLocker = fopen(szFile, "w");
#endif
}