Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/fcntl.h>
- #include <time.h>
- #include <unistd.h>
- #include <dirent.h>
- #include <string.h>
- #define MAXN 11234
- int main() {
- DIR *dir = opendir("./"); //System call para abrir diretório
- struct dirent *entry;
- struct stat status;
- 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
- char dates[MAXN];
- if (dir != NULL) {//se não houver erro
- while (entry = readdir(dir)) { // enquanto houver diretório/arquivo...
- if (stat(entry->d_name, &status) == -1) { // recupera informação sobre o arquivo
- puts("ERROR");
- } else {
- if (S_ISREG(status.st_mode) || S_ISDIR(status.st_mode)) { // verifica se é diretório ou arquivo regular
- //File Name: <nome_do_arquivo>
- strcpy(dates, "File Name: ");
- strcpy(dates+11, entry->d_name);
- //escreve no output.txt
- write(arq, dates, strlen(dates));
- //Last Access: <last_access_time>
- strcpy(dates, "\nLast Access: ");
- strcpy(dates+14, asctime(localtime(&(status.st_atime))));// conversão de tempo
- //escreve...
- write(arq, dates, strlen(dates));
- //Last Modify: <last_modify_time>
- strcpy(dates, "Last Modify: ");
- strcpy(dates+13, asctime(localtime(&(status.st_mtime))));
- //escreve...
- write(arq, dates, strlen(dates));
- //Last Change: <last_change_time>
- strcpy(dates, "Last Change: ");
- strcpy(dates+13, asctime(localtime(&(status.st_ctime))));
- //escreve...
- write(arq, dates, strlen(dates));
- //write(arq, (char*)"\n", 2);
- }
- }
- }
- closedir(dir); // fecha diretório
- } else {
- puts("MEGA ERROR");
- }
- close(arq); // fecha output
- return 0;
- }
Add Comment
Please, Sign In to add comment