Random File and URL Opener
Posted: Mon Apr 04, 2016 1:11 am
filesopen Minimum_minutes_random_delay Maximum_minutes_random_delay Database_name Database_configuration_row_number
Here I have a console program I wrote that randomly and automatically opens local files or URLs taken from an SQLite3 database which it handles by itself using the SQLite3 Amalgamation library.
It's fully written in C and works under Windows so far (maybe it can work under Wine too).
Here is the compiled binary:
FILESOPEN.EXE
Here you can see and learn how I wrote the C code to handle the SQLite3 code from C:
>> Text Recording for the Random File Opener <<
The documented structure and usage of the databases is contained in the text recording.
You can test the program with the following database which contains more than 2 million domain names from the daily top 1 million from Alexa:
domains_alexaranks.db
To run the program you just need the following command:
This is the main source code (compiled using MinGW GCC):
Here I have a console program I wrote that randomly and automatically opens local files or URLs taken from an SQLite3 database which it handles by itself using the SQLite3 Amalgamation library.
It's fully written in C and works under Windows so far (maybe it can work under Wine too).
Here is the compiled binary:
FILESOPEN.EXE
Here you can see and learn how I wrote the C code to handle the SQLite3 code from C:
>> Text Recording for the Random File Opener <<
The documented structure and usage of the databases is contained in the text recording.
You can test the program with the following database which contains more than 2 million domain names from the daily top 1 million from Alexa:
domains_alexaranks.db
To run the program you just need the following command:
Code: Select all
filesopen 1 1 domains_alexaranks.db 1
filesopen minimum_minutes_random_delay maximum_minutes_random_delay database_name database_configuration_row_number
This is the main source code (compiled using MinGW GCC):
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "SQLite3-Amalgamation-3.12.0/sqlite3.h"
#include "SQLite3-Amalgamation-3.12.0/sqlite3.c"
#include "SQLite3-Amalgamation-3.12.0/sqlite3ext.h"
#include <windows.h>
#include <time.h>
const char sqlite3_version[] = SQLITE_VERSION;
//Some custom function declarations:
///
unsigned long moreRandomFactor=1;
unsigned long long_getNumBits(unsigned long num);
unsigned long stringSum(char *str, unsigned int str_SZ);
unsigned long getDateMilliseconds(void);
unsigned long stringToRandomNumber(char *str, unsigned int str_SZ, unsigned long max_rand, unsigned long toRet);
//Set default values for the number of table rows and for the current column
//this last one will be selected at random:
///
int SQLite3_Row_Ctr=1;
int SQLite3_Current_Row=1;
sqlite3 *SQLite3_DB_Obj;
sqlite3_stmt *SQLite3_State;
int SQLite3_DB_Res_Handle;
char *SQL_String;
char *SQLite3_ErrorMsg=NULL;
char *SQLite3_zTail_String;
//These are the default minimum and maximum number of minutes to wait
//before trying to open other file/directory entry:
///
int minwait=1;
int maxwait=1;
long minmax_rand(long min, long max)
{
return rand()%((max+1) - min) + min;
}
char *config_tablename;
char *config_datarowname;
char *config_idrowname;
char *config_fileprotocol;
char *config_mainshell;
int config_svfskip;
char *config_slashchar;
static int SQLite3_Callback_config(void *NotUsed, int argc, char **argv, char **azColName)
{
config_tablename=malloc(512);
config_datarowname=malloc(512);
config_idrowname=malloc(512);
config_fileprotocol=malloc(512);
config_mainshell=malloc(512);
config_slashchar=malloc(512);
sprintf(config_tablename,"%s",argv[0]);
sprintf(config_datarowname,"%s",argv[1]);
sprintf(config_idrowname,"%s",argv[2]);
sprintf(config_fileprotocol,"%s",argv[3]);
sprintf(config_mainshell,"%s",argv[4]);
config_svfskip=atoi(argv[5]);
sprintf(config_slashchar,"%s",argv[6]);
printf("Getting database configuration...\n");
printf("tablename=%s\ndatarowname=%s\nidrowname=%s\nfileprotocol=%s\nmainshell=%s\nsvfskip=%d\nslashchar=%s\n\n", config_tablename, config_datarowname, config_idrowname, config_fileprotocol, config_mainshell, config_svfskip, config_slashchar);
return 0;
}
static int SQLite3_Callback(void *NotUsed, int argc, char **argv, char **azColName)
{
int x=0;
int requiredSize=0;
int openstrlen=0;
char *cmd=NULL;
char *cmd2=NULL;
char *openstr="open";
char *openstrW;
char *explorerstr=config_mainshell;
char *explorerstrW;
int execres=0;
openstrW=malloc(131072);
explorerstrW=malloc(131072);
cmd=malloc(131072);
cmd2=malloc(131072);
MultiByteToWideChar(
(UINT)CP_UTF8,
(DWORD)0,
(LPCSTR)explorerstr,
(int)-1,
(LPWSTR)explorerstrW,
(int)131072
);
MultiByteToWideChar(
(UINT)CP_UTF8,
(DWORD)0,
(LPCSTR)openstr,
(int)-1,
(LPWSTR)openstrW,
(int)131072
);
for(x=0; x<argc; x++)
{
snprintf(cmd,131072,"\"%s%s\"",config_fileprotocol,argv[x]+config_svfskip);
//Try to increase the randomness of the selected row element:
///
moreRandomFactor=stringToRandomNumber(cmd, 131072, SQLite3_Row_Ctr-1, moreRandomFactor);
/*
int MultiByteToWideChar(
_In_ UINT CodePage,
_In_ DWORD dwFlags,
_In_ LPCSTR lpMultiByteStr,
_In_ int cbMultiByte,
_Out_opt_ LPWSTR lpWideCharStr,
_In_ int cchWideChar
);
*/
MultiByteToWideChar(
(UINT)CP_UTF8,
(DWORD)0,
(LPCSTR)cmd,
(int)-1,
(LPWSTR)cmd2,
(int)131072
);
printf("%ld: %s\n\n", SQLite3_Current_Row, cmd);
/*
HINSTANCE ShellExecute(
_In_opt_ HWND hwnd,
_In_opt_ LPCTSTR lpOperation,
_In_ LPCTSTR lpFile,
_In_opt_ LPCTSTR lpParameters,
_In_opt_ LPCTSTR lpDirectory,
_In_ INT nShowCmd
);
*/
execres=(int)ShellExecuteW(
(HWND)0,
(LPCWSTR)openstrW,
(LPCWSTR)explorerstrW,
(LPCWSTR)cmd2,
(LPCWSTR)NULL,
(INT)SW_SHOWNORMAL
);
switch(execres)
{
case 0:
printf("exec: 0");
break;
case ERROR_BAD_FORMAT:
printf("exec: ERROR_BAD_FORMAT");
break;
case SE_ERR_ACCESSDENIED:
printf("exec: SE_ERR_ACCESSDENIED");
break;
case SE_ERR_ASSOCINCOMPLETE:
printf("exec: SE_ERR_ASSOCINCOMPLETE");
break;
case SE_ERR_DDEBUSY:
printf("exec: SE_ERR_DDEBUSY");
break;
case SE_ERR_DDEFAIL:
printf("exec: SE_ERR_DDEFAIL");
break;
case SE_ERR_DDETIMEOUT:
printf("exec: SE_ERR_DDETIMEOUT");
break;
case SE_ERR_DLLNOTFOUND:
printf("exec: SE_ERR_DLLNOTFOUND");
break;
case SE_ERR_FNF:
printf("exec: SE_ERR_FNF");
break;
case SE_ERR_NOASSOC:
printf("exec: SE_ERR_NOASSOC");
break;
case SE_ERR_OOM:
printf("exec: SE_ERR_OOM");
break;
case SE_ERR_SHARE:
printf("exec: SE_ERR_SHARE");
break;
}
sleep(60*(int)minmax_rand(minwait,maxwait));
}
free(cmd);
free(cmd2);
free(openstrW);
free(explorerstrW);
return 0;
}
static int SQLite3_Callback2(void *NotUsed, int argc, char **argv, char **azColName)
{
SQLite3_Row_Ctr=atol(argv[0]);
return SQLite3_Row_Ctr;
}
void clear_state()
{
//NOTE: This is for the good practice of not leaving
// freeing of resources or other default operations
// at their default state but accelerate and ensure
// the global sanity of the environment and the program
// by specifying every operation exactly as we want it:
///
sqlite3_finalize(SQLite3_State);
sqlite3_close(SQLite3_DB_Obj);
free(SQL_String);
}
int main(int argc, char *argv[])
{
atexit(clear_state);
SQL_String=malloc(4096);
if(argc>=5)
{
if(!(minwait=atoi(argv[1])))minwait=1;
if(!(maxwait=atoi(argv[2])))maxwait=1;
}
else
{
printf("Usage: filesopen min_minutes max_minutes SQLITE3_Database_Path Database_Config_ID\n\n");
return -2;
}
//Open the database and see if it was successful. If not, just exit the program:
///
SQLite3_DB_Res_Handle=sqlite3_open(argv[3], &SQLite3_DB_Obj);
if(SQLite3_DB_Res_Handle!=SQLITE_OK)
{
printf("Error opening database\n\n");
return -1;
}
//Get the configuration of the database to use the proper table and field names,
//as well as basic formatting for the data:
///
snprintf(SQL_String, 4096, "SELECT tablename,datarowname,idrowname,fileprotocol,mainshell,svfskip,slashchar FROM config WHERE configid=%s LIMIT 1", argv[4]);
SQLite3_DB_Res_Handle=sqlite3_exec(
SQLite3_DB_Obj,
SQL_String,
SQLite3_Callback_config,
SQLite3_zTail_String,
&SQLite3_ErrorMsg
);
//Get all columns for the first time to count them.
//Its callback will return the count in SQLite3_Row_Ctr:
///
snprintf(SQL_String, 4096, "SELECT COALESCE(MAX(%s)+1, 0) FROM %s", config_idrowname, config_tablename);
SQLite3_DB_Res_Handle=sqlite3_exec(
SQLite3_DB_Obj,
SQL_String,
SQLite3_Callback2,
SQLite3_zTail_String,
&SQLite3_ErrorMsg
);
printf("Cycling through %ld files...\n", SQLite3_Row_Ctr-1);
while(1)
{
//Go to next row (selected randomly):
///
SQLite3_Current_Row=moreRandomFactor;
//Get random files to open:
///
snprintf(SQL_String, 4096, "SELECT %s FROM %s WHERE %s=%ld LIMIT 1", config_datarowname, config_tablename, config_idrowname, SQLite3_Current_Row);
SQLite3_DB_Res_Handle=sqlite3_exec(
SQLite3_DB_Obj,
SQL_String,
SQLite3_Callback,
SQLite3_zTail_String,
&SQLite3_ErrorMsg
);
//If there was a database error or fault, just end:
///
if(SQLite3_ErrorMsg)return 0;
}
return 0;
}
unsigned long long_getNumBits(unsigned long num)
{
unsigned long toRet=0;
while(num)
{
num>>=1;
toRet++;
}
return toRet;
}
unsigned long stringSum(char *str, unsigned int str_SZ)
{
int x=0;
unsigned long toRet=0;
for(x=0; x<str_SZ; x++)
{
toRet+=x;
toRet+=(int)str[x];
}
return toRet;
}
unsigned long getDateMilliseconds(void)
{
return clock();
}
unsigned long stringToRandomNumber(char *str, unsigned int str_SZ, unsigned long max_rand, unsigned long toRet)
{
unsigned long x=0;
toRet+=minmax_rand(1, max_rand);
for(x=0; x<str_SZ; x++)
{
toRet+=x;
toRet+=getDateMilliseconds();
toRet+=(unsigned long)str[x];
if(toRet>max_rand)toRet&=minmax_rand(1, max_rand);
}
if(toRet==0)toRet++;
return toRet;
}