Hush Full Node software. We were censored from Github, this is where all development happens now.
https://hush.is
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
3.9 KiB
133 lines
3.9 KiB
6 years ago
|
/******************************************************************************
|
||
|
* Copyright © 2014-2018 The SuperNET Developers. *
|
||
|
* *
|
||
|
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
|
||
|
* the top-level directory of this distribution for the individual copyright *
|
||
|
* holder information and the developer policies on copyright and licensing. *
|
||
|
* *
|
||
|
* Unless otherwise agreed in a custom licensing agreement, no part of the *
|
||
|
* SuperNET software, including this file may be copied, modified, propagated *
|
||
|
* or distributed except according to the terms contained in the LICENSE file *
|
||
|
* *
|
||
|
* Removal or modification of this copyright notice is prohibited. *
|
||
|
* *
|
||
|
******************************************************************************/
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <unistd.h>
|
||
6 years ago
|
#include "cJSON.c"
|
||
6 years ago
|
|
||
6 years ago
|
char *OS_nonportable_path(char *str)
|
||
|
{
|
||
|
int32_t i;
|
||
|
for (i=0; str[i]!=0; i++)
|
||
|
if ( str[i] == '/' )
|
||
|
str[i] = '\\';
|
||
|
return(str);
|
||
|
}
|
||
|
|
||
|
char *OS_portable_path(char *str)
|
||
|
{
|
||
|
#ifdef _WIN32
|
||
|
return(OS_nonportable_path(str));
|
||
|
#else
|
||
|
#ifdef __PNACL
|
||
|
/*int32_t i,n;
|
||
|
if ( str[0] == '/' )
|
||
|
return(str);
|
||
|
else
|
||
|
{
|
||
|
n = (int32_t)strlen(str);
|
||
|
for (i=n; i>0; i--)
|
||
|
str[i] = str[i-1];
|
||
|
str[0] = '/';
|
||
|
str[n+1] = 0;
|
||
|
}*/
|
||
|
#endif
|
||
|
return(str);
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
void *OS_loadfile(char *fname,uint8_t **bufp,long *lenp,long *allocsizep)
|
||
|
{
|
||
|
FILE *fp;
|
||
|
long filesize,buflen = *allocsizep;
|
||
|
uint8_t *buf = *bufp;
|
||
|
*lenp = 0;
|
||
|
if ( (fp= fopen(OS_compatible_path(fname),"rb")) != 0 )
|
||
|
{
|
||
|
fseek(fp,0,SEEK_END);
|
||
|
filesize = ftell(fp);
|
||
|
if ( filesize == 0 )
|
||
|
{
|
||
|
fclose(fp);
|
||
|
*lenp = 0;
|
||
|
printf("OS_loadfile null size.(%s)\n",fname);
|
||
|
return(0);
|
||
|
}
|
||
|
if ( filesize > buflen )
|
||
|
{
|
||
|
*allocsizep = filesize;
|
||
|
*bufp = buf = (uint8_t *)realloc(buf,(long)*allocsizep+64);
|
||
|
}
|
||
|
rewind(fp);
|
||
|
if ( buf == 0 )
|
||
|
printf("Null buf ???\n");
|
||
|
else
|
||
|
{
|
||
|
if ( fread(buf,1,(long)filesize,fp) != (unsigned long)filesize )
|
||
|
printf("error reading filesize.%ld\n",(long)filesize);
|
||
|
buf[filesize] = 0;
|
||
|
}
|
||
|
fclose(fp);
|
||
|
*lenp = filesize;
|
||
|
//printf("loaded.(%s)\n",buf);
|
||
|
} //else printf("OS_loadfile couldnt load.(%s)\n",fname);
|
||
|
return(buf);
|
||
|
}
|
||
|
|
||
|
void *OS_filestr(long *allocsizep,char *_fname)
|
||
|
{
|
||
|
long filesize = 0; char *fname,*buf = 0; void *retptr;
|
||
|
*allocsizep = 0;
|
||
|
fname = malloc(strlen(_fname)+1);
|
||
|
strcpy(fname,_fname);
|
||
|
retptr = OS_loadfile(fname,&buf,&filesize,allocsizep);
|
||
|
free(fname);
|
||
|
return(retptr);
|
||
|
}
|
||
|
|
||
|
char *issue_curl(char *url)
|
||
|
{
|
||
|
long fsize; char curlstr[1024],*fname = "/tmp/oraclefeed.json"
|
||
|
sprintf(curlstr,"curl --url \"%s\" > %s",url);
|
||
|
system(curlstr);
|
||
|
return(OS_filestr(&fsize,fname));
|
||
|
}
|
||
6 years ago
|
|
||
|
cJSON *url_json(char *url)
|
||
|
{
|
||
6 years ago
|
cJSON *json = 0;
|
||
6 years ago
|
if ( (jsonstr= issue_curl(url)) != 0 )
|
||
|
{
|
||
|
printf("(%s) -> (%s)\n",url,jsonstr);
|
||
|
json = cJSON_Parse(jsonstr);
|
||
|
free(jsonstr);
|
||
|
}
|
||
|
return(json);
|
||
|
}
|
||
|
|
||
|
int32_t main(int32_t argc,char **argv)
|
||
|
{
|
||
|
cJSON *pjson,*bpi,*usd;
|
||
|
printf("Powered by CoinDesk (%s)\n","https://www.coindesk.com/price/");
|
||
|
if ( (pjson= url_json("http://api.coindesk.com/v1/bpi/currentprice.json","")) != 0 )
|
||
|
{
|
||
|
if ( (bpi= jobj(pjson,"bpi")) != 0 && (usd= jobj(bpi,"USD")) != 0 )
|
||
|
printf("BTC/USD %.4f\n",jdouble(usd,"rate_float"));
|
||
|
json_close(pjson);
|
||
|
}
|
||
|
return(0);
|
||
|
}
|