shadeyourself

Dirwalker: .avi

Jun 2nd, 2020 (edited)
2,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.93 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <dirent.h>
  5. #include <string.h>
  6. #include <sys/stat.h>
  7. #include <stdlib.h>
  8.  
  9. int ifAvi(const char * filePath);
  10. int getSize(const char * filePath);
  11. int ifSame(const char * firsFilePath, const char * secondFilePath);
  12. void aviCount(const char * dir,  int * aviAmount);
  13. void getAviPathes(const char * dir,  char ** aviPathes, int * num);
  14.  
  15.  
  16. //C:/Users/m video/Desktop/videos
  17. int main(){
  18.     char startDir[PATH_MAX+1], finishDir[PATH_MAX+1];
  19.    
  20.     gets(startDir);
  21.     gets(finishDir);
  22.  
  23.     strcat(finishDir, "/");
  24.     strcat(finishDir, "avis");
  25.  
  26.     mkdir(finishDir);
  27.  
  28.     int aviAmount = 0;
  29.     aviCount(startDir, &aviAmount);
  30.  
  31.     char *aviPathes[aviAmount];
  32.     for(int i = 0; i < aviAmount; i++)
  33.         aviPathes[i] = (char*)malloc(sizeof(char)*(PATH_MAX + 1));
  34.     int num = 0;
  35.     getAviPathes(startDir, aviPathes, &num);
  36.  
  37.     printf("-------------------------\n");
  38.  
  39.     //for(int i = 0; i < aviAmount; i++)
  40.         //printf("%s\n", aviPathes[i]);
  41.  
  42.     int count = 1;
  43.     for(int i = 0; i < aviAmount - 1; i++){
  44.         for(int j = i + 1; j < aviAmount; j++){
  45.             if(ifSame(aviPathes[i], aviPathes[j]) != 1) continue;
  46.             char newPath[PATH_MAX + 1];
  47.             char number[5];
  48.             strcpy(newPath, finishDir);
  49.             strcat(newPath, "/map_");
  50.             if(count > 9 && count < 100) strcat(newPath, "0");
  51.             if(count < 10) strcat(newPath, "00");
  52.             strcat(newPath, itoa(count, number, 10));
  53.             strcat(newPath, ".avi");
  54.             printf("OLDPATH: %s\nNEWPATH: %s\n", aviPathes[i], newPath);
  55.             rename(aviPathes[i], newPath);
  56.             count++;
  57.         }
  58.     }
  59.  
  60.     return 0;
  61. }
  62. //C:/Users/m video/Desktop/videos
  63. int getSize(const char * filePath){
  64.     int size;
  65.  
  66.     FILE * f = fopen(filePath, "r");
  67.     if(!f) return -1;
  68.  
  69.     fseek(f, 0L, SEEK_END);
  70.     size = ftell(f);
  71.  
  72.     fclose(f);
  73.     return size;
  74. }
  75. //C:/Users/m video/Desktop/videos
  76. int ifAvi(const char * filePath){
  77.     FILE * f = fopen(filePath, "rb");
  78.     if(!f) return -1;
  79.  
  80.     char buf[13];
  81.     size_t result = fread(buf, 1, 12, f);
  82.  
  83.     if(result < 12) {
  84.         fclose(f);
  85.         return 0;
  86.     }
  87.  
  88.     if(buf[0] != 'R') {
  89.         fclose(f);
  90.         return 0;
  91.     }
  92.     if(buf[1] != 'I') {
  93.         fclose(f);
  94.         return 0;
  95.     }
  96.     if(buf[2] != 'F') {
  97.         fclose(f);
  98.         return 0;
  99.     }
  100.     if(buf[3] != 'F') {
  101.         fclose(f);
  102.         return 0;
  103.     }
  104.     if(buf[8] != 'A') {
  105.         fclose(f);
  106.         return 0;
  107.     }
  108.     if(buf[9] != 'V') {
  109.         fclose(f);
  110.         return 0;
  111.     }
  112.     if(buf[10] != 'I') {
  113.         fclose(f);
  114.         return 0;
  115.     }
  116.     fclose(f);
  117.     return 1;
  118. }
  119. //C:/Users/m video/Desktop/videos
  120. int ifSame(const char * firsFilePath, const char * secondFilePath){
  121.     FILE * fisrtFile = fopen(firsFilePath, "r");
  122.     if(!fisrtFile) return -1;
  123.  
  124.     FILE * secondFile = fopen(secondFilePath, "r");
  125.     if(!secondFile) {
  126.         fclose(fisrtFile);
  127.         return -1;
  128.     }
  129.  
  130.     int firstFileSize = getSize(firsFilePath);
  131.     int secondFileSize = getSize(secondFilePath);
  132.     char first;
  133.     char second;
  134.  
  135.     if(firstFileSize != secondFileSize) {
  136.         fclose(fisrtFile);
  137.         fclose(secondFile);
  138.         return 0;
  139.     }
  140.  
  141.     for(int i; i<firstFileSize; i++){
  142.         first = fgetc(fisrtFile);
  143.         second = fgetc(secondFile);
  144.         if(first!=second) {
  145.             fclose(fisrtFile);
  146.             fclose(secondFile);
  147.             return 0;
  148.         }
  149.     }
  150.     fclose(fisrtFile);
  151.     fclose(secondFile);
  152.     return 1;
  153. }
  154. //C:/Users/m video/Desktop/videos
  155. void aviCount(const char * dir, int * aviAmount){
  156.     DIR *dp;
  157.     if((dp = opendir(dir)) == NULL) {
  158.         printf("cannot open directory\n");
  159.         return;
  160.     }
  161.  
  162.     char currentPath[PATH_MAX+1];
  163.  
  164.     struct dirent *entry;
  165.     struct stat statbuf;
  166.  
  167.     chdir(dir);
  168.     while((entry = readdir(dp)) != NULL) {
  169.  
  170.         strcpy(currentPath, dir);
  171.         strcat(currentPath, "/");
  172.         strcat(currentPath, entry->d_name);
  173.  
  174.         stat(entry->d_name,&statbuf);
  175.         if(S_ISDIR(statbuf.st_mode)) {
  176.             if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0 || strcmp("avis",entry->d_name) == 0)
  177.                 continue;
  178.             aviCount(currentPath, aviAmount);
  179.             continue;
  180.         }
  181.         if(ifAvi(currentPath) == 1) *(aviAmount) = *(aviAmount) + 1;
  182.     }
  183.     chdir("..");
  184.     closedir(dp);
  185. }
  186. void getAviPathes(const char * dir, char ** aviPathes, int * num){
  187.     DIR *dp;
  188.     if((dp = opendir(dir)) == NULL) {
  189.         printf("cannot open directory\n");
  190.         return;
  191.     }
  192.  
  193.     char currentPath[PATH_MAX+1];
  194.  
  195.     struct dirent *entry;
  196.     struct stat statbuf;
  197.  
  198.     chdir(dir);
  199.     while((entry = readdir(dp)) != NULL) {
  200.  
  201.         strcpy(currentPath, dir);
  202.         strcat(currentPath, "/");
  203.         strcat(currentPath, entry->d_name);
  204.  
  205.         stat(entry->d_name,&statbuf);
  206.         if(S_ISDIR(statbuf.st_mode)) {
  207.             if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0 || strcmp("avis",entry->d_name) == 0)
  208.                 continue;
  209.             getAviPathes(currentPath, aviPathes, num);
  210.             continue;
  211.         }
  212.        
  213.         if(ifAvi(currentPath) == 1){
  214.             strcpy(aviPathes[(*num)],currentPath);
  215.             (*num) = (*num) + 1;
  216.         }
  217.     }
  218.     chdir("..");
  219.     closedir(dp);
  220. }
Add Comment
Please, Sign In to add comment