0

I wrote a program to find file/folder on linux.

I have a problem, because it only searches in main directory (directory given by user) and doesn't go inside the other folders and further on.

How to fix that guys? So that it would work as intended.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <string.h>
char *kom;
void fileInfo(char *path)
{
    struct tm *time;
    struct stat bufor;
    struct passwd *p;
    struct dirent *wpis;
    stat(path,&bufor);
            time = gmtime(&bufor.st_mtime);
            p = getpwuid(bufor.st_uid);
time = gmtime(&bufor.st_mtime);
                p = getpwuid(bufor.st_uid);
                if(S_ISREG(bufor.st_mode)) printf("-");
                if(S_ISDIR(bufor.st_mode)) printf("d");
                if(S_ISLNK(bufor.st_mode)) printf("l");
                if(S_ISFIFO(bufor.st_mode))printf("p");
                if(bufor.st_mode &S_IRUSR) printf("r");
                else printf("-");
                if(bufor.st_mode &S_IWUSR) printf("w");
                else printf("-");
                if(bufor.st_mode &S_IXUSR) printf("x");
                else printf("-");
                if(bufor.st_mode &S_IRGRP) printf("r");
                else printf("-");
                if(bufor.st_mode &S_IWGRP) printf("w");
                else printf("-");
                if(bufor.st_mode &S_IXGRP) printf("x");
                else printf("-");
                if(bufor.st_mode &S_IROTH) printf("r");
                else printf("-");
                if(bufor.st_mode &S_IWOTH) printf("w");
                else printf("-");
                if(bufor.st_mode &S_IXOTH) printf("x");
                else printf("-");
                printf("\t%10s\t%10ld\t%02i-%02i %02i:%02i\t%s\n",
                       p->pw_name, bufor.st_size ,
                       time->tm_mon+1,time->tm_mday,time->tm_hour,time->tm_min,
                       path);
        }

int main(int argc, char *argv[])
{
    struct tm *time;
    struct stat bufor;
    struct passwd *p;
    char fpath[255];
DIR *dp;
struct dirent *dirp;
if (argc != 3)
{
printf("usage: ./Exe_Name dir_name file_name");
exit(0);
}
if ((dp = opendir(argv[1])) == NULL)
{
printf("can't open %s", argv[1]);
exit(1);
}
fpath[0] = '\0';
strcat(fpath,argv[1]);
strcat(fpath,"/");
strcat(fpath,argv[2]);
printf("^^^^  %s\n",fpath);
while ((dirp = readdir(dp)) != NULL)
{kom=dirp->d_name;
if(!strcmp(dirp->d_name,argv[2])){
printf("%s\n", dirp->d_name);
fileInfo(fpath);
}
}
closedir(dp);
exit(0);
}
Martin Verjans
  • 4,675
  • 1
  • 21
  • 48

0 Answers0