分类: LINUX
2007-09-22 14:07:09
void print_usage(void);
void print_usage(void)
{
printf("Usage: test dirname\n");
}
int main(int argc,char *argv[])
{
DIR * dp;
struct dirent *filename;
if (argc < 2)
{
print_usage();
exit(1);
}
dp = opendir(argv[1]);
if (!dp)
{
fprintf(stderr,"open directory error\n");
return 0;
}
while (filename=readdir(dp))
{
printf("filename:%-10s\td_info:%ld\t d_reclen:%us\n",
filename->d_name,filename->d_ino,filename->d_reclen);
}
closedir(dp);
return 0;
}
将上面代码保存为readdir.c执行下面的命令进行编译测试
[root@phpos ~]# gcc readdir.c
[root@phpos ~]# ./a.out
Usage: test dirname
[root@phpos ~]# ./a.out /etc/samba/
filename:lmhosts d_info:49296 d_reclen:24s
filename:smb.conf d_info:49825 d_reclen:24s
filename:smbpasswd d_info:49402 d_reclen:24s
filename:.. d_info:47906 d_reclen:16s
filename:secrets.tdb d_info:49812 d_reclen:24s
filename:. d_info:49295 d_reclen:16s
filename:smbusers d_info:49377 d_reclen:24s
[root@phpos ~]# ./a.out /var/
filename:preserve d_info:31971 d_reclen:24s
filename:lock d_info:31966 d_reclen:16s
filename:yp d_info:31976 d_reclen:16s
filename:empty d_info:31962 d_reclen:24s
filename:run d_info:31972 d_reclen:16s
filename:lib d_info:31939 d_reclen:16s
filename:nis d_info:31969 d_reclen:16s
filename:local d_info:31965 d_reclen:24s
filename:spool d_info:31973 d_reclen:24s
filename:tmp d_info:31941 d_reclen:16s
filename:opt d_info:31970 d_reclen:16s
filename:log d_info:31959 d_reclen:16s
filename:crash d_info:31997 d_reclen:24s
filename:.. d_info:2 d_reclen:16s
filename:account d_info:32037 d_reclen:24s
filename:mail d_info:31968 d_reclen:16s
filename:cache d_info:31960 d_reclen:24s
filename:db d_info:31961 d_reclen:16s
filename:. d_info:31938 d_reclen:16s
[root@phpos ~]#
程序正常执行!到此完了呵呵!!