gpt4 book ai didi

c - 文件 link(device,socket)/dev/fd/need avoid links transition 的 Linux C 问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:54:10 25 4
gpt4 key购买 nike

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

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

请帮帮我

最佳答案

当您应该使用 lstat() 时,您却在使用 stat()。来自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.

关于c - 文件 link(device,socket)/dev/fd/need avoid links transition 的 Linux C 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5641882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com