int main(int argc, char *argv[])
{
	int ret;
	if(argc != 2)
	{
		printf("usage:ftw <starting_pathname>");
		exit(0);
	}

	ret = myftw(argv[1], myfunc);
	ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock;
	
	if(ntot == 0)
		ntot = 1;

	printf("regular files = %7ld, %5.2f %%\n", nreg, nreg*100.0/ntot);
	
	printf("directories = %7ld, %5.2f %%\n", ndir, ndir*100.0/ntot);

	printf("block special = %7ld, %5.2f %%\n", nblk, nblk*100.0/ntot);

	printf("char special = %7ld, %5.2f %%\n", nchr, nchr*100.0/ntot);

	printf("FIFOs = %7ld, %5.2f %%\n", nfifo, nfifo*100.0/ntot);

	printf("symbolic link = %7ld, %5.2f %%\n", nslink, nslink*100.0/ntot);

	printf("sockets = %7ld, %5.2f %%\n", nsock, nsock*100.0/ntot);

	exit(ret);
}
Exemple #2
0
int
main(int argc, char *argv[])
{
	int		ret;

	if (argc != 2)
		printf("usage:  ftw  <starting-pathname>");

	ret = myftw(argv[1], myfunc);		/* does it all */

	ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock;
	if (ntot == 0)
		ntot = 1;		/* avoid divide by 0; print 0 for all counts */
	printf("regular files  = %7ld, %5.2f %%\n", nreg,
	  nreg*100.0/ntot);
	printf("directories    = %7ld, %5.2f %%\n", ndir,
	  ndir*100.0/ntot);
	printf("block special  = %7ld, %5.2f %%\n", nblk,
	  nblk*100.0/ntot);
	printf("char special   = %7ld, %5.2f %%\n", nchr,
	  nchr*100.0/ntot);
	printf("FIFOs          = %7ld, %5.2f %%\n", nfifo,
	  nfifo*100.0/ntot);
	printf("symbolic links = %7ld, %5.2f %%\n", nslink,
	  nslink*100.0/ntot);
	printf("sockets        = %7ld, %5.2f %%\n", nsock,
	  nsock*100.0/ntot);

	exit(ret);
}
Exemple #3
0
int main(int argc, char *argv[])
{
	int ret;

	if (argc != 2)
	{
		fprintf(stderr, "usage: ftw starting-pathname\n");
		exit(1);
	}

	ret = myftw(argv[1], myfunc);
	
	ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock;
	// avoid divided by 0
	if (ntot == 0)
		ntot = 1;	

	// DON"T use tab, or it'll be messy
	fprintf(stdout, "regular files = %7ld, %5.2f %%\n", nreg, nreg*100.0/ntot);
	fprintf(stdout, "dirtories     = %7ld, %5.2f %%\n", ndir, ndir*100.0/ntot);
	fprintf(stdout, "block  special= %7ld, %5.2f %%\n", nblk, nblk *100.0/ntot);
	fprintf(stdout, "char   special= %7ld, %5.2f %%\n", nchr, nchr*100.0/ntot);
	fprintf(stdout, "fifo    files = %7ld, %5.2f %%\n", nfifo, nfifo*100.0/ntot);
	fprintf(stdout, "symbolic links= %7ld, %5.2f %%\n", nslink, nslink*100.0/ntot);
	fprintf(stdout, "sockets       = %7ld, %5.2f %%\n", nsock, nsock*100.0/ntot);
	fprintf(stdout, "regular files = %7ld, %5.2f %%\n", nreg, nreg*100.0/ntot);
	
	return 0;
}
Exemple #4
0
int main (int argc, char* argv[])
{
    int     ret;

    if (argc != 2)
	{
		printf("Usage ERROR!");
		return -1;
	}

    ret = myftw(argv[1], myfunc);

    ntot = nreg + nblk + nchr + nfifo + nslink + nsock;
    if (ntot == 0)
        ntot = 1;
    printf("regular files   = %7ld, %5.2f %%\n", nreg, nreg*100.0/ntot);
    printf("directories     = %7ld, %5.2f %%\n", ndir, ndir*100.0/ntot);
    printf("block special   = %7ld, %5.2f %%\n", nblk, nblk*100.0/ntot);
    printf("char special    = %7ld, %5.2f %%\n", nchr, nchr*100.0/ntot);
    printf("FIFOs           = %7ld, %5.2f %%\n", nfifo, nfifo*100.0/ntot);
    printf("symbolic links  = %7ld, %5.2f %%\n", nslink, nslink*100.0/ntot);
    printf("sockets         = %7ld, %5.2f %%\n", nsock, nsock*100.0/ntot);

    return ret;
}
Exemple #5
0
int main (int argc, char **argv)
{
	// parse program parameters
	char *pathname  = NULL;
	char *searchstr = NULL;
	char *filetypes = NULL;
	int index;
	int c;

	struct FlagOpts flags;
	flags.c = 0; flags.h = 0; flags.S = 0; flags.l = 0;

	opterr = 0;
	while ((c = getopt (argc, argv, "p:f:l::s:")) != -1)
	switch (c)
	  {
	  case 'p':
				pathname = optarg;
		break;
			case 'f':
				fileTypesFlag = 1;
				filetypes = optarg;
				if ( mygrep( "c" , 1, filetypes, strlen(filetypes)) )
					flags.c = 1;
				if ( mygrep( "h" , 1, filetypes, strlen(filetypes)) )
					flags.h = 1;
				if ( mygrep( "S" , 1, filetypes, strlen(filetypes)) )
					flags.S = 1;
				break;
			case 'l':
				flags.l = 1;
				break;
	  case 's':
				searchstr = optarg;
				if ( isValidGrepStr(searchstr) == 0 ) {
					my_errprintf("Invalid argument for [-s]: %s\n", searchstr );
					exit(EXIT_FAILURE);
				}
		break;
	  case '?':
		if (optopt == 'f')
		  my_errprintf ("Option -%c requires an argument.\n", optopt);
		else if (isprint (optopt))
		  my_errprintf ("Unknown option `-%c'.\n", optopt);
		else
		  my_errprintf ("Unknown option character `\\x%x'.\n", optopt);
		return 1;
	  default:
		abort ();
	  }

	for (index = optind; index < argc; index++)
	my_printf ("Non-option argument %s\n", argv[index]);

	// Run program with parsed input
	myftw(pathname, searchstr, &flags);

	return 0;
}
Exemple #6
0
int SearchFile(const char *envpath, const char *file, char *filepath)
{
    dirlist=createlist(dirlist);
    char path[PATH_MAX+1]={'\0'};
    strcpy(path,envpath);
    int finished=myftw(path, file,filepath);
    deletelist(dirlist);
    return finished;
}
Exemple #7
0
int main(int argc, char *argv[])
{
    int ret;
    if (argc == 2) { 
        /* 输出各类文件数 */
        ret = myftw(argv[1], myfunc);
        ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock + ntot;
        if (ntot == 0)
            ntot = 1;
        printf("regular files    \t= %7ld, %5.2f %%\n", nreg, nreg * 100.0 / ntot);
        printf("regular files that\n");
        printf("no larger than %d\t= %7ld, %5.2f %%\n", filelen, nreg_small, nreg_small * 100.0 / ntot);
        printf("directories      \t= %7ld, %5.2f %%\n", ndir, ndir * 100.0 / ntot);
        printf("block special    \t= %7ld, %5.2f %%\n", nblk, nblk * 100.0 / ntot);
        printf("char special     \t= %7ld, %5.2f %%\n", nchr, nchr * 100.0 / ntot);
        printf("FIFOs            \t= %7ld, %5.2f %%\n", nfifo, nfifo * 100.0 / ntot);
        printf("symbolic links   \t= %7ld, %5.2f %%\n", nslink, nslink * 100.0 / ntot);
        printf("sockets          \t= %7ld, %5.2f %%\n", nsock, nsock * 100.0 / ntot);
    }
    else if (argc == 4 && (strcmp(argv[2], "-comp") == 0)) {
        /* 输出在<pathname>目录子树之下,
        所有与<filename>文件内容一致的文件的绝对路径名 */
        if (lstat(argv[3], &filestat) < 0) {
            err_quit("stat error");
        }
        filename = argv[3];
        ret = myftw(argv[1], myfunc2);
    }
    else if (argc >= 4 && (strcmp(argv[2], "-name") == 0)) {
        /* 命令输出在<pathname>目录子树之下,
        所有与<str>…序列中文件名相同的文件的绝对路径名。*/
        strnum = argc - 3;
        str = &argv[3];
        ret = myftw(argv[1], myfunc3);   
    }
    else {
        err_quit("usage: myfind  <pathname>  [-comp <filename> | -name <str>...]");
    }
    exit(ret);
}
int main(int argc, char *argv[])
{
	int ret;

	if (argc != 2) {
		printf("usage:  ftw  <starting-pathname>");
		return 0;
	}

	ret = myftw(argv[1], myfunc);
	desktop_list_items(&head);

	exit(ret);
}
Exemple #9
0
int main(int argc,char*argv[])
{
	int ret;
	if(argc!=2) err_quit("Usage error\n");
	ret=myftw(argv[1],myfunc);
	ntot=nreg+ndir+nblk+nchr+nfifo+nslink+nsock;
	if(ntot==0) ntot=1;
	printf("regular files = %7ld, %5.2f %%\n",nreg,nreg*100.0/ntot);
	printf("dir files     = %7ld, %5.2f %%\n",ndir,ndir*100.0/ntot);
	printf("block         = %7ld, %5.2f %%\n",nblk,nblk*100.0/ntot);
	printf("char          = %7ld, %5.2f %%\n",nchr,nchr*100.0/ntot);
	printf("FIFOs         = %7ld, %5.2f %%\n",nfifo,nfifo*100.0/ntot);
	exit(0);
}
Exemple #10
0
int myftw(char *path, const char *file, char *filepath)
{
    struct stat path_stat;
    struct dirent *dirp;
    DIR *dp;
    int n;
    if(stat(path,&path_stat)!=0)
    {
        printf("stat failed.\n");
        return 0;
    }
    if(S_ISDIR(path_stat.st_mode)==0)   //路径是文件,判断文件名是否与目标文件相同
    {
        char name[255]={'\0'}; int i=0;
        for(n=strlen(path)-1;n>=0;n--)
            if(path[n]=='/') break;
        for(n+=1;n<strlen(path);n++) name[i++]=path[n];
        if(!strcmp(name,file)) 
        {
            strcpy(filepath,path);
            return 1;
        }
        return 0;
    }
    else //path正确是一个目录
    {
        if(traverse(dirlist,path_stat.st_ino))   //没访问过这个文件夹,避免成环
            return 0;
        insertdir(dirlist,path_stat.st_ino); //存入inode号
        n=strlen(path);
        path[n++]='/';
        path[n]=0;
        if((dp=opendir(path))==NULL)    //能否打开
            return 0;
        while((dirp=readdir(dp))!=NULL)  //遍历文件夹下的每一个文件或文件夹,调用函数myftw()
        {
            if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..")==0)  //跳过.和..这两个软链接
                continue;
            strcpy(&path[n],dirp->d_name);
            if(myftw(path,file,filepath)==1)
            {
                closedir(dp);
                return 1;
            }
        }
        closedir(dp); //关闭文件夹
    }
    return 0;
}
Exemple #11
0
int main(int argc,char* argv[])
{
	int ret;
	if(argc!=2){
	  printf("usage:walkDir <pathdir>");
	  exit(1);
	}
	ret=myftw(argv[1],myfunc);//callback function:myfunc()
    
	printf("regular files:%ld\n",nreg);
	printf("diractory files:%ld\n",ndir);
	printf("block files:%ld\n",nblk);
	printf("char files:%ld\n",nchr);
	printf("fifo files:%ld\n",nfifo);
	printf("socket files:%ld\n",nsock);
	printf("link files:%ld\n",nlink);
	exit(ret);
}
Exemple #12
0
/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description:  
 * =====================================================================================
 */
int main ( int argc, char *argv[] )
{
    int ret;
    if(argc!=2)
        ERR_EXIT("参数个数不对");

    ret = myftw(argv[1],myfunc);
    ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock;
    if(ntot==0)
        ntot =1;
    printf("regular files = %7ld, %5.2f %%\n",nreg,nreg*100.0/ntot);
    printf("directories   = %7ld, %5.2f %%\n",ndir,ndir*100.0/ntot);
    printf("block special = %7ld, %5.2f %%\n",nblk,nblk*100.0/ntot);
    printf("char special  = %7ld, %5.2f %%\n",nchr,nchr*100.0/ntot);
    printf("fifos         = %7ld, %5.2f %%\n",nfifo,nfifo*100.0/ntot);
    printf("symbolic links = %7ld, %5.2f %%\n",nslink,nslink*100.0/ntot);
    printf("sockets       = %7ld, %5.2f %%\n",nsock,nsock*100.0/ntot);
    exit(ret);
}				/* ----------  end of function main  ---------- */
Exemple #13
0
int main(int argc, char *argv[])
{
    int ret;
    if (argc != 2)
        err_quit("usage: %s <starting-pathname>", argv[0]);
    // do it all
    ret = myftw(argv[1], myfunc);

    ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock;
    // avoid divide by 0
    if (ntot == 0)
        ntot = 1;
    printf("regular files   = %7ld, %5.2f %%\n", nreg, nreg*100.0/ntot);
    printf("directories     = %7ld, %5.2f %%\n", ndir, ndir*100.0/ntot);
    printf("symbolic links  = %7ld, %5.2f %%\n", nslink, nslink*100.0/ntot);
    printf("char special    = %7ld, %5.2f %%\n", nchr, nchr*100.0/ntot);
    printf("block special   = %7ld, %5.2f %%\n", nblk, nblk*100.0/ntot);
    printf("sockets         = %7ld, %5.2f %%\n", nsock, nsock*100.0/ntot);
    printf("FIFOs           = %7ld, %5.2f %%\n", nfifo, nfifo*100.0/ntot);
    return 0;
}
Exemple #14
0
int main (int argc, char **argv)
{
       ar.l = 0;       
       ar.f = 0;       ar.fval = NULL;
       ar.p = 0;       ar.pval = NULL;
       ar.s = 0;       ar.sval = NULL;                  
       int index;
       int c;  
       opterr = 0;
       int filechk = 0;    
       DIR *dp;

       while ((c = getopt (argc, argv, "lp:s:f:")) != -1){
         switch (c)
           {
           case 'l':
             ar.l = 1;       
             break;
           case 'f':
             ar.fval = optarg;
             if(strcmp(ar.fval, "c") != 0 && strcmp(ar.fval, "h") != 0 && strcmp(ar.fval, "S") != 0 ){
             	fprintf (stderr, "Option -%c only accepts 'c', 'h', or 'S' as arguments.\n", optopt);
             	return 1;
             }
             else{
           	  ar.f = (int)ar.fval[0]; //c = 99, S = 83, h = 104
//           	  fprintf (stderr, "fflag is: %d \n", ar.f);        
             }
             break;
           case 'p':
             ar.pval = optarg;
             if ((dp = opendir((char*)ar.pval)) == NULL){
               fprintf (stderr,"%s is not a valid pathname\n", ar.pval);
               return 1;             
             }
             else{          
               ar.p = 1;
               int nlen = strlen(ar.pval);
               fprintf(stderr, "%d... %c\n " ,nlen, ar.pval[nlen-1]);
               if(ar.pval[nlen-1] == '/'){             
                 fprintf(stderr, "deleting... %c\n " ,ar.pval[nlen-1]);
                 ar.pval[nlen-1] = 0;
               }
             }

             break;
           case 's':
             ar.s = 1;
             ar.sval = optarg;           
             break;
           case '?':
             if (optopt == 'f')
               fprintf (stderr, "Option -%c requires an argument.\n", optopt);
             else if (optopt == 'p')
               fprintf (stderr, "Option -%c requires an argument.\n", optopt);
             else if (optopt == 's')
               fprintf (stderr, "Option -%c requires an argument.\n", optopt);
             else if (isprint (optopt))
               fprintf (stderr, "Unknown option `-%c'.\n", optopt);
             else
               fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
             return 1;
           default:
             abort ();
           } //end switch(c)
       }  //end while getopt

       printf ("pflag = %d, fflag = %d, lflag = %d, sflag = %d\n",
               ar.p, ar.f, ar.l, ar.s );    
       printf ("fvalue = %s, pvalue = %s, svalue = %s\n",
               ar.fval, ar.pval, ar.sval );  
                       
       if(ar.s == 0 || ar.p == 0 ){
         fprintf(stderr, "%s\n", "Missing essential -p and -s arguments");
         return 1;
       }    
       
       //Print out bad arguments
       for (index = optind; index < argc; index++){
         printf ("Non-option argument: %s\n", argv[index]);
       }       
       
         //traverse through directory
	 myftw(ar.pval, myfunc);

       return 0;       
       
}
Exemple #15
0
int main(int argc,  char *argv[])
{
    int ret;
    struct stat statbuf;

    if(!(argc == 2 || (argc == 4 && 0 == strcmp(argv[2], "-comp")) || (argc >= 4 && 0 == strcmp(argv[2], "-name"))))
                err_quit("usage: myfind <pathname> [-comp <filename> | -name<str>...]");

    if(lstat(argv[1], &statbuf) < 0)
        err_quit("lstat error: %s\n", argv[1]);

    if(S_ISDIR(statbuf.st_mode) == 0) //equal 0, not a directory, then use current directory
        strcpy(argv[1], ".");
    
    if(argc == 2)
    {
        ret = myftw(argv[1], myfunc);

        ntot = nreg + ndir + nblk + nfifo + nslink + nsock + nchr;
        if(ntot == 0)
            ntot = 1;/* avoid divide by 0; print 0 for all counts*/
        printf("regular files  = %7ld, %5.2f %%\n", nreg, nreg*100.0/ntot);
        printf("less than 4096 = %7ld, %5.2f %%\n", less4096, less4096*100.0/ntot);
        printf("derectories    = %7ld, %5.2f %%\n", ndir, ndir*100.0/ntot);
        printf("block special  = %7ld, %5.2f %%\n", nblk, nblk*100.0/ntot);
        printf("char special   = %7ld, %5.2f %%\n", nchr, nchr*100.0/ntot);
        printf("FIFOs          = %7ld, %5.2f %%\n", nfifo, nfifo*100.0/ntot);
        printf("symbolic links = %7ld, %5.2f %%\n", nslink, nslink*100.0/ntot);
        printf("sockets        = %7ld, %5.2f %%\n", nsock, nsock*100.0/ntot);
    }

    /*找出所有与指定文件有 相同内容 的文件*/
    if(argc == 4 && 0 == strcmp(argv[2], "-comp"))
    {
        /*输入判断,如果文件不符合,则可以直接返回错误,不用open了*/
        if(lstat(argv[3], &statbuf) < 0)
            err_quit("lstat error: %s\n", argv[3]);
        if(!S_ISREG(statbuf.st_mode))
            err_quit("not a regular file: %s\n", argv[3]);

        /*读入文件内容*/
        int fd;
        filesize = statbuf.st_size;

        if((fd = open(argv[3], O_RDONLY, FILE_MODE)) == -1)
            err_sys("can't open the file: %s\n", argv[3]);
        if((filebuf = (char*)malloc(sizeof(char) * filesize)) == NULL ||
               (comparebuf = (char*)malloc(sizeof(char) * filesize)) == NULL)
                err_sys("malloc error\n");
        if(read(fd, filebuf, filesize) != filesize)
            err_sys("read error: %s\n", argv[3]);
        close(fd);

        /*将起始目录转为绝对路径*/
        int len;
        inputpath = path_alloc(&len);
        getRealPath(argv[1], inputpath);
		char* tmp = inputpath + strlen(inputpath);
		*(tmp-1) = 0;

        //printf("inputpath_after_tmp:%s\n")
        //???
        char *ptr, *end;
        const char *split;
        split = getFileName(argv[3]);    /*分离目录和文件名*/
        
        //保存指定文件的绝对目录
        inputfilepath = path_alloc(&len);    //path_alloc返回的类型是char*
        getRealPath(argv[1], inputfilepath);
        
        end = argv[3] + strlen(argv[3]);
        ptr = inputfilepath + strlen(inputfilepath);
        while(split < end)
            *ptr++ = *split++;
        *ptr = 0;

        //开始查找
        printf("\nSame content as '%s':\n", argv[3]);
        ret = myftw(inputpath, myfuncCompareContent);
        if(filefindcount == 0)
            printf("Can't find any match file.\n");
        printf("\n");
    }

    /*找出filename相同的文件*/
    if(argc >= 4 && strcmp(argv[2], "-name") == 0)
    {
        int len, i;
        const char *split;
        inputpath = path_alloc(&len);
        inputfilename = path_alloc(&len);
        getRealPath(argv[1], inputpath);    //inputpath后面有'/'
		char* tmp = inputpath + strlen(inputpath);
		*(tmp-1) = 0;

        printf("\n");
        for(i = 3; i < argc; i++)
        {
            split = getFileName(argv[i]);
            if(split != argv[i])    /*输入的文件名包含了路径*/
                err_quit("only filename, do you include pathname?");

            strncpy(inputfilename, argv[i], len);
            filefindcount = 0;
            printf("%d. Same filename as '%s':\n", i-2, argv[i]);
            ret = myftw(inputpath, myfuncCompareName);
            if(filefindcount == 0)
                printf("Cannot find any match file\n\n");
        }
    }

    exit(ret);
}