Reputation: 1
I need a program that should count files,find the biggest of them and total summ of files in directory and subdirectories(for subdirectory separately ) and what a problem: my programm always try go by links,but i don't need that! i try to find link and socket files, but something don't work
#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;
};
}
` When script finds link-file of pre-directory it cycles. Tried to avoid links transitions but failed. try to define /dev/fd/4.../dev/fd/23 files but always error "no such files or directory"
#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);
}
please help me
Upvotes: 0
Views: 595
Reputation: 393114
And my $0.02: don't reinvent wheels
$ 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
Upvotes: 1
Reputation: 86353
You are using stat()
when you should be using lstat()
. From the lstat()
manual page:
lstat() is identical to stat(), except that if path is a symbolic link, then the link itself is stat-ed, not the file that it refers to.
Upvotes: 4