Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <dirent.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <string.h>
- #include <fcntl.h>
- int first_test = 1;
- void listRec(const char *path, int size, int ok_name_ends, const char* name_ends_with)
- {
- DIR *dir = NULL;
- struct dirent *entry = NULL;
- char fullPath[512];
- struct stat statbuf;
- dir = opendir(path);
- if(dir == NULL) {
- printf("ERROR\n");
- perror("invalid directory path");
- return ;
- }
- printf("SUCCESS\n");
- while((entry = readdir(dir)) != NULL) {
- if(strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
- snprintf(fullPath, 512, "%s/%s", path, entry->d_name);
- if(lstat(fullPath, &statbuf) == 0) {
- if(ok_name_ends==1){
- if(strstr(entry->d_name,name_ends_with))
- {
- char aux[500] = "";
- strcpy(aux,entry->d_name + 17);
- char aux1[500] = "";
- strcpy(aux1,entry->d_name + 5);
- printf("%s\n", fullPath);
- }
- }
- else{
- if(size == -1){
- printf("%s\n", fullPath);
- }
- else if(statbuf.st_size > size && S_ISREG(statbuf.st_mode)){
- printf("%s\n", fullPath);
- }
- }
- }
- }
- }
- closedir(dir);
- }
- void listRecursivitate( const char *path, int size, int ok_name_ends, const char* name_ends_with)
- {
- DIR *dir = NULL;
- struct dirent *entry = NULL;
- char fullPath[512];
- struct stat statbuf;
- dir = opendir(path);
- if(dir == NULL) {
- printf("ERROR\n");
- perror("invalid directory path");
- return ;
- }
- if(first_test){
- printf("SUCCESS\n");
- }
- while((entry = readdir(dir)) != NULL) {
- if(strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
- snprintf(fullPath, 512, "%s/%s", path, entry->d_name);
- if(lstat(fullPath, &statbuf) == 0) {
- first_test = 0;
- if(ok_name_ends == 1){
- if(strstr(entry->d_name,name_ends_with))
- {
- char aux[500] = "";
- strcpy(aux,entry->d_name + 15);
- char aux1[500] = "";
- strcpy(aux1,entry->d_name + 5);
- printf("%s\n", fullPath);
- }
- }
- else{
- if(size == -1){
- printf("%s\n", fullPath);
- }
- else if(statbuf.st_size > size && S_ISREG(statbuf.st_mode)){
- printf("%s\n", fullPath);
- }
- if(S_ISDIR(statbuf.st_mode)) {
- listRecursivitate(fullPath,size, ok_name_ends, name_ends_with);
- }
- }
- }
- }
- }
- closedir(dir);
- }
- /*
- void parse(char *path){
- char magic = "";
- unsigned short header = 0;
- int version = 0;
- unsigned short no_of_section = 0;
- char name[13];
- unsigned short type = 0;
- int offset = 0;
- int size = 0;
- int fd = -1;
- fd = open(path, O_RDONLY);
- if(fd == -1){
- perror("Could not open input file!");
- return -1;
- }
- lseek(fd,-3,SEEK_SET);
- read(fd, &size, 2);
- read(fd, &magic, 1);
- if (magic != 'S')
- {
- printf("ERROR\nwrong magic\n");
- return;
- }
- lseek(fd, -size, SEEK_CUR);
- read(fd, &version, 2);
- if (version<126 || version>209)
- {
- printf("ERROR\nwrong version\n");
- return;
- }
- }
- */
- int parse(const char *path, int afiseaza)
- {
- int i=0,nr=0,size=0,version=0,fd=-1,ok=1,er=0;//1- magic,2-version 3-sect_nr 4-sect_type
- char magic[2]={0};
- char name[13]={0};
- int type=0;
- int offset=0;
- fd=open(path,O_RDONLY);
- if(fd==-1)
- {
- perror("Could not open input file!");
- return -1;
- }
- lseek(fd,0,SEEK_SET);
- if(read(fd,magic,1)==1)
- {
- //magic[0]=0;
- //printf("%d\n", strcmp(magic,"S"));
- if(strcmp(magic,"S")!=0)
- {
- ok=0;
- er=1;
- }
- }
- else {
- perror("Could not read from input file");
- close(fd);
- return -1;
- }
- if(read(fd,&size,2)!=2)
- {
- perror("Could not read from input file");
- close(fd);
- return -1;
- }
- if(read(fd,&version,2)==2)
- {
- if(version<126 || version >209)
- {
- if(ok){
- er=2;
- ok=0;
- }
- }
- }
- else {
- perror("Could not read from input file");
- close(fd);
- return -1;
- }
- if(read(fd,&nr,1)==1)
- {
- if(nr<3 || nr>10)
- {
- if(ok){
- er=3;
- ok=0;
- }
- }
- }
- else {
- perror("Could not read from input file");
- close(fd);
- return -1;
- }
- for(i=0;i<nr;i++)
- {
- //if(i==0)lseek(fd,9,SEEK_CUR);
- //else lseek(fd,17,SEEK_CUR);
- if(read(fd,&type,2)!=2)
- {
- perror("Could not read from input file");
- close(fd);
- return -1;
- }
- else
- {
- //printf("%d \n",type);
- if(type!=50 && type!=81)
- {
- {
- if(ok){
- er=4;
- ok=0;
- break;
- }
- }
- }
- }
- }
- if(er)
- { if(afiseaza){
- switch(er){
- case 1: printf("\nERROR\nwrong magic"); break;
- case 2: printf("\nERROR\nwrong version\n"); break;
- case 3: printf("ERROR\nwrong sect_nr\n");break;
- default: printf("ERROR\nwrong sect_types");
- }
- }
- close(fd);
- return -1;
- }
- else {
- if(afiseaza==1)
- {printf("SUCCESS\nversion=%d\nnr_sections=%d\n",version,nr);
- lseek(fd,11,SEEK_SET);
- for(i=0;i<nr;i++)
- {
- if(read(fd,name,12)!=12)
- {
- perror("Could not read from input file");
- close(fd);
- return -1;
- }
- name[12]=0;
- if(read(fd,&type,2)!=2)
- {
- perror("Could not read from input file");
- close(fd);
- return -1;
- }
- if(read(fd,&offset,4)!=4)
- {
- perror("Could not read from input file");
- close(fd);
- return -1;
- }
- if(read(fd,&size,4)!=4)
- {
- perror("Could not read from input file");
- close(fd);
- return -1;
- }
- printf("section%d: %s %d %d\n",i+1,name,type, size);
- }
- }
- close(fd);
- return 1;
- }
- }
- int main(int argc, char **argv){
- int list = 0, recursive = 0;
- int ok_name_ends = 0;
- char path[200];
- int parse1 = 0;
- char *name_ends_with = NULL;
- int size = -1;
- int i = 1;
- if(argc >= 2){
- if(strcmp(argv[1], "variant") == 0){
- printf("95965\n");
- }
- else {
- while(i < argc){
- if(strcmp(argv[i],"parse") == 0){
- parse1 = 1;
- }
- else if(strcmp(argv[i],"list") == 0){
- list = 1;
- }
- else if(strcmp(argv[i], "recursive") == 0){
- recursive = 1;
- }
- else if(strstr(argv[i],"path")){
- strcpy(path,strstr(argv[i],"="));
- strcpy(path,path+1);
- }
- else if(strncmp(argv[i], "size_greater=", 13) == 0) {
- char *nr_str = argv[i] + 13;
- size = atoi(nr_str);
- }
- else if(strncmp(argv[i],"name_ends_with", 15) != 0){
- ok_name_ends = 1;
- name_ends_with = argv[i] + 15;
- }
- i++;
- }
- if(list == 1){
- if(recursive == 1){
- listRecursivitate( path, size, ok_name_ends, name_ends_with);
- }
- else{
- listRec(path, size, ok_name_ends, name_ends_with);
- }
- }
- else if(parse1 == 0){
- listRec(path, size, ok_name_ends, name_ends_with);
- }
- if(parse1 == 1){
- parse(path, 1);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement