Jeremiah_

SO SC-B

Sep 30th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <sys/fcntl.h>
  5. #include <time.h>
  6. #include <unistd.h>
  7. #include <dirent.h>
  8. #include <string.h>
  9. #define MAXN 11234
  10.  
  11. int main() {
  12.     DIR *dir = opendir("./"); //System call para abrir diretório
  13.     struct dirent *entry;
  14.     struct stat status;
  15.     int arq = open("output.txt",  O_APPEND| O_CREAT |O_TRUNC| O_WRONLY, S_IRWXU); //abre o arquivo ou cria se não existir ainda
  16.     char dates[MAXN];
  17.     if (dir != NULL) {//se não houver erro
  18.         while (entry = readdir(dir)) { // enquanto houver diretório/arquivo...
  19.             if (stat(entry->d_name, &status) == -1) { // recupera informação sobre o arquivo
  20.                 puts("ERROR");
  21.             } else {
  22.                 if (S_ISREG(status.st_mode) || S_ISDIR(status.st_mode)) { // verifica se é diretório ou arquivo regular
  23.  
  24.                     //File Name: <nome_do_arquivo>
  25.                     strcpy(dates, "File Name: ");
  26.                     strcpy(dates+11, entry->d_name);
  27.                     //escreve no output.txt
  28.                     write(arq, dates, strlen(dates));
  29.  
  30.                     //Last Access: <last_access_time>
  31.                     strcpy(dates, "\nLast Access: ");
  32.                     strcpy(dates+14, asctime(localtime(&(status.st_atime))));// conversão de tempo
  33.                     //escreve...
  34.                     write(arq, dates, strlen(dates));
  35.  
  36.                     //Last Modify: <last_modify_time>
  37.                     strcpy(dates, "Last Modify: ");
  38.                     strcpy(dates+13, asctime(localtime(&(status.st_mtime))));
  39.                     //escreve...
  40.                     write(arq, dates, strlen(dates));
  41.  
  42.                     //Last Change: <last_change_time>
  43.                     strcpy(dates, "Last Change: ");
  44.                     strcpy(dates+13, asctime(localtime(&(status.st_ctime))));
  45.                     //escreve...
  46.                     write(arq, dates, strlen(dates));
  47.                     //write(arq, (char*)"\n", 2);
  48.                 }
  49.             }
  50.         }
  51.  
  52.         closedir(dir); // fecha diretório
  53.     } else {
  54.         puts("MEGA ERROR");
  55.     }
  56.     close(arq); // fecha output
  57.     return 0;
  58. }
Add Comment
Please, Sign In to add comment