Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include <dirent.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <grp.h>
- #include <pwd.h>
- #include <unistd.h>
- int comp (const void *i, const void *j) {
- return strcmp(*(const char**)i, *(const char**)j);
- }
- void fill(char *array, struct stat fStat){
- if(S_ISDIR(fStat.st_mode))
- array[0] = 'd';
- else if(S_ISBLK(fStat.st_mode))
- array[0] = 'b';
- else if(S_ISLNK(fStat.st_mode))
- array[0] = 'l';
- else if(S_ISCHR(fStat.st_mode))
- array[0] = 'c';
- else if(S_ISFIFO(fStat.st_mode))
- array[0] = 'p';
- else{
- array[0] = '-';
- }
- if (fStat.st_mode & S_IRUSR)
- array[1] = 'r';
- if (fStat.st_mode & S_IWUSR)
- array[2] = 'w';
- if (fStat.st_mode & S_IXUSR)
- array[3] = 'x';
- if (fStat.st_mode & S_IWGRP)
- array[4] = 'w';
- if (fStat.st_mode & S_IRGRP)
- array[5] = 'r';
- if (fStat.st_mode & S_IXGRP)
- array[6] = 'x';
- if (fStat.st_mode & S_IROTH)
- array[7] = 'r';
- if (fStat.st_mode & S_IWOTH)
- array[8] = 'w';
- if(fStat.st_mode & S_IXOTH)
- array[9] = 'x';
- }
- int countF(DIR *dir){
- int count = 0;
- while((readdir(dir)) != NULL){
- ++count;
- }
- rewinddir(dir);
- return count;
- }
- void recursion(char *directoryName){
- struct stat f;
- if(!S_ISDIR(f.st_mode)){
- struct passwd *p = getpwuid(f.st_uid);
- struct group *g = getgrgid(f.st_gid);
- char *array = (char *) malloc(12);
- int l = 0;
- for (l = 0; l < 9; ++l) {
- array[l] = '-';
- }
- fill(array, f);
- printf("%s %d ", array, f.st_nlink);
- if (p != NULL && g != NULL) {
- printf("%s %s %d %s", p->pw_name, g);
- } else if (p == NULL && g != NULL) {
- printf("%d %s %d %s", f.st_uid, g);
- } else if (p != NULL && g == NULL) {
- printf("%s %d %d %s", p->pw_name, f.st_gid);
- } else {
- printf("%d %d %d %s", f.st_uid, f.st_gid);
- }
- printf("%d %s", f.st_size, directoryName);
- if (S_ISLNK(f.st_mode)) {
- char link[1000000000];
- if (readlink(directoryName, link, 1000000000) == -1) {
- exit(1);
- } else {
- printf(" -> %s", link);
- }
- }
- printf("\n");
- }
- else {
- DIR *dir = opendir(directoryName);
- if (dir == NULL) {
- perror("Error");
- closedir(dir);
- return;
- }
- int countOfFiles = countF(dir);
- if (directoryName[strlen(directoryName) - 1] != '/') {
- directoryName = (char *) realloc(directoryName, strlen(directoryName) + 2);
- strcat(directoryName, "/");
- }
- char *arrayOfFiles[countOfFiles];
- char **arrayOfDirectories = (char **) malloc(1000000000);
- int j, k;
- for (j = 0; j < countOfFiles; ++j) {
- arrayOfFiles[j] = (char *) malloc(1000000000);
- if (arrayOfFiles[j] == NULL) {
- perror("");
- free(arrayOfFiles[j]);
- exit(0);
- }
- for (k = 0; k < strlen(readdir(dir)->d_name); ++k) {
- arrayOfFiles[j][k] = readdir(dir)->d_name[k];
- }
- }
- qsort(arrayOfFiles, countOfFiles, sizeof(char *), comp);
- int countOfDirectories = 0;
- for (k = 0; k < countOfFiles; ++k) {
- if (!(strlen(arrayOfFiles[k]) == 1 && arrayOfFiles[k][0] == '.') &&
- !(strlen(arrayOfFiles[k]) == 2 && arrayOfFiles[k][0] == '.' && arrayOfFiles[k][1] == '.')) {
- struct stat Stat;
- if (lstat(directoryName, &Stat) != 0) {
- perror("");
- exit(1);
- }
- if (S_ISDIR(Stat.st_mode)) {
- arrayOfDirectories[countOfDirectories] = directoryName;
- countOfDirectories++;
- }
- struct passwd *p = getpwuid(Stat.st_uid);
- struct group *g = getgrgid(Stat.st_gid);
- char *array = (char *) malloc(12);
- int l = 0;
- for (l = 0; l < 9; ++l) {
- array[l] = '-';
- }
- fill(array, Stat);
- printf("%s %d ", array, Stat.st_nlink);
- if (p != NULL && g != NULL) {
- printf("%s %s %d %s", p->pw_name, g);
- } else if (p == NULL && g != NULL) {
- printf("%d %s %d %s", Stat.st_uid, g);
- } else if (p != NULL && g == NULL) {
- printf("%s %d %d %s", p->pw_name, Stat.st_gid);
- } else {
- printf("%d %d %d %s", Stat.st_uid, Stat.st_gid);
- }
- printf("%d %s", Stat.st_size, arrayOfFiles[k]);
- if (S_ISLNK(Stat.st_mode)) {
- char link[1000000000];
- if (readlink(directoryName, link, 1000000000) == -1) {
- exit(1);
- } else {
- printf(" -> %s", link);
- }
- }
- printf("\n");
- }
- }
- printf("\n");
- for (k = 0; k < countOfDirectories; ++k) {
- recursion(arrayOfDirectories[k]);
- }
- for (j = 0; j < countOfFiles; ++j) {
- free(arrayOfFiles[j]);
- }
- for (j = 0; j < countOfDirectories; ++k) {
- free(arrayOfDirectories);
- }
- free(arrayOfFiles);
- free(directoryName);
- free(arrayOfDirectories);
- closedir(dir);
- }
- }
- int main(int argc, char **argv) {
- recursion(argv[1]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement