Guest User

Untitled

a guest
May 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.43 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <sys/stat.h>
  5. #include <dirent.h>
  6. #include <stdint.h>
  7. void searchDir(char *dirname, int stepsIn);
  8. int filesAllocatedSpace(int size, int blockSize);
  9. long long int allocatedSpace = 0;
  10. long long int usedSpace = 0;
  11.  
  12.  
  13.  
  14. int main() {
  15.     printf("file i-node example\n"); // CHANGE THIS STUFF
  16.     char *directoryLocation;
  17.     directoryLocation = (char*)malloc(256*sizeof(directoryLocation));
  18.     strcpy(directoryLocation, "./");
  19.     searchDir(directoryLocation, 0);
  20.    
  21.     return 1;
  22. }
  23.  
  24. int filesAllocatedSpace(int size, int blockSize) {
  25.     int SpaceAlocated = 0;
  26.     do {
  27.         SpaceAlocated += blockSize
  28.     } while (size > SpaceAlocated)
  29.     return SpaceAlocated;
  30. }
  31.  
  32. void searchDir(char *dirname,int stepsIn)
  33. {
  34.         DIR *currentDirectory;
  35.         struct dirent *fileEntry;
  36.         char *locationToPassIn, *directoryLocation;
  37.         locationToPassIn = (char*)malloc(256*sizeof(locationToPassIn));
  38.         directoryLocation = (char*)malloc(256*sizeof(directoryLocation));
  39.         struct stat fileStats;
  40.         strcpy(directoryLocation, dirname);
  41.         currentDirectory = opendir(directoryLocation);
  42.         if (currentDirectory != NULL)
  43.         {
  44.             while ((fileEntry = readdir(currentDirectory)) != NULL)
  45.             {
  46.                 if((strncmp(fileEntry->d_name, "..", 2) != 0) && (strncmp(fileEntry->d_name, ".", 1) != 0))
  47.                 {
  48.                     strcpy(locationToPassIn, fileEntry->d_name);
  49.                     stat(locationToPassIn, &fileStats);
  50.                     switch(IS_REG(fileStats.)
  51.                     {
  52.                         case DT_REG: // FILE
  53.                         allocatedSpace += fileAllocatedSpace(fileStats.st_size,SUPERBLOCK.block_size);
  54.                         usedSpace += fileStats.st_size;
  55.                                     printf("<file name>: %s <inode>: %ld <size>: %ld and <unused space>: %ld\n", fileEntry->d_name,fileEntry->d_ino, fileStats.st_size, ((fileStats.st_blksize*fileStats.st_blocks) - fileStats.st_size));
  56.                         break;
  57.                         case DT_DIR: // DIRECTORY
  58.                         allocatedSpace += fileAllocatedSpace(fileStats.st_size,SUPERBLOCK.block_size);
  59.                         usedSpace += fileStats.st_size;
  60.                                     printf("<Folder name>: %s <inode>: %ld <size>: %ld and <unused space>: %ld\n", fileEntry->d_name,fileEntry->d_ino, fileStats.st_size,((fileStats.st_blksize*fileStats.st_blocks) - fileStats.st_size));
  61.                         strcpy(directoryLocation,dirname);
  62.                         strcat(directoryLocation, "/");
  63.                         strcat(directoryLocation,fileEntry->d_name);
  64.                         searchDir(directoryLocation, stepsIn+1);
  65.                         break;
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.         closedir(currentDirectory);
  71.         return;
  72. }
Add Comment
Please, Sign In to add comment