- 论坛徽章:
- 0
|
在看《unix环境高级编程(第二版)》中的程序4-7递归降序遍历目录层次结构时,里面有个dopath()递归函数,函数内容如下所示:
static int dopath(Myfunc * func)
{
struct stat statbuf;
struct dirent *dirp;
DIR *dp;
int ret;
char *ptr;
if(lstat(fullpath,&statbuf)<0)
return(func(fullpath,&statbuf,FTW_NS));
if(S_ISDIR(statbuf.st_mode)==0) /*not a directory*/
return (func(fullpath,&statbuf,FTW_F));
if((ret=func(fullpath,&statbuf,FTW_D))!=0)
return(ret);
ptr=fullpath+strlen(fullpath);
*ptr++='/';
*ptr=0;
if((dp=opendir(fullpath))==NULL)
return(func(fullpath,&statbuf,FTW_DNR));
while((dirp=readdir(dp))!=NULL)
{
if(strcmp(dirp->d_name,".")==0||strcmp(dirp->d_name,"..")==0)
continue;
strcpy(ptr,dirp->d_name);
if((ret=dopath(func))!=0)
break;
}
ptr[-1]=0;
if(closedir(dp)<0)
err_ret("can't close directory %s",fullpath);
return (ret);
}
看不懂ptr[-1]=0这句语句是什么意思,数组的索引可以用负数吗?不是从0开始吗,还是这根本不是数组??
请高手指教,谢谢! |
|