Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<fcntl.h>
- #include<pwd.h>
- #include<grp.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<sys/stat.h>
- #include<sys/types.h>
- #include<dirent.h>
- #include<libgen.h>
- #include<string.h>
- #include<errno.h>
- void printAttributes(struct stat st)
- {
- char atr[10] = "----------";
- if (S_ISLNK(st.st_mode)) atr[0] = 'l';
- if (S_ISREG(st.st_mode)) atr[0] = '-';
- if (S_ISDIR(st.st_mode)) atr[0] = 'd';
- if (S_ISBLK(st.st_mode)) atr[0] = 'b';
- if (S_ISCHR(st.st_mode)) atr[0] = 'c';
- if (S_ISFIFO(st.st_mode)) atr[0] = 'p';
- if (S_ISSOCK(st.st_mode)) atr[0] = 's';
- if (S_IRUSR & st.st_mode) atr[1] = 'r';
- if (S_IWUSR & st.st_mode) atr[2] = 'w';
- if (S_IXUSR & st.st_mode) atr[3] = 'x';
- if (S_IRGRP & st.st_mode) atr[4] = 'r';
- if (S_IWGRP & st.st_mode) atr[5] = 'w';
- if (S_IXGRP & st.st_mode) atr[6] = 'x';
- if (S_IROTH & st.st_mode) atr[7] = 'r';
- if (S_IWOTH & st.st_mode) atr[8] = 'w';
- if (S_IXOTH & st.st_mode) atr[9] = 'x';
- printf("%s ", atr);
- }
- void printUsr(const struct stat* st)
- {
- struct passwd* pw = getpwuid(st->st_uid);
- if (pw == NULL) printf("%d ", st->st_uid);
- else printf("%s ", pw->pw_name);
- }
- void printGroup(const struct stat* st)
- {
- struct group* gr = getgrgid(st->st_gid);
- if (gr == NULL) printf("%d ", st->st_uid);
- else printf("%s ", gr->gr_name);
- }
- void printStat(struct stat* st, char* filename)
- {
- if (strcmp(basename(filename), ".") == 0 || strcmp(basename(filename), "..") == 0)
- {
- return;
- }
- if (!S_ISLNK(st->st_mode))
- {
- printAttributes(*st);
- printf("%ld ", st->st_nlink);
- printGroup(st);
- printGroup(st);
- printf("%ld ", st->st_size);
- printf("%s\n", basename(filename));
- }
- if (S_ISLNK(st->st_mode))
- {
- printAttributes(*st);
- printf("%ld ", st->st_nlink);
- printGroup(st);
- printGroup(st);
- printf("%ld ", st->st_size);
- char tmp[PATH_MAX] = "";
- ssize_t r = readlink(filename, tmp, sizeof(tmp));
- if (r == -1)
- {
- fprintf(stderr, "%s\n", strerror(errno));
- exit(1);
- }
- tmp[r] = '\0';
- printf("%s -> ", basename(filename));
- printf("%s\n", tmp);
- }
- }
- int main(int argc, char* argv[])
- {
- struct stat st;
- if(lstat(argv[1], &st) == -1)
- {
- fprintf(stderr, "%s\n", strerror(errno));
- exit(1);
- }
- if (!S_ISDIR(st.st_mode))
- {
- if (!S_ISLNK(st.st_mode))
- {
- printAttributes(st);
- printf("%ld ", st.st_nlink);
- printGroup(&st);
- printGroup(&st);
- printf("%ld ", st.st_size);
- printf("%s\n", argv[1]);
- }
- if (S_ISLNK(st.st_mode))
- {
- printAttributes(st);
- printf("%ld ", st.st_nlink);
- printGroup(&st);
- printGroup(&st);
- printf("%ld ", st.st_size);
- char tmp[PATH_MAX] = "";
- ssize_t r = readlink(argv[1], tmp, sizeof(tmp));
- if (r == -1)
- {
- fprintf(stderr, "%s\n", strerror(errno));
- exit(1);
- }
- tmp[r] = '\0';
- printf("%s -> ", argv[1]);
- printf("%s\n", tmp);
- }
- }
- else
- {
- int dir_fd = open(argv[1], O_RDONLY | O_DIRECTORY);
- if (dir_fd == -1)
- {
- fprintf(stderr, "%s\n", strerror(errno));
- exit(1);
- }
- DIR* dir = fdopendir(dir_fd);
- if (dir == NULL)
- {
- fprintf(stderr, "%s\n", strerror(errno));
- exit(1);
- }
- struct dirent* file;
- while(file = readdir(dir))
- {
- char path[PATH_MAX] = "";
- strcat(path, argv[1]);
- strcat(path, "/");
- strcat(path, file->d_name);
- if (lstat(path, &st) != -1)
- {
- printStat(&st, path);//file->d_name);
- }
- else
- {
- fprintf(stderr, "%s\n", strerror(errno));
- exit(1);
- }
- }
- closedir(dir);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement