Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>
- #include <dirent.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <time.h>
- char* getType(int fileType) {
- char* type;
- switch (fileType) {
- case DT_REG:
- type = "file";
- break;
- case DT_DIR:
- type = "dir";
- break;
- case DT_FIFO:
- type = "fifo";
- break;
- case DT_SOCK:
- type = "sock";
- break;
- case DT_CHR:
- type = "char dev";
- break;
- case DT_BLK:
- type = "block dev";
- break;
- case DT_LNK:
- type = "slink";
- break;
- default:
- type ="error";
- break;
- }
- return type;
- }
- void searchDir(char* dirName) {
- DIR* currentDirectory = opendir(dirName);
- if(currentDirectory == NULL) {
- printf("Nie ma takiego katalogu\n");
- } else {
- struct dirent* thisDir;
- while((thisDir = readdir(currentDirectory)) != NULL) {
- if(strcmp(thisDir->d_name, ".") && strcmp(thisDir->d_name, "..")) {
- char* newPath = calloc(1024, sizeof(char));
- struct stat stats;
- strcat(newPath, dirName);
- strcat(newPath, "/");
- strcat(newPath, thisDir->d_name);
- lstat(newPath, &stats);
- char* type = getType(thisDir->d_type);
- char* path = realpath(newPath, NULL);
- printf("%s:\n Pełna ścieżka: %s\n Rodzaj pliku: %s\n Rozmiar pliku: %ld B\n Data ostatniego dostępu: %ld\n Data ostatnie modyfikacji: %ld\n", thisDir->d_name, path, type, stats.st_size, stats.st_atime, stats.st_mtime);
- if(thisDir->d_type == DT_DIR && thisDir->d_type != DT_LNK) {
- searchDir(newPath);
- }
- free(newPath);
- }
- }
- }
- }
- int main(int argc, char* argv[]) {
- char* startDir = argv[1];
- char* equality = argv[2];
- char* date = argv[3];
- if(argc < 4) {
- printf("Podano złą liczbę argumentów\n");
- } else if(strcmp(equality, "=") && strcmp(equality, ">") && strcmp(equality, "<")) {
- printf("Wprowadzono nieprawidłowy znak porówniania\n");
- } else {
- searchDir(startDir);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement