程序在linux上查找文件/文件夹

时间:2016-12-08 10:07:36

标签: c linux search

我写了一个程序来查找linux上的文件/文件夹。

我遇到了问题,因为它只搜索主目录(用户提供的目录),并且不会进入其他文件夹并进一步开启。

如何解决这些家伙?这样它就能按预期工作。

#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);
}

0 个答案:

没有答案