Skip to content

Instantly share code, notes, and snippets.

@thinkycx
Created June 19, 2018 04:34
Show Gist options
  • Save thinkycx/a6359201f79ffdf4e97a111c5fe4cf01 to your computer and use it in GitHub Desktop.
Save thinkycx/a6359201f79ffdf4e97a111c5fe4cf01 to your computer and use it in GitHub Desktop.
Only support to scan folder and regular file.Maybe support symbolic etc in future...
/*
date: 20180619
author: thinkycx
usage: gcc scanfolder.c -o scanfolder
./scanfolder [foldername]
./scscanfolderan /home
Only support to scan folder and regular file.Maybe support symbolic etc in future...
*/
#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(char *path){
#define SEARCH_PATH "/home"
struct foldernode{
char *path; // point to foldername or filename path
struct foldernode *next;
};
DIR *dir;
struct stat statbuf;
struct dirent *ptr;
char *filename;
char *filepath;
char *data;
char *foldername ;
char *folderpath ;
struct foldernode folderstart;
folderstart.path = path;
folderstart.next = NULL;
struct foldernode * folderfirst; // use to search
folderfirst = &folderstart;
struct foldernode * folderlast; // use to add foldernode
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){ //folder
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);
printf("[*]FILE %s\n",filepath);
free(filename);
free(filepath);
}
else if(ptr->d_type == 4){ //file
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);
printf("[*]FOLDER %s\n",folderpath);
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; // change folderfirst point to next foldernode
closedir(dir);
}
}
int main(int argc,char **argv, char **env) {
if(argc == 2){
f(argv[1]);
}else{
printf("Usage: scanfolder [foldername]\n\tscanfolder /home\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment