Guest User

Untitled

a guest
Jul 23rd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.67 KB | None | 0 0
  1. //#include <libio.h>
  2. //#include <boost/regex.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <dirent.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include "pcre.h"
  12.  
  13. #ifdef TARGET_OS_MAC
  14.     #error Cant be compiled on MAC yet
  15. #elif defined _WIN32
  16.     #ifdef __MINGW32__
  17.         #define WIN32_LEAN_AND_MEAN  /* exclude winsock.h because of clash with select() */
  18.         //#include <windows.h>         /* needed for WinAPI stuff */
  19.         #undef WIN32_LEAN_AND_MEAN
  20.         #include <winsock2.h>
  21.     #else
  22.         #include <windows.h>
  23.         #include <winsock.h>
  24.     #endif
  25.     #include "mysql/mysql.h"
  26. #elif defined __linux__
  27.     #include "mysql/mysql.h"
  28. #else
  29.     #error "Unknow platform"
  30. #endif
  31.  
  32.  
  33. #define OVECCOUNT 100
  34. static int zorg = 0;
  35.  
  36.  
  37. int preg_match(char *str_match, char *str_pattern)
  38. {
  39.     /*
  40.     * pattern
  41.     * 0      // default options
  42.     * &error   // error message
  43.     * &erroffset
  44.     * NULL     // default character tables
  45.     *
  46.     * */
  47.  
  48.     const char  *error;
  49.     char        *matches;
  50.     int         erroffset;
  51.     pcre        *re;
  52.     int         rc, i;
  53.     char        *rm;
  54.     int         ovector[OVECCOUNT];
  55.  
  56.     re = pcre_compile(str_pattern, PCRE_CASELESS | PCRE_MULTILINE, &error, &erroffset, NULL);
  57.     if(!re)
  58.     {
  59.         printf("pcre_compile failed (offset: %d), %s\n", erroffset, error);
  60.         return -1;
  61.     }
  62.  
  63.     /*
  64.     * re     // compiled pattern
  65.     * NULL,  // no extra data
  66.     * string
  67.     * stringLen
  68.     * 0    // start at offset 0 in the string
  69.     * 0    // default options
  70.     * ovector  // output vector info
  71.     * OVECCOUNT// number of element in ovec
  72.     * */
  73.  
  74.     unsigned int offset = 0;
  75.     unsigned int len = strlen(str_match);
  76.     while(offset < len && (rc = pcre_exec(re, NULL, str_match, len, offset, 0, ovector, OVECCOUNT)) >= 0)
  77.     {
  78.         if(rc < 0)
  79.         {
  80.             switch(rc)
  81.             {
  82.                 case PCRE_ERROR_NOMATCH:
  83.                     printf("String didn't match\n"); break;
  84.                 default:
  85.                     printf("Error while matching: %d\n"); break;
  86.             }
  87.             pcre_free(re);
  88.             return -1;
  89.         }else
  90.         {
  91.             if(rc == 0)
  92.             {
  93.  
  94.             }
  95.  
  96.             for(i = 0; i < rc; i++)
  97.             {
  98.                 char  *sub = str_match + ovector[2*i];
  99.                 int   sublen = ovector[2*i+1] - ovector[2*i];
  100.                 printf("%.*s, ", sublen, sub);
  101.             }
  102.             offset = ovector[1];
  103.         }
  104.     }
  105.     return 0;
  106. }
  107.  
  108.  
  109. int is_dir(char *path)
  110. {
  111.     DIR *rootD=NULL;
  112.  
  113.     if((rootD = opendir(path))!=NULL)
  114.     {
  115.         closedir(rootD);
  116.         return 1;
  117.     }
  118.     closedir(rootD);
  119.     return 0;
  120. }
  121.  
  122. int search(char *path, char *pattern)
  123. {
  124.     DIR             *rootD;
  125.     struct dirent   *Entry;
  126.     char            *real_path=NULL;
  127.  
  128.     if((rootD=opendir(path)) == NULL)
  129.         return -1;
  130.  
  131.     while(Entry = readdir(rootD))
  132.     {
  133.         if(real_path!=NULL)
  134.         {
  135.             free(real_path);
  136.             real_path=NULL;
  137.         }
  138.        
  139.         if(!strcmp(Entry->d_name,".") || !strcmp(Entry->d_name,".."))
  140.             continue;
  141.  
  142.         real_path= (char *)malloc( strlen(path) + strlen(Entry->d_name) + 2);
  143.         sprintf(real_path,"%s/%s",path,Entry->d_name);
  144.        
  145.         if( is_dir(real_path) == 1 )
  146.         {
  147.             search(real_path, pattern);
  148.         }else
  149.         {
  150.             printf("\n\n> [%s]\n",real_path);
  151.             regFilter(real_path, pattern);
  152.             zorg++;
  153.         }
  154.     }
  155.  
  156.     closedir(rootD);
  157.     if(real_path!=NULL)
  158.     {
  159.         free(real_path);
  160.         real_path=NULL;
  161.     }
  162.     printf("\n");
  163.     return 0;
  164. }
  165.  
  166.  
  167. int regFilter(char *path, char *pattern)
  168. {
  169.     unsigned char   *buffer;
  170.     FILE            *stream;
  171.     struct          stat st;
  172.     int             c, fsize, sfsize;
  173.  
  174.     if((stream = fopen(path, "r")) == NULL)
  175.         return -1;
  176.  
  177.     stat(path, &st);
  178.     sfsize = st.st_size;
  179.    
  180.     fseek(stream, 0L, SEEK_END);
  181.     fsize = ftell(stream);
  182.     fseek(stream, 0L, SEEK_SET);
  183.     rewind(stream);
  184.  
  185.     if(sfsize == fsize && fsize <= 1000000)
  186.     {
  187.         if((buffer = malloc(sizeof(buffer)*fsize+1)) == NULL)
  188.         {
  189.             fprintf(stderr, "out of memory: %d\n", fsize);
  190.             fclose(stream);
  191.             return EXIT_FAILURE;
  192.         }
  193.  
  194.         fread(buffer, fsize, 1, stream);
  195.         preg_match(buffer, pattern);
  196.         fclose(stream);
  197.     }
  198.     return 0;
  199. }
  200.  
  201. void viewBuf(char *buffer, int fsize)
  202. {
  203.   int   c;
  204.  
  205.   for(c=0;c<fsize;c++)
  206.   {
  207.     printf("%.2X ", (int)buffer[c]);
  208.  
  209.     // put an extra space between every 4 bytes
  210.     if(c % 4 == 3)
  211.       printf(" ");
  212.  
  213.     // Display 16 bytes per line
  214.     if(c % 16 == 15)
  215.       printf("\n");
  216.   }
  217. }
  218.  
  219.  
  220. int main(int ac, char **av)
  221. {
  222.     if(ac == 3)
  223.         search(av[2], av[1]);
  224.     else
  225.         return EXIT_FAILURE;
  226.     printf("\n");
  227.    
  228.     MYSQL       *conn;
  229.     MYSQL_RES   *res;
  230.     MYSQL_ROW   row;
  231.     char        *server = "localhost";
  232.     char        *user = "root";
  233.     char        *password = "";
  234.     char        *database = "mysql";
  235.     conn = mysql_init(NULL);
  236.    
  237.     if(!mysql_real_connect(conn, server, user, password, database, 0, NULL, 0))
  238.     {
  239.         fprintf(stderr, "%s\n", mysql_error(conn));
  240.         exit(1);
  241.     }else
  242.     {
  243.         printf("[%s]\n", mysql_get_client_info());
  244.     }
  245.   return EXIT_SUCCESS;
  246. }
Add Comment
Please, Sign In to add comment