/*-----------------------------------------------------------------\
 Function Name	: OLEUNWRAP_save_stream
 Returns Type	: int
 	----Parameter List
	1. char *fname,
	2.  char *stream,
	3.  size_t bytes ,
 	------------------
 Exit Codes	:
 Side Effects	:
--------------------------------------------------------------------
 Comments:

--------------------------------------------------------------------
 Changes:

\------------------------------------------------------------------*/
int OLEUNWRAP_save_stream( struct OLEUNWRAP_object *oleuw, char *fname, char *decode_path, char *stream, size_t bytes )
{
	char *full_name;
	FILE *f;
	int result = 0;

	DUW LOGGER_log("%s:%d:OLEUNWRAP_save_stream:DEBUG: fname=%s, decodepath=%s, size=%ld"
			,FL
			,fname
			,decode_path
			,bytes
			);

	full_name = PLD_dprintf("%s/%s", decode_path, fname );
	if (full_name == NULL)
	{
		LOGGER_log(_("%s:%d:OLEUNWRAP_save_stream:ERROR: Unable to create filename string from '%s' and '%s'"),FL,fname,decode_path);
		return -1;
	}

	wcfopen(f, full_name, "wb");
	if (f != NULL)
	{
		size_t write_count;

		write_count = fwrite( stream, 1, bytes, f );
		if (write_count != bytes)
		{
			LOGGER_log(_("%s:%d:OLEUNWRAP_save_stream:WARNING: Only wrote %d of %d bytes to file %s\n"),FL, write_count, bytes, full_name );
		}

		fclose(f);


	} else {
		LOGGER_log(_("%s:%d:OLEUNWRAP_save_stream:ERROR: Unable to open %s for writing (%s)\n"),FL,full_name, strerror(errno));
		result = -1;
	}

	if (full_name) FREE(full_name);

	DUW LOGGER_log("%s:%d:OLEUNWRAP_save_stream:DEBUG: Done saving '%s'",FL, fname);

	return result;
}
Exemple #2
0
/*------------------------------------------------------------------------*/
BOOL HistoryFile::writeToFile(std::string filename)
{
    BOOL bOK = FALSE;

    if (this->Commands.empty())
    {
        return bOK;
    }
    else
    {
        FILE *pFile = NULL;

        if (filename.empty())
        {
            return bOK;
        }

        wcfopen(pFile , (char*)filename.c_str(), "wt");

        if (pFile)
        {
            list<CommandLine>::iterator it_commands;
            for (it_commands = this->Commands.begin(); it_commands != this->Commands.end(); ++it_commands)
            {
                std::string line = (*it_commands).get();
                if (!line.empty())
                {
                    fputs(line.c_str(), pFile);
                    fputs("\n", pFile);
                }
            }
            fclose(pFile);
            bOK = TRUE;
        }
    }
    return bOK;
}
Exemple #3
0
/*--------------------------------------------------------------------------*/
SCICOS_BLOCKS_IMPEXP void writec(int *flag, int *nevprt,
                                 double *t, double xd[],
                                 double x[], int *nx,
                                 double z[], int *nz,
                                 double tvec[], int *ntvec,
                                 double rpar[], int *nrpar,
                                 int ipar[], int *nipar,
                                 double *inptr[], int insz[],
                                 int *nin, double *outptr[],
                                 int outsz[], int *nout)
/*
ipar[1]   = lfil : file name length
ipar[2:4] = fmt  : numbers type ascii code
ipar[5]   = n : buffer length in number of records
ipar[6]   = swap
ipar[7:6+lfil] = character codes for file name
*/

{
    char str[100], type[4];
    int job = 1, three = 3;
    FILE *fd = NULL;
    int n = 0, k = 0, i = 0, ierr = 0;
    double *buffer = NULL, *record = NULL;

    --ipar;
    --z;
    fd = (FILE *)(long)z[2];
    buffer = (z + 3);
    ierr = 0;
    /*
    k    : record counter within the buffer
    */

    if (*flag == 2 && *nevprt > 0) /* add a new record to the buffer */
    {
        n    = ipar[5];
        k    = (int)z[1];
        /* copy current record to output */
        record = buffer + (k - 1) * (insz[0]);

        for (i = 0; i < insz[0]; i++)
        {
            record[i] = *(inptr[0] + i);
        }
        if (k < n)
        {
            z[1] = z[1] + 1.0;
        }
        else  /* buffer is full write it to the file */
        {
            for (i = 0; i < three; ++i)
            {
                type[i] = (char) ipar[i + 2];
            }
            for (i = 2; i >= 0; i--)
                if (type[i] != ' ')
                {
                    type[i + 1] = '\0';
                    break;
                }
            mput2(fd, ipar[6], buffer, ipar[5]*insz[0], type, &ierr);
            if (ierr != 0)
            {
                *flag = -3;
                return;
            }
            z[1] = 1.0;
        }
    }
    else if (*flag == 4)
    {
        for (i = 0; i < ipar[1]; ++i)
        {
            str[i] = (char) ipar[i + 7];
        }
        str[ipar[1]] = '\0';
        wcfopen(fd, str, "wb");
        if (!fd )
        {
            scicos_print(_("Could not open the file!\n"));
            *flag = -3;
            return;
        }
        z[2] = (long)fd;
        z[1] = 1.0;
    }
    else if (*flag == 5)
    {
        if (z[2] == 0)
        {
            return;
        }
        k    = (int) z[1];
        if (k >= 1) /* flush rest of buffer */
        {
            for (i = 0; i < three; ++i)
            {
                type[i] = (char) ipar[i + 2];
            }
            for (i = 2; i >= 0; i--)
                if (type[i] != ' ')
                {
                    type[i + 1] = '\0';
                    break;
                }
            mput2(fd, ipar[6], buffer, (k - 1)*insz[0], type, &ierr);
            if (ierr != 0)
            {
                *flag = -3;
                return;
            }
        }
        fclose(fd);
        z[2] = 0.0;
    }
    return;
}
Exemple #4
0
/*--------------------------------------------------------------------------*/
SCICOS_BLOCKS_IMPEXP void writeau(int *flag, int *nevprt,
                                  double *t, double xd[],
                                  double x[], int *nx,
                                  double z[], int *nz,
                                  double tvec[], int *ntvec,
                                  double rpar[], int *nrpar,
                                  int ipar[], int *nipar,
                                  double *inptr[], int insz[],
                                  int *nin, double *outptr[],
                                  int outsz[], int *nout)
/*
ipar[1]   = lfil : file name length
ipar[2:4] = fmt  : numbers type ascii code
ipar[5]   = n : buffer length in number of records
ipar[6]   = swap
ipar[7:6+lfil] = character codes for file name
*/
{
    FILE *fd = NULL;
    int n = 0, k = 0, i = 0, ierr = 0;
    double *buffer = NULL, *record = NULL;
    /*  long offset;*/
    int SCALE  = 32768;
    int BIAS   =   132;
    int CLIP   = 32635;
    int OFFSET =   335;
    double y = 0.;
    int sig = 0;
    int e = 0;
    double f = 0.;


    --ipar;
    --z;
    fd = (FILE *)(long)z[2];
    buffer = (z + 3);
    ierr = 0;
    /*
    k    : record counter within the buffer
    */

    if (*flag == 2 && *nevprt > 0)
    {
        /* add a new record to the buffer */
        n    = ipar[5];
        k    = (int)z[1];
        /* copy current record to output
        printf("%i\n",k);*/
        record = buffer + (k - 1) * (*nin);

        for (i = 0; i < *nin; i++)
        {
            y = *inptr[i];
            y = SCALE * y;
            if (y < 0.0)
            {
                y = -y;
                sig = -1;
            }
            else
            {
                sig = 1;
            }
            if (y > CLIP)
            {
                y = CLIP;
            }
            y = y + BIAS;
            f = frexp(y, &e);
            y = 64 * sig - 16 * e - (int) (32 * f) + OFFSET;
            record[i] = y;
        }
        if (k < n)
        {
            z[1] = z[1] + 1.0;
        }
        else
        {
            mput2(fd, ipar[6], buffer, ipar[5] * (*nin), "uc", &ierr);
            if (ierr != 0)
            {
                *flag = -3;
                return;
            }
            z[1] = 1.0;

        }

    }
    else if (*flag == 4)
    {
        wcfopen(fd, "/dev/audio", "wb");
        if (!fd )
        {
            sciprint(_("Could not open /dev/audio!\n"));
            *flag = -3;
            return;
        }
        z[2] = (double)(long)fd;
        z[1] = 1.0;
    }
    else if (*flag == 5)
    {
        if (z[2] == 0)
        {
            return;
        }
        k    = (int) z[1];
        if (k > 1) /* flush rest of buffer */
        {
            mput2(fd, ipar[6], buffer, (k - 1) * (*nin), "uc", &ierr);
            if (ierr != 0)
            {
                *flag = -3;
                return;
            }
        }
        fclose(fd);
        z[2] = 0.0;
    }
    return;
}
Exemple #5
0
/*--------------------------------------------------------------------------*/
SCICOS_BLOCKS_IMPEXP void readau(int *flag, int *nevprt,
                                 double *t, double xd[],
                                 double x[], int *nx,
                                 double z[], int *nz,
                                 double tvec[], int *ntvec,
                                 double rpar[], int *nrpar,
                                 int ipar[], int *nipar,
                                 double *inptr[], int insz[],
                                 int *nin, double *outptr[],
                                 int outsz[], int *nout)
/*
ipar[1]   = lfil : file name length
ipar[2:4] = fmt  : numbers type ascii code
ipar[5]   = void
ipar[6]   = n : buffer length in number of records
ipar[7]   = maxvoie : record size
ipar[8]   = swap
ipar[9]   = first : first record to read
ipar[10:9+lfil] = character codes for file name
*/
{
    char str[100], type[4];
    int job = 1, three = 3;
    FILE *fd = NULL;
    int n = 0, k = 0, kmax = 0, m = 0, i = 0, irep = 0, ierr = 0;
    double *buffer = NULL, *record = NULL;
    long offset ;
    double y = 0.;
    int quot = 0, rem = 0;
    double SCALE = 0.000030517578125;
    int ETAB[8];
    int mu = 0;
    int sig = 0;
    int e = 0;
    int f = 0;

    ETAB[0] = 0;
    ETAB[1] = 132;
    ETAB[2] = 396;
    ETAB[3] = 924;
    ETAB[4] = 1980;
    ETAB[5] = 4092;
    ETAB[6] = 8316;
    ETAB[7] = 16764;

    --ipar;
    --z;
    fd = (FILE *)(long)z[3];
    buffer = (z + 4);

    /*
    k    : record counter within the buffer
    kmax :  number of records in the buffer
    */

    if (*flag == 1)
    {
        n    = ipar[6];
        k    = (int)z[1];
        /* copy current record to output */
        record = buffer + (k - 1) * ipar[7];

        for (i = 0; i < *nout; i++)
        {
            mu = (int) record[i];

            mu = 255 - mu;
            if (mu > 127)
            {
                sig = 1;
            }
            else
            {
                sig = 0;
            }
            /* comment out for SUNOS SS 8/10/99
            divt=div(mu,16);
            e=divt.quot-8*sig+1;
            f=divt.rem;
            */
            quot = mu / 16;
            rem = mu - 16 * quot;
            e = quot - 8 * sig + 1;
            f = rem;

            y = ldexp((double)(f), (e + 2));
            /* ff=(double)(e+2);
            y=((double) f) * pow(two, ff); */

            e = ETAB[e - 1];

            y = SCALE * (1 - 2 * sig) * (e + y);

            *outptr[i] = y;
        }
        if (*nevprt > 0)
        {
            /*     discrete state */
            kmax = (int) z[2];
            if (k >= kmax && kmax == n)
            {
                /*     read a new buffer */
                m = ipar[6] * ipar[7];
                for (i = 0; i < three; ++i)
                {
                    type[i] = (char) ipar[i + 2];
                }
                for (i = 2; i >= 0; i--)
                    if (type[i] != ' ')
                    {
                        type[i + 1] = '\0';
                        break;
                    }
                ierr = 0;
                mget2(fd, ipar[8], buffer, m, type, &ierr);
                if (ierr > 0)
                {
                    scicos_print(_("Read error!\n"));
                    fclose(fd);
                    z[3] = 0.0;
                    *flag = -1;
                    return;
                }
                else if (ierr < 0)
                {
                    /* EOF reached */
                    kmax = -(ierr + 1) / ipar[7];
                }
                else
                {
                    kmax = ipar[6];
                }

                z[1] = 1.0;
                z[2] = kmax;
            }
            else if (k < kmax)
            {
                z[1] = z[1] + 1.0;
            }
        }
    }
    else if (*flag == 4)
    {
        for (i = 0; i < ipar[1]; ++i)
        {
            str[i] = (char) ipar[i + 10];
        }
        str[ipar[1]] = '\0';
        wcfopen(fd, str, "rb");
        if (!fd )
        {
            scicos_print(_("Could not open the file!\n"));
            *flag = -1;
            return;
        }
        z[3] = (long)fd;
        /* skip first records */
        if (ipar[9] > 1)
        {
            for (i = 0; i < three; ++i)
            {
                type[i] = (char) ipar[i + 2];
            }
            for (i = 2; i >= 0; i--)
                if (type[i] != ' ')
                {
                    type[i + 1] = '\0';
                    break;
                }
            offset = (ipar[9] - 1) * ipar[7] * sizeof(char);
            irep = fseek(fd, offset, 0) ;
            if ( irep != 0 )
            {
                scicos_print(_("Read error\n"));
                *flag = -1;
                fclose(fd);
                z[3] = 0.0;
                return;
            }
        }
        /* read first buffer */
        m = ipar[6] * ipar[7];
        for (i = 0; i < three; ++i)
        {
            type[i] = (char) ipar[i + 2];
        }
        for (i = 2; i >= 0; i--)
            if (type[i] != ' ')
            {
                type[i + 1] = '\0';
                break;
            }
        mget2(fd, ipar[8], buffer, m, type, &ierr);
        if (ierr > 0)
        {
            scicos_print(_("Read error!\n"));
            *flag = -1;
            fclose(fd);
            z[3] = 0.0;
            return;
        }
        else if (ierr < 0)
        {
            /* EOF reached */
            kmax = -(ierr + 1) / ipar[7];
        }
        else
        {
            kmax = ipar[6];
        }

        z[1] = 1.0;
        z[2] = kmax;
    }
    else if (*flag == 5)
    {
        if (z[3] == 0)
        {
            return;
        }
        fclose(fd);
        z[3] = 0.0;
    }
    return;
}
Exemple #6
0
/*--------------------------------------------------------------------------*/
SCICOS_BLOCKS_IMPEXP void readc(int *flag, int *nevprt,
                                double *t, double xd[],
                                double x[], int *nx,
                                double z[], int *nz,
                                double tvec[], int *ntvec,
                                double rpar[], int *nrpar,
                                int ipar[], int *nipar,
                                double *inptr[], int insz[],
                                int *nin, double *outptr[],
                                int outsz[], int *nout)
/*
ipar[1]   = lfil : file name length
ipar[2:4] = fmt  : numbers type ascii code
ipar[5]   = is there a time record
ipar[6]   = n : buffer length in number of records
ipar[7]   = maxvoie : record size
ipar[8]   = swap
ipar[9]   = first : first record to read
ipar[10:9+lfil] = character codes for file name
ipar[10+lfil:9+lfil++ny+ievt] = reading mask
*/
{
    char str[100], type[4];
    int job = 1, three = 3;
    FILE *fd = NULL;
    int n = 0, k = 0, ievt = 0, kmax = 0, m = 0, i = 0, irep = 0, ierr = 0;
    double *buffer = NULL, *record = NULL;
    int *mask = NULL;
    long offset ;


    --ipar;
    --z;
    fd = (FILE *)(long)z[3];
    buffer = (z + 4);
    mask = ipar + 11 + ipar[1] - ipar[5];

    /*
    k    : record counter within the buffer
    kmax :  number of records in the buffer
    */

    if (*flag == 1)
    {
        n    = ipar[6];
        ievt = ipar[5];
        k    = (int)z[1];
        /* copy current record to output */
        record = buffer + (k - 1) * ipar[7] - 1;

        for (i = 0; i < outsz[0]; i++)
        {
            *(outptr[0] + i) = record[mask[ievt + i]];
        }

        if (*nevprt > 0)
        {
            /*     discrete state */
            kmax = (int)z[2];
            if (k >= kmax && kmax == n)
            {
                /*     read a new buffer */
                m = ipar[6] * ipar[7];
                for (i = 0; i < three; ++i)
                {
                    type[i] = (char) ipar[i + 2];
                }
                for (i = 2; i >= 0; i--)
                    if (type[i] != ' ')
                    {
                        type[i + 1] = '\0';
                        break;
                    }
                ierr = 0;
                mget2(fd, ipar[8], buffer, m, type, &ierr);
                if (ierr > 0)
                {
                    scicos_print(_("Read error!\n"));
                    fclose(fd);
                    z[3] = 0.0;
                    *flag = -1;
                    return;
                }
                else if (ierr < 0)
                {
                    /* EOF reached */
                    kmax = -(ierr + 1) / ipar[7];
                }
                else
                {
                    kmax = ipar[6];
                }

                z[1] = 1.0;
                z[2] = kmax;
            }
            else if (k < kmax)
            {
                z[1] = z[1] + 1.0;
            }
        }
    }
    else if (*flag == 3)
    {
        ievt = ipar[5];
        n    = ipar[6];
        k    = (int)z[1];
        kmax = (int) z[2];
        if (k > kmax && kmax < n)
        {
            if (ievt)
            {
                tvec[0] = *t - 1.0;
            }
            else
            {
                tvec[0] = *t * (1.0 + 0.0000000001);
            }
        }
        else
        {
            record = buffer + (k - 1) * ipar[7] - 1;
            if (ievt)
            {
                tvec[0] = record[mask[0]];
            }
        }
    }
    else if (*flag == 4)
    {
        for (i = 0; i < ipar[1]; ++i)
        {
            str[i] = (char) ipar[i + 10];
        }
        str[ipar[1]] = '\0';
        wcfopen(fd, str, "rb");
        if (!fd )
        {
            scicos_print(_("Could not open the file!\n"));
            *flag = -1;
            return;
        }
        z[3] = (long)fd;
        /* skip first records */
        if (ipar[9] > 1)
        {
            for (i = 0; i < three; ++i)
            {
                type[i] = (char) ipar[i + 2];
            }
            for (i = 2; i >= 0; i--)
                if (type[i] != ' ')
                {
                    type[i + 1] = '\0';
                    break;
                }
            offset = (ipar[9] - 1) * ipar[7] * worldsize(type);
            irep = fseek(fd, offset, 0) ;
            if ( irep != 0 )
            {
                scicos_print(_("Read error\n"));
                *flag = -1;
                fclose(fd);
                z[3] = 0.0;
                return;
            }
        }
        /* read first buffer */
        m = ipar[6] * ipar[7];
        for (i = 0; i < three; ++i)
        {
            type[i] = (char) ipar[i + 2];
        }
        for (i = 2; i >= 0; i--)
            if (type[i] != ' ')
            {
                type[i + 1] = '\0';
                break;
            }
        mget2(fd, ipar[8], buffer, m, type, &ierr);
        if (ierr > 0)
        {
            scicos_print(_("Read error!\n"));
            *flag = -1;
            fclose(fd);
            z[3] = 0.0;
            return;
        }
        else if (ierr < 0)
        {
            /* EOF reached */
            kmax = -(ierr + 1) / ipar[7];
        }
        else
        {
            kmax = ipar[6];
        }

        z[1] = 1.0;
        z[2] = kmax;
    }
    else if (*flag == 5)
    {
        if (z[3] == 0)
        {
            return;
        }
        fclose(fd);
        z[3] = 0.0;
    }
    return;
}
Exemple #7
0
/* ==================================================================== */
char *downloadFile(char *url, char *dest, char *username, char *password, char **content)
{
    CURL *curl;
    CURLcode res;
    char *filename = NULL;
    FILE *file;
    inputString buffer;
    char *destdir = NULL;
    char *destfile = NULL;

    curl = curl_easy_init();
    if (curl == NULL)
    {
        Scierror(999, "Failed opening the curl handle.\n");
        return NULL;
    }

    res = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
    if (res != CURLE_OK)
    {
        Scierror(999, "Failed to set error buffer [%d]\n", res);
        return NULL;
    }

    // Get destination directory and filename
    if (dest != NULL)
    {
        // Destination is specified in argument
        char* pathdrive = (char*)MALLOC(sizeof(char) * (PATH_MAX + 1));
        char* pathdir = (char*)MALLOC(sizeof(char) * (PATH_MAX + 1));
        char* pathfile = (char*)MALLOC(sizeof(char) * (PATH_MAX + 1));
        char* pathext = (char*)MALLOC(sizeof(char) * (PATH_MAX + 1));

        splitpath(dest, TRUE, pathdrive, pathdir, pathfile, pathext);

        if (!isdir(dest))
        {
            // Destination is a file
            destdir = (char *)MALLOC((strlen(pathdrive) + strlen(pathdir) + 1) * sizeof(char));
            strcpy(destdir, pathdrive);
            strcat(destdir, pathdir);

            // Get filename
            destfile = (char *)MALLOC((strlen(pathfile) + strlen(pathext) + 1) * sizeof(char));
            strcpy(destfile, pathfile);
            strcat(destfile, pathext);
        }
        else
        {
            // Destination is a directory
            destdir = (char *)MALLOC((strlen(pathdrive) + strlen(pathdir) + strlen(pathfile) + strlen(pathext) + strlen(DIR_SEPARATOR) + 1) * sizeof(char));
            strcpy(destdir, pathdrive);
            strcat(destdir, pathdir);
            strcat(destdir, pathfile);
            strcat(destdir, pathext);
            strcat(destdir, DIR_SEPARATOR);

            // Retrieve filename from URL
            destfile = getFileNameFromURL(url);
        }

        FREE(pathdrive);
        FREE(pathdir);
        FREE(pathfile);
        FREE(pathext);
    }
    else
    {
        // Destination is not specified in argument
        // Destination directory is current dir
        int err = 0;
        char *currentdir;
        currentdir = scigetcwd(&err);
        if (!err)
        {
            destdir = (char *)MALLOC((strlen(currentdir) + strlen(DIR_SEPARATOR) + 1) * sizeof(char));
            strcpy(destdir, currentdir);
            strcat(destdir, DIR_SEPARATOR);
            FREE(currentdir);
        }
        else
        {
            Scierror(999, _("Failed getting current dir, error code: %d\n"), err);
            return NULL;
        }

        // Destination filename retrieved from URL
        destfile = getFileNameFromURL(url);
    }

    if (destfile == NULL)
    {
        FREE(destdir);
        return NULL;
    }

    // Build file path
    filename = (char *)MALLOC((strlen(destdir) + strlen(destfile) + 1) * sizeof(char));
    strcpy(filename, destdir);
    strcat(filename, destfile);
    FREE(destdir);
    FREE(destfile);

    res = curl_easy_setopt(curl, CURLOPT_URL, url);
    if (res != CURLE_OK)
    {
        Scierror(999, _("Failed to set URL [%s]\n"), errorBuffer);
        FREE(filename);
        return NULL;
    }

    //Set authentication variables
    if (username != NULL)
    {
        char * userpass;
        int uplen = (int)strlen(username);
        if (password != NULL)
        {
            uplen = uplen + (int)strlen(password);
        }

        res = curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        if (res != CURLE_OK)
        {
            Scierror(999, "Failed to set httpauth type to ANY [%s]\n", errorBuffer);
            return NULL;
        }

        userpass = (char *)MALLOC((uplen + 2) * sizeof(char));
        strcpy(userpass, username);
        strcat(userpass, ":");
        if (password != NULL)
        {
            strcat(userpass, password);
        }

        res = curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
        if (res != CURLE_OK)
        {
            FREE(filename);
            Scierror(999, _("Failed to set user:pwd [%s]\n"), errorBuffer);
            return NULL;
        }

        FREE(userpass);
    } /* end authentication section */

    {
        //Set proxy variables
        char *proxyHost = NULL;
        char *proxyUserPwd = NULL;
        long proxyPort = 1080;
        int proxySet = 0;

        proxySet = getProxyValues(&proxyHost, &proxyPort, &proxyUserPwd);

        if (proxySet == 1)
        {
            res = curl_easy_setopt(curl, CURLOPT_PROXY, proxyHost);
            if (res != CURLE_OK)
            {
                FREE(proxyHost);
                FREE(proxyUserPwd);
                FREE(filename);
                Scierror(999, _("Failed to set proxy host [%s]\n"), errorBuffer);
                return NULL;
            }

            res = curl_easy_setopt(curl, CURLOPT_PROXYPORT, proxyPort);
            if (res != CURLE_OK)
            {
                FREE(proxyHost);
                FREE(proxyUserPwd);
                FREE(filename);
                Scierror(999, _("Failed to set proxy port [%s]\n"), errorBuffer);
                return NULL;
            }
            if (proxyUserPwd != NULL)
            {
                res = curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxyUserPwd);
                if (res != CURLE_OK)
                {
                    FREE(proxyHost);
                    FREE(proxyUserPwd);
                    FREE(filename);
                    Scierror(999, _("Failed to set proxy user:password [%s]\n"), errorBuffer);
                    return NULL;
                }
            }

            FREE(proxyHost);
            FREE(proxyUserPwd);
        }
    } /* end of the set of the proxy */

    res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
    if (res != CURLE_OK)
    {
        FREE(filename);
        Scierror(999, _("Failed to set write function [%s]\n"), errorBuffer);
        return NULL;
    }

    init_string(&buffer);

    //Get data to be written to the variable
    res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
    if (res != CURLE_OK)
    {
        FREE(filename);
        free_string(&buffer);
        Scierror(999, _("Failed to set write data [%s]\n"), errorBuffer);
        return NULL;
    }

    // Follow redirects
    res = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
    if (res != CURLE_OK)
    {
        FREE(filename);
        free_string(&buffer);
        Scierror(999, _("Failed to set 'Follow Location' [%s]\n"), errorBuffer);
        return NULL;
    }

    res = curl_easy_perform(curl);
    if (res != 0)
    {
        FREE(filename);
        free_string(&buffer);
        Scierror(999, _("Transfer did not complete successfully: %s\n"), errorBuffer);
        return NULL;
    }

    wcfopen(file, (char*)filename, "wb");
    if (file == NULL)
    {
        free_string(&buffer);
        Scierror(999, _("Failed opening '%s' for writing.\n"), filename);
        FREE(filename);
        return NULL;
    }

    /* Write the file */
    fwrite(buffer.ptr, sizeof(char), buffer.len, file);

    /* Create the variable which contains the output argument */
    *content = buffer.ptr;

    /* always cleanup */
    curl_easy_cleanup(curl);

    fclose(file);
    return filename;
}
Exemple #8
0
/* ==================================================================== */
char *downloadFile(char *url, char *dest, char *username, char *password, char **content)
{
    CURL *curl;
    CURLcode res;
    char *filename = NULL;

    curl = curl_easy_init();

    if (curl)
    {
        FILE *file;

        struct inputString buffer;

        init_string(&buffer);

        res = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
        if (res != CURLE_OK)
        {
            Scierror(999, "Failed to set error buffer [%d]\n", res);
            return NULL;
        }

        if (dest == NULL)
        {
            /* No second argument provided */
            filename = getFileNameFromURL(url);
        }
        else
        {
            if (isdir(dest))
            {
                /* The target is a directory. Select the name from the URL */
                char *name = getFileNameFromURL(url);

                filename = (char *)MALLOC((strlen(name) + strlen("/") + strlen(dest) + 1) * sizeof(char));
                strcpy(filename, dest);
                strcat(filename, "/");
                strcat(filename, name);

            }
            else
            {
                filename = (char *)MALLOC((strlen(dest) + 1) * sizeof(char));
                strcpy(filename, dest);
            }
        }

        wcfopen(file, (char*)filename, "wb");

        if (file == NULL)
        {
            Scierror(999, _("Failed opening '%s' for writing.\n"), filename);
            return NULL;
        }

        res = curl_easy_setopt(curl, CURLOPT_URL, url);
        if (res != CURLE_OK)
        {
            Scierror(999, _("Failed to set URL [%s]\n"), errorBuffer);
            return NULL;
        }

        //Set authentication variables
        if (username != NULL)
        {
            char * userpass;
            int uplen = (int)strlen(username);
            if (password != NULL)
            {
                uplen = uplen + (int)strlen(password);
            }

            userpass = (char *)MALLOC((uplen + 2) * sizeof(char));

            strcpy(userpass, username);
            strcat(userpass, ":");
            if (password != NULL)
            {
                strcat(userpass, password);
            }

            res = curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
            if (res != CURLE_OK)
            {
                Scierror(999, "Failed to set httpauth type to ANY [%s]\n", errorBuffer);
                return NULL;
            }
            res = curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
            if (res != CURLE_OK)
            {
                Scierror(999, _("Failed to set user:pwd [%s]\n"), errorBuffer);
                return NULL;
            }
        } /* end authentication section */

        {
            //Set proxy variables
            char *proxyHost = NULL;
            char *proxyUserPwd = NULL;
            long proxyPort = 1080;
            int proxySet = 0;

            proxySet = getProxyValues(&proxyHost, &proxyPort, &proxyUserPwd);

            if (proxySet == 1)
            {
                res = curl_easy_setopt(curl, CURLOPT_PROXY, proxyHost);
                if (res != CURLE_OK)
                {
                    Scierror(999, _("Failed to set proxy host [%s]\n"), errorBuffer);
                    return NULL;
                }
                curl_easy_setopt(curl, CURLOPT_PROXYPORT, proxyPort);
                if (res != CURLE_OK)
                {
                    Scierror(999, _("Failed to set proxy port [%s]\n"), errorBuffer);
                    return NULL;
                }
                if (proxyUserPwd != NULL)
                {
                    res = curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, proxyUserPwd);
                    if (res != CURLE_OK)
                    {
                        Scierror(999, _("Failed to set proxy user:password [%s]\n"), errorBuffer);
                        return NULL;
                    }
                }

            }
        } /* end of the set of the proxy */

        res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);

        if (res != CURLE_OK)
        {
            Scierror(999, _("Failed to set write data [%s]\n"), errorBuffer);
            return NULL;
        }

        //Get data to be written to the variable
        res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
        if (res != CURLE_OK)
        {
            Scierror(999, _("Failed to set write data [%s]\n"), errorBuffer);
            return NULL;
        }

        // Follow redirects
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);

        res = curl_easy_perform(curl);

        if (res != 0)
        {
            Scierror(999, _("Transfer did not complete successfully: %s\n"), errorBuffer);
            return NULL;
        }

        /* Write the file */
        fwrite(buffer.ptr, sizeof(char), buffer.len, file);

        /* Create the variable which contains the output argument */
        *content = buffer.ptr;

        /* always cleanup */
        curl_easy_cleanup(curl);

        fclose(file);

        return filename;
    }
    else
    {
        Scierror(999, "Failed opening the curl handle.\n");
        return NULL;
    }
    return NULL;
}
Exemple #9
0
/* ==================================================================== */
int getProxyValues(char **proxyHost, long *proxyPort, char **proxyUserPwd)
{
    FILE * pFile;
    long lSize;
    char * buffer;
    size_t result;

    char *configPtr;
    char *osName;

    char *host, *user, *password, *userpwd;
    long port;
    int useproxy;

    char *tp, *field, *value, *eqptr;
    int eqpos, tplen;

    //construct ATOMS config file path
    configPtr = (char *)MALLOC(PATH_MAX * sizeof(char));
    strcpy(configPtr, getSCIHOME());

    osName = (char *)MALLOC(50 * sizeof(char));
    strcpy(osName, getOSFullName());
    if (strcmp(osName, "Windows") == 0)
    {
        char *osVer = (char *)MALLOC(50 * sizeof(char));
        strcpy(osVer, getOSRelease());
        if (strstr(osVer, "x64") != NULL)
        {
            strcat(configPtr, "/.atoms/x64/config");
        }
        else
        {
            strcat(configPtr, "/.atoms/config");
        }
    }
    else
    {
        strcat(configPtr, "/.atoms/config");
    }


    wcfopen (pFile, configPtr , "rb" );
    if (pFile == NULL)
    {
        //		Scierror(999,"Could not open scicurl_config file\n");
        return 0;
    }

    fseek (pFile , 0 , SEEK_END);
    lSize = ftell(pFile);
    rewind (pFile);

    // allocate memory to contain the whole file
    buffer = (char*)MALLOC((lSize + 1) * sizeof(char));
    if (buffer == NULL)
    {
        return 0;
    }
    buffer[lSize] = '\0';

    // copy the file into the buffer
    result = fread (buffer, 1, lSize, pFile);
    if (result != lSize)
    {
        Scierror(999, _("Failed to read the scicurl_config file '%s'.\n"), configPtr);
        return 0;
    }

    host = user = password = userpwd = NULL;
    useproxy = 0;

    tp = field = value = eqptr = NULL;
    eqpos = tplen = 0;


    // parse each line to extract variables
    tp = strtok(buffer, "\n");
    while (tp != NULL)
    {

        eqptr = strrchr(tp, '=');
        tplen = (int)strlen(tp);
        if (eqptr == NULL)
        {
            Scierror(999, _("Improper syntax of scicurl_config file ('%s'), '=' not found %d:%s\n"), configPtr, tplen, tp);
            return 0;
        }
        eqpos = (int)(eqptr - tp);
        if (tplen <= eqpos + 1)
        {
            Scierror(999, _("Improper syntax of scicurl_config file ('%s'), after an '='\n"), configPtr);
            return 0;
        }
        if (tp[eqpos - 1] != ' ' || tp[eqpos + 1] != ' ')
        {
            Scierror(999, _("Improper syntax of scicurl_config file ('%s'), space before and after '=' expected\n"), configPtr);
            return 0;
        }

        //get field and value from each line
        field = (char *)MALLOC(sizeof(char) * (eqpos));
        value = (char *)MALLOC(sizeof(char) * (strlen(tp) - eqpos - 1));

        memcpy(field, tp, eqpos - 1);
        field[eqpos - 1] = '\0';

        memcpy(value, tp + eqpos + 2, strlen(tp) - eqpos - 2);
        value[strlen(tp) - eqpos - 2] = '\0';


        //check and read proxy variables
        if (strcmp(field, "useProxy") == 0)
        {
            if (strcmp(value, "False") == 0)
            {
                return 0;
            }
            if (strcmp(value, "True") == 0)
            {
                useproxy = 1;
            }
        }
        else if (strcmp(field, "proxyHost") == 0)
        {
            host = (char *)MALLOC((strlen(value) + 1) * sizeof(char));
            strcpy(host, value);
        }
        else if (strcmp(field, "proxyPort") == 0)
        {
            port = strtol(value, NULL, 10);
        }
        else if (strcmp(field, "proxyUser") == 0)
        {
            user = (char *)MALLOC((strlen(value) + 1) * sizeof(char));
            strcpy(user, value);
        }
        else if (strcmp(field, "proxyPassword") == 0)
        {
            password = (char *)MALLOC((strlen(value) + 1) * sizeof(char));
            strcpy(password, value);
        }

        free(field);
        free(value);

        tp = strtok(NULL, "\n");
    }

    // if proxy is set, update the parameters
    if (useproxy == 1)
    {

        // proxyUserPwd = "user:password"
        int userlen, passlen;
        userlen = passlen = 0;
        if (user != NULL)
        {
            userlen = (int)strlen(user);
        }
        if (password != NULL)
        {
            passlen = (int)strlen(user);
        }
        if (userlen + passlen != 0)
        {
            userpwd = (char *)MALLOC((userlen + passlen + 2) * sizeof(char));
            strcpy(userpwd, user);
            strcat(userpwd, ":");
            if (password != NULL)
            {
                strcat(userpwd, password);
            }
        }

        *proxyHost = host;
        *proxyPort = port;
        *proxyUserPwd = userpwd;

    }

    fclose(pFile);
    free(buffer);
    return useproxy;
}
Exemple #10
0
/*--------------------------------------------------------------------------*/
int C2F(sci_getmd5) (char *fname, unsigned long fname_len)
{
    int m1 = 0, n1 = 0;

    int mn = 0;
    int i  = 0;

    char **Input_Matrix  = NULL;
    char **Output_Matrix = NULL;

    Rhs = Max(Rhs, 0);
    CheckRhs(1, 2) ;
    CheckLhs(1, 1) ;

    if (Rhs == 1)
    {
        if (GetType(1) == sci_strings)
        {
            GetRhsVar(1, MATRIX_OF_STRING_DATATYPE, &m1, &n1, &Input_Matrix);
            mn = m1 * n1;

            Output_Matrix = (char**)MALLOC(sizeof(char*) * (mn));
            if (Output_Matrix)
            {
                for (i = 0; i < mn; i++)
                {
                    FILE *fp = NULL;
                    char *MD5 = NULL;
                    char *real_path = NULL;

                    /* Replaces SCI, ~, HOME, TMPDIR by the real path */
                    real_path = expandPathVariable(Input_Matrix[i]);

                    /* bug 4469 */
                    if (isdir(real_path))
                    {
                        Scierror(999, _("%s: The file %s does not exist.\n"), fname, Input_Matrix[i]);
                        freeArrayOfString(Output_Matrix, i);
                        freeArrayOfString(Input_Matrix, mn);
                        FREE(real_path);
                        real_path = NULL;
                        return 0;
                    }

                    wcfopen(fp, real_path, "rb");

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

                    if (fp)
                    {
                        MD5 = md5_file(fp);
                        fclose(fp);
                        Output_Matrix[i] = strdup(MD5);
                        if (MD5)
                        {
                            FREE(MD5);
                            MD5 = NULL;
                        }
                    }
                    else
                    {
                        Scierror(999, _("%s: The file %s does not exist.\n"), fname, Input_Matrix[i]);
                        freeArrayOfString(Output_Matrix, i);
                        freeArrayOfString(Input_Matrix, mn);
                        return 0;
                    }
                }

                CreateVarFromPtr( Rhs + 1, MATRIX_OF_STRING_DATATYPE, &m1, &n1, Output_Matrix );
                LhsVar(1) = Rhs + 1 ;
                PutLhsVar();
            }
            else
            {
                Scierror(999, _("%s: Memory allocation error.\n"), fname);
            }
        }
        else
        {
            Scierror(999, _("%s: Wrong type of input argument #%d: String expected.\n"), fname, 1);
        }
    }
    else /* Rhs == 2 */
    {
        if ( (GetType(1) == sci_strings) && (GetType(2) == sci_strings) )
        {
            int m2 = 0, n2 = 0, l2 = 0;
            char *Param2 = NULL;

            GetRhsVar(1, MATRIX_OF_STRING_DATATYPE, &m1, &n1, &Input_Matrix);
            mn = m1 * n1;

            GetRhsVar(2, STRING_DATATYPE, &m2, &n2, &l2);
            Param2 = cstk(l2);

            if ( stricmp(Param2, "string") == 0 )
            {
                Output_Matrix = (char**)MALLOC(sizeof(char*) * (mn));

                if (Output_Matrix)
                {
                    for (i = 0; i < mn; i++)
                    {
                        char *MD5 = NULL;

                        MD5 = md5_str(Input_Matrix[i]);
                        Output_Matrix[i] = strdup(MD5);
                        if (MD5)
                        {
                            FREE(MD5);
                            MD5 = NULL;
                        }

                        if (Output_Matrix[i] == NULL)
                        {
                            freeArrayOfString(Input_Matrix, m1 * n1);
                            freeArrayOfString(Output_Matrix, i);
                            Scierror(999, ("%s: No more memory.\n"), fname);
                            return 0;
                        }
                    }

                    CreateVarFromPtr(Rhs + 1, MATRIX_OF_STRING_DATATYPE, &m1, &n1, Output_Matrix );
                    LhsVar(1) = Rhs + 1 ;
                    PutLhsVar();
                }
                else
                {
                    Scierror(999, _("%s: Memory allocation error.\n"), fname);
                }
            }
            else
            {
                Scierror(999, _("%s: Wrong value for input argument #%d: \"%s\" expected.\n"), fname, 2, "string");
            }
        }
        else
        {
            Scierror(999, _("%s: Wrong type for input arguments #%d or #%d: Strings expected.\n"), fname, 1, 2);
        }
    }

    freeArrayOfString(Input_Matrix, mn);
    freeArrayOfString(Output_Matrix, mn);

    return 0;
}