帮忙看一下这个程序。运行时总显示“段错误”
程序作用:在 Linux 下,编写 Shell 程序,计算磁盘上所有目录下平均文件个数、所有目录平均cZ
sx%@8Uw
深度、所有文件名平均长度7s|fCr8^|H
源程序:
Yq2?w)gYDs#include <sys/stat.h>8Qw
v7Hx%V$o
#include <sys/types.h>*y0Z#o#Ni{]-D
#include <string.h>
0|(f6o}[Xw-b#include <stdio.h>
&Ku-R@(u S_~#include <stdlib.h>
W:rIr)mbZLinux伊甸园论坛#include <dirent.h>-}u&B#Qw9A
int func(char*path,int* AllFileNumber,int* AllDepthNumber,
6b4f@Z^"Vev int* AllFilenameNumber,int* AllFoldNumber)Linux伊甸园论坛l.T8J$O:W
Nl#F
{
)t)X.PU!W y struct stat statbuf;www.linuxeden.com(_+}5@Is
struct dirent *dirp;Linux伊甸园论坛%y]"C["A6`
DIR *dp;9}eNnj$@D
char *str1;www.linuxeden.como%T
{8@v%Q:l
z
char *str2;
a \5jnc:zNwww.linuxeden.com int judge1;L1rMn9IGVu
]f
int judge2;
%CTP7t:{gi5f int ret;
R_.Y9SL @$cwww.linuxeden.com judge1=0;
(U9F8Q(G%Sfwww.linuxeden.com judge2=0;W ~*Z*eJrA
!S.i?.xR3jLinux伊甸园论坛 stat(path,&statbuf);
I6F C Rb!T
l1Z
)N&rNo4bU!uswitch(statbuf.st_mode&S_IFMT)
pTV7Yl;}O{
%P*^(h;{5J.KD"[Linux伊甸园论坛case S_IFREG:
|7p[yh[Linux伊甸园论坛 *AllFilenameNumber=strlen(path)+1;www.linuxeden.com,Tk%E;xAS#X*i(@-[
*AllFileNumber=*AllFileNumber+1;E6Mr/hulUr+iK
*AllDepthNumber=*AllDepthNumber+1;
i3LC:|
??G1qC break;
!az
]n8@%Gq)^3iLinux伊甸园论坛case S_IFDIR: *AllDepthNumber=*AllDepthNumber+1;www.linuxeden.com-^.ts.} We^ m4S9@
*AllFoldNumber=*AllFoldNumber+1;www.linuxeden.com$avDbg$j#s~
dp=opendir(path);
p Pe'XuY while((dirp=readdir(dp))!=NULL)
6PX;[4U$X do]2u {
~$n:q;i?:w if((strcmp(dirp->d_name,".")==0)||(strcmp(dirp->d_name,"..")==0))Linux伊甸园论坛g`"h2SMM)F)gO
:vRl kmz*r'~:^4u if(judge1==0){
F!WOsf$T4dLinux伊甸园论坛
oD m*IW){ strcpy(str1,path);
!c,bjV6|Pa!b\bLinux伊甸园论坛 judge1=1;}
w_~!{acJ MLinux伊甸园论坛 if (judge2==0)www.linuxeden.comBZl0a
XC~
{www.linuxeden.com
VL7S7eppR2\'v
str2=str1+strlen(str1);www.linuxeden.com"V8`E;k`:Z{*S.y
judge2=1;
Ji3]"bh/hDLinux伊甸园论坛 }Linux伊甸园论坛s-B._,FH%}
0kqd
a7J{
g"R *str2++='/';Linux伊甸园论坛[#}7O@Y6]oID+o
*str2=0;
8M`N6I{T:P strcpy(str2,dirp->d_name); www.linuxeden.comTzsMhN3g"m;id
ret=func(str1, AllFileNumber,AllDepthNumber,
L4dL oo
?yi AllFilenameNumber,AllFoldNumber);www.linuxeden.com%Od~K6}~_%^%Q
*str2--;Linux伊甸园论坛-EK F1bO1i&d
*str2=0;Wp%}zj
qs
];^(Ae
}Linux伊甸园论坛;?+f/[-gwp$~]n
closedir(dp);
,tON&Z\^#B break;
rTQiv|&jE8t&xdefault: break;p)q3u)V'S&E PL
}f;y7mKB^F:`
return 0;
H5kh$v:[#sq5VK9g} |