Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include <libio.h>
- //#include <boost/regex.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include "pcre.h"
- #ifdef TARGET_OS_MAC
- #error Cant be compiled on MAC yet
- #elif defined _WIN32
- #ifdef __MINGW32__
- #define WIN32_LEAN_AND_MEAN /* exclude winsock.h because of clash with select() */
- //#include <windows.h> /* needed for WinAPI stuff */
- #undef WIN32_LEAN_AND_MEAN
- #include <winsock2.h>
- #else
- #include <windows.h>
- #include <winsock.h>
- #endif
- #include "mysql/mysql.h"
- #elif defined __linux__
- #include "mysql/mysql.h"
- #else
- #error "Unknow platform"
- #endif
- #define OVECCOUNT 100
- static int zorg = 0;
- int preg_match(char *str_match, char *str_pattern)
- {
- /*
- * pattern
- * 0 // default options
- * &error // error message
- * &erroffset
- * NULL // default character tables
- *
- * */
- const char *error;
- char *matches;
- int erroffset;
- pcre *re;
- int rc, i;
- char *rm;
- int ovector[OVECCOUNT];
- re = pcre_compile(str_pattern, PCRE_CASELESS | PCRE_MULTILINE, &error, &erroffset, NULL);
- if(!re)
- {
- printf("pcre_compile failed (offset: %d), %s\n", erroffset, error);
- return -1;
- }
- /*
- * re // compiled pattern
- * NULL, // no extra data
- * string
- * stringLen
- * 0 // start at offset 0 in the string
- * 0 // default options
- * ovector // output vector info
- * OVECCOUNT// number of element in ovec
- * */
- unsigned int offset = 0;
- unsigned int len = strlen(str_match);
- while(offset < len && (rc = pcre_exec(re, NULL, str_match, len, offset, 0, ovector, OVECCOUNT)) >= 0)
- {
- if(rc < 0)
- {
- switch(rc)
- {
- case PCRE_ERROR_NOMATCH:
- printf("String didn't match\n"); break;
- default:
- printf("Error while matching: %d\n"); break;
- }
- pcre_free(re);
- return -1;
- }else
- {
- if(rc == 0)
- {
- }
- for(i = 0; i < rc; i++)
- {
- char *sub = str_match + ovector[2*i];
- int sublen = ovector[2*i+1] - ovector[2*i];
- printf("%.*s, ", sublen, sub);
- }
- offset = ovector[1];
- }
- }
- return 0;
- }
- int is_dir(char *path)
- {
- DIR *rootD=NULL;
- if((rootD = opendir(path))!=NULL)
- {
- closedir(rootD);
- return 1;
- }
- closedir(rootD);
- return 0;
- }
- int search(char *path, char *pattern)
- {
- DIR *rootD;
- struct dirent *Entry;
- char *real_path=NULL;
- if((rootD=opendir(path)) == NULL)
- return -1;
- while(Entry = readdir(rootD))
- {
- if(real_path!=NULL)
- {
- free(real_path);
- real_path=NULL;
- }
- if(!strcmp(Entry->d_name,".") || !strcmp(Entry->d_name,".."))
- continue;
- real_path= (char *)malloc( strlen(path) + strlen(Entry->d_name) + 2);
- sprintf(real_path,"%s/%s",path,Entry->d_name);
- if( is_dir(real_path) == 1 )
- {
- search(real_path, pattern);
- }else
- {
- printf("\n\n> [%s]\n",real_path);
- regFilter(real_path, pattern);
- zorg++;
- }
- }
- closedir(rootD);
- if(real_path!=NULL)
- {
- free(real_path);
- real_path=NULL;
- }
- printf("\n");
- return 0;
- }
- int regFilter(char *path, char *pattern)
- {
- unsigned char *buffer;
- FILE *stream;
- struct stat st;
- int c, fsize, sfsize;
- if((stream = fopen(path, "r")) == NULL)
- return -1;
- stat(path, &st);
- sfsize = st.st_size;
- fseek(stream, 0L, SEEK_END);
- fsize = ftell(stream);
- fseek(stream, 0L, SEEK_SET);
- rewind(stream);
- if(sfsize == fsize && fsize <= 1000000)
- {
- if((buffer = malloc(sizeof(buffer)*fsize+1)) == NULL)
- {
- fprintf(stderr, "out of memory: %d\n", fsize);
- fclose(stream);
- return EXIT_FAILURE;
- }
- fread(buffer, fsize, 1, stream);
- preg_match(buffer, pattern);
- fclose(stream);
- }
- return 0;
- }
- void viewBuf(char *buffer, int fsize)
- {
- int c;
- for(c=0;c<fsize;c++)
- {
- printf("%.2X ", (int)buffer[c]);
- // put an extra space between every 4 bytes
- if(c % 4 == 3)
- printf(" ");
- // Display 16 bytes per line
- if(c % 16 == 15)
- printf("\n");
- }
- }
- int main(int ac, char **av)
- {
- if(ac == 3)
- search(av[2], av[1]);
- else
- return EXIT_FAILURE;
- printf("\n");
- MYSQL *conn;
- MYSQL_RES *res;
- MYSQL_ROW row;
- char *server = "localhost";
- char *user = "root";
- char *password = "";
- char *database = "mysql";
- conn = mysql_init(NULL);
- if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
- {
- fprintf(stderr, "%s\n", mysql_error(conn));
- exit(1);
- }else
- {
- printf("[%s]\n", mysql_get_client_info());
- }
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment