0

我需要一个应该计算文件的程序,找到其中最大的文件以及目录和子目录中文件的总和(分别用于子目录),这是一个多么大的问题:我的程序总是尝试通过链接,但我不需要那个!我尝试查找链接和套接字文件,但有些东西不起作用

#include <dirent.h>     
#include <errno.h>      
#include <fcntl.h>      
#include <stdio.h>      
#include <stdlib.h>     
#include <string.h>     
#include <sys/stat.h>   
#include <sys/types.h>  
#include <sys/fcntl.h>  
#include <sys/wait.h>   
#include <time.h>       
#include <unistd.h>     
#include <malloc.h>

struct stat st;

struct dirent * d;  
off_t sz[10000];    
FILE *fp;   
char PROG_NAME[100];
char FILE_NAME[100];

char * nm[10000],* maxn;

int rec1(char * rp);

int main (int argc, char *argv[])
{
/*//it's for terminal
    //how much arguments
    if (argc < 3)
    {
    printf("Too few arguments\n");
        return 1;
    };
    if (argc> 3)
    {
        printf("Too many arguments\n");
        return 1;
    };
    FILE * fp; //filepath 
   if ((fp=fopen(argv[2],"w"))==NULL)
   {
      fprintf(stderr,"%s fopen: %s %s\n",argv[0],argv[2],strerror(errno));
      return 1;
   };
   if (fclose(fp)==-1)
   {
      fprintf(stderr,"%s fclose: %s %s\n",argv[0],argv[2],strerror(errno));
      return 1;
   };
   strcpy(PROG_NAME,argv[0]);
   strcpy(FILE_NAME,argv[2]);*/
   strcpy(PROG_NAME,"Prog1");
   strcpy(FILE_NAME,"out.txt");
   char a[100]="/dev/"; //directory where we start  
   if ((fp=fopen(FILE_NAME,"w"))==NULL)     
   {
        fprintf(stderr,"%s fopen: %s %s\n",PROG_NAME,FILE_NAME,strerror(errno));
        return 1;
   };
   rec1(a); 
   if (fclose(fp)==-1)  
   {
        fprintf(stderr,"%s fclose: %s %s\n",PROG_NAME,FILE_NAME,strerror(errno));
        return 1;
   };
   exit(EXIT_SUCCESS);
}

int rec1(char * rp) 
{
   int i=0; 
   off_t sb=0,ts=0;//
   char nb[1000], s[1000];
   DIR * dp;//dirpath
   strcpy(nb,"");//занулим nb
   if ((dp=opendir(rp)) == NULL)
   {
        fprintf(stderr,"%s opendir: %s %s \n",PROG_NAME,rp,strerror(errno));
        return 1;
   };
   while ((d=readdir(dp))!=NULL)
   {
        if (strcmp(d->d_name,".")!= 0 && strcmp(d->d_name,"..")!= 0)
        {
            strcpy(s,realpath(rp,NULL));
            if (s[strlen(s)-1] != '/')
            {
                strcat(s,"/");
            };
            strcat(s,d->d_name);
            stat(s,&st);//вызывает stat
            if (stat(s,&st) != 0)
            {
                printf(" %s :error stat file: %s \n",PROG_NAME, s);
            }
            else
            {
                if (!S_ISDIR(st.st_mode))
                {
                    if (st.st_size>sb)
                    {
                        strcpy(nb,d->d_name);
                        sb=st.st_size;
                    };
                    i++;
                    ts+=st.st_size;
                }
                else//!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                {
                    if (S_ISDIR(st.st_mode)&&(!(S_ISLNK(st.st_mode)))&&(!S_ISSOCK(st.st_mode))
                     &&(!S_ISFIFO(st.st_mode))&&(!S_ISCHR(st.st_mode))&&(!S_ISBLK(st.st_mode)))
                    {
                        rec1(s);
                    };
                };
            };        
        };
   };
   printf("%s %ld %ld %s \n",rp,i,ts,nb);
   fprintf(fp,"%s %ld %ld %s \n",rp,i,ts,nb);
   if (closedir(dp)==-1)
   {
       printf("%s closedir: %s %s \n",PROG_NAME,rp,strerror(errno));
       return 1;
   };
}

` 当脚本找到预目录的链接文件时,它会循环。试图避免链接转换但失败了。尝试定义 /dev/fd/4.../dev/fd/23 文件但总是错误“没有这样的文件或目录”

#include <sys/types.h>
       #include <sys/stat.h>
       #include <time.h>
       #include <stdio.h>
       #include <stdlib.h>

       int
       main(int argc, char *argv[])
       {
           struct stat sb;

           if (argc != 2) {
               fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
               exit(EXIT_FAILURE);
           }

           if (stat(argv[1], &sb) == -1) {
               perror("stat");
               exit(EXIT_FAILURE);
           }

           printf("File type:                ");

           switch (sb.st_mode & S_IFMT) {
           case S_IFBLK:  printf("block device\n");            break;
           case S_IFCHR:  printf("character device\n");        break;
           case S_IFDIR:  printf("directory\n");               break;
           case S_IFIFO:  printf("FIFO/pipe\n");               break;
           case S_IFLNK:  printf("symlink\n");                 break;
           case S_IFREG:  printf("regular file\n");            break;
           case S_IFSOCK: printf("socket\n");                  break;
           default:       printf("unknown?\n");                break;
           }

           printf("I-node number:            %ld\n", (long) sb.st_ino);

           printf("Mode:                     %lo (octal)\n",
                   (unsigned long) sb.st_mode);

           printf("Link count:               %ld\n", (long) sb.st_nlink);
           printf("Ownership:                UID=%ld   GID=%ld\n",
                   (long) sb.st_uid, (long) sb.st_gid);

           printf("Preferred I/O block size: %ld bytes\n",
                   (long) sb.st_blksize);
           printf("File size:                %lld bytes\n",
                   (long long) sb.st_size);
           printf("Blocks allocated:         %lld\n",
                   (long long) sb.st_blocks);

           printf("Last status change:       %s", ctime(&sb.st_ctime));
           printf("Last file access:         %s", ctime(&sb.st_atime));
           printf("Last file modification:   %s", ctime(&sb.st_mtime));

           exit(EXIT_SUCCESS);
       }

请帮我

4

2 回答 2

4

stat()在应该使用的时候使用lstat()。从lstat()手册页

lstat() 与 stat() 相同,除了如果 path 是符号链接,则链接本身是 stat-ed,而不是它所引用的文件。

于 2011-04-12T21:54:23.563 回答
1

还有我的 0.02 美元:不要重新发明轮子

$ time sudo du --exclude /sys -xslLc /*
3           /bbs
10028       /bin
166960      /boot
0           /bulk
288         /dev
246068      /etc
100521273   /home
4           /home2
11588       /initrd.img
11612       /initrd.img.old
1789892     /lib
16          /lost+found
12          /media
12          /mnt
3           /net
747204      /opt
7245712     /proc
1035364     /root
38312       /sbin
4           /selinux
4           /srv
4           /tanq
37432       /tmp
14636048    /usr
1476612     /var
4328        /vmlinuz
4196        /vmlinuz.old
127982979   total

real    0m53.157s
user    0m20.773s
sys 0m21.137s
于 2011-04-12T22:29:14.463 回答