Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <pwd.h>
- #include <sys/types.h>
- #include <time.h>
- #include <dirent.h>
- #include <string.h>
- #include <libgen.h>
- int fileinfo (char *nf, int indent);
- int listdir (char *nf, int indent);
- int fileinfo (char *nf, int indent) {
- struct stat s;
- char fileName[2048];
- int i;
- if (stat (nf, &s) == -1)
- return -1;
- if (s.st_mode & S_IFDIR && strcmp(".", basename(nf)) != 0 && strcmp("..", basename(nf)) != 0) {
- for (i = 0; i < indent; i++)
- printf (" ");
- //printf("[DIR]%s\n %s", basename(nf), nf);
- strcpy(fileName, nf);
- strcat(fileName, "/");
- listdir(fileName, indent + 1);
- }
- if(s.st_mode & S_IFREG) {
- for (i = 0; i < indent; i++)
- printf (" ");
- printf ("%s\n", basename(nf));
- }
- return 0;
- }
- int listdir (char *nf, int indent) {
- DIR *pd;
- struct dirent *pde;
- char cale[256], specificator[256];
- if ((pd = opendir (nf)) == NULL)
- return -1;
- printf("[DIR]%s\n", basename(nf));
- strcpy (cale, nf);
- while ((pde = readdir (pd)) != NULL) {
- strcpy (specificator, cale);
- strcat (specificator, pde->d_name);
- if (fileinfo (specificator, indent +1 ) == -1)
- perror (specificator);
- }
- closedir (pd);
- return 0;
- }
- int main (int argc, char **argv) {
- if (argc == 2) {
- if (listdir (argv[1], 0) == -1)
- perror (argv[1]);
- }
- else perror ("Not enough args!");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment