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);
- }
- int parse(const char *path, int afiseaza)
- {
- int size=0,version=0,fd=-1,ok=1;
- unsigned short no_of_sections = 0;
- char magic;
- char section_header[13]={0};
- unsigned short type=0;
- fd=open(path,O_RDONLY);
- if(fd==-1){
- perror("Could not open input file!");
- return -1;
- }
- lseek(fd,0,SEEK_SET);
- read(fd, &magic, 1);
- if(magic != 'S'){
- ok=0;
- printf("\nERROR\nwrong magic");
- return -1;
- }
- read(fd,&size,2);
- read(fd,&version,2);
- if(version<126 || version >209){
- if(ok){
- ok=0;
- printf("\nERROR\nwrong version\n");
- return -1;
- }
- }
- read(fd,&no_of_sections,1);
- if(no_of_sections<3 || no_of_sections>10){
- if(ok){
- ok=0;
- printf("ERROR\nwrong sect_nr\n");
- return -1;
- }
- }
- for(int i=0;i<no_of_sections;i++){
- read(fd,&type,2);
- //printf("%d \n",type);
- if(type!=50 && type!=81){
- if(ok){
- ok=0;
- printf("ERROR\nwrong sect_types\n");
- return -1;
- }
- }
- }
- if(ok == 1){
- if(afiseaza == 1){
- printf("SUCCESS\n version=%d\n nr_sections=%d\n", version,no_of_sections);
- lseek(fd, 11, SEEK_SET);
- for(int i=0; i<no_of_sections; i++){
- printf("section%d: %s %d %d\n", i+1, section_header, 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){
- listRecursivitate(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