Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <dirent.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <stdlib.h>
- int ifAvi(const char * filePath);
- int getSize(const char * filePath);
- int ifSame(const char * firsFilePath, const char * secondFilePath);
- void aviCount(const char * dir, int * aviAmount);
- void getAviPathes(const char * dir, char ** aviPathes);
- //C:/Users/m video/Desktop/videos
- int main(){
- char startDir[PATH_MAX+1], finishDir[PATH_MAX+1];
- int aviAmount = 0;
- gets(startDir);
- gets(finishDir);
- printf("-------------------------\n");
- strcat(finishDir, "/");
- strcat(finishDir, "avis");
- //mkdir(finishDir);
- aviCount(startDir, &aviAmount);
- printf("%d\n", aviAmount);
- char *aviPathes[aviAmount];
- for(int i;i<aviAmount;i++)
- aviPathes[i] = (char*)malloc(sizeof(char)*(PATH_MAX + 1));
- getAviPathes(startDir, aviPathes);
- printf("-------------------------\n");
- //for(int i = 0;i<aviAmount;i++)
- //printf("%s\n", aviPathes);
- return 0;
- }
- //C:/Users/m video/Desktop/videos
- int getSize(const char * filePath){
- int size;
- FILE * f = fopen(filePath, "r");
- if(!f)return -1;
- fseek(f, 0L, SEEK_END);
- size = ftell(f);
- fclose(f);
- return size;
- }
- //C:/Users/m video/Desktop/videos
- int ifAvi(const char * filePath){
- FILE * f = fopen(filePath, "rb");
- if(!f) return -1;
- char buf[13];
- size_t result = fread(buf, 1, 12, f);
- if(result < 12) return 0;
- if(buf[0] != 'R') return 0;
- if(buf[1] != 'I') return 0;
- if(buf[2] != 'F') return 0;
- if(buf[3] != 'F') return 0;
- if(buf[8] != 'A') return 0;
- if(buf[9] != 'V') return 0;
- if(buf[10] != 'I') return 0;
- fclose(f);
- return 1;
- }
- //C:/Users/m video/Desktop/videos
- int ifSame(const char * firsFilePath, const char * secondFilePath){
- FILE * fisrtFile = fopen(firsFilePath, "r");
- if(!fisrtFile) return -1;
- FILE * secondFile = fopen(secondFilePath, "r");
- if(!secondFile) return -1;
- int firstFileSize = getSize(firsFilePath);
- int secondFileSize = getSize(secondFilePath);
- char first;
- char second;
- if(firstFileSize != secondFileSize) return 0;
- for(int i; i<firstFileSize; i++){
- first = fgetc(fisrtFile);
- second = fgetc(secondFile);
- if(first!=second) return 0;
- }
- return 1;
- }
- //C:/Users/m video/Desktop/videos
- void aviCount(const char * dir, int * aviAmount){
- DIR *dp;
- if((dp = opendir(dir)) == NULL) {
- printf("cannot open directory\n");
- return;
- }
- char currentPath[PATH_MAX+1];
- struct dirent *entry;
- struct stat statbuf;
- chdir(dir);
- while((entry = readdir(dp)) != NULL) {
- strcpy(currentPath, dir);
- strcat(currentPath, "/");
- strcat(currentPath, entry->d_name);
- stat(entry->d_name,&statbuf);
- if(S_ISDIR(statbuf.st_mode)) {
- if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0 || strcmp("avis",entry->d_name) == 0)
- continue;
- aviCount(currentPath, aviAmount);
- }
- if(ifAvi(currentPath) == 1) *(aviAmount) = *(aviAmount) + 1;
- }
- chdir("..");
- closedir(dp);
- }
- void getAviPathes(const char * dir, char ** aviPathes){
- DIR *dp;
- if((dp = opendir(dir)) == NULL) {
- printf("cannot open directory\n");
- return;
- }
- char currentPath[PATH_MAX+1];
- struct dirent *entry;
- struct stat statbuf;
- chdir(dir);
- while((entry = readdir(dp)) != NULL) {
- strcpy(currentPath, dir);
- strcat(currentPath, "/");
- strcat(currentPath, entry->d_name);
- stat(entry->d_name,&statbuf);
- if(S_ISDIR(statbuf.st_mode)) {
- if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0 || strcmp("avis",entry->d_name) == 0)
- continue;
- getAviPathes(currentPath, aviPathes);
- }
- if(ifAvi(currentPath) == 1){
- printf("%s\n", currentPath);
- strcpy(*aviPathes,currentPath);
- aviPathes++;
- }
- }
- chdir("..");
- closedir(dp);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement