问题的提出来自<>里 readdir() 的例子程序,稍微修改过,如下:
#include
#include
#include
#include
int main (void)
{
DIR *dir = NULL;
struct dirent *ptr = NULL;
dir = opendir ( "/etc/rc.d" );
printf ( "dir: %s\n", dir );
while( (ptr = readdir(dir)) != NULL )
printf ( "d_ino: %d, d_off: %-11d, d_reclen: %d, d_type: %d, d_name: %s\n", ptr->d_ino, ptr->d_off, ptr->d_reclen, ptr->d_type, ptr->d_name );
closedir(dir);
return 0;
}
编译时报警:
$ make readdirr
gcc -Wall -g readdirr.c -o readdirr
readdirr.c: 在函数 ‘main’ 中:
readdirr.c:15: 警告:格式 ‘%s’ 需要类型 ‘char *’,但实参 2 的类型为 ‘struct DIR *’
readdirr.c:17: 警告:格式 ‘%d’ 需要类型 ‘int’,但实参 2 的类型为 ‘__ino_t’
readdirr.c:17: 警告:格式 ‘%-11d’ 需要类型 ‘int’,但实参 3 的类型为 ‘__off_t’
$
无视,^_^ 继续运行(可以通过强制类型转换弄掉这些报警,:))
$ ./readdirr
dir:
d_ino: 2025731, d_off: 186033110 , d_reclen: 20, d_type: 4, d_name: rc6.d
d_ino: 2026608, d_off: 266940224 , d_reclen: 20, d_type: 8, d_name: rc.local
d_ino: 2025728, d_off: 339221820 , d_reclen: 20, d_type: 4, d_name: rc3.d
d_ino: 2025665, d_off: 397264913 , d_reclen: 16, d_type: 4, d_name: ..
d_ino: 2025721, d_off: 621510449 , d_reclen: 16, d_type: 4, d_name: .
d_ino: 2025725, d_off: 850833563 , d_reclen: 20, d_type: 4, d_name: rc0.d
d_ino: 2025726, d_off: 1342567813 , d_reclen: 20, d_type: 4, d_name: rc1.d
d_ino: 2025729, d_off: 1458401961 , d_reclen: 20, d_type: 4, d_name: rc4.d
d_ino: 2025730, d_off: 1587336036 , d_reclen: 20, d_type: 4, d_name: rc5.d
d_ino: 2026607, d_off: 1659291183 , d_reclen: 16, d_type: 8, d_name: rc
d_ino: 2026609, d_off: 1684534258 , d_reclen: 24, d_type: 8, d_name: rc.sysinit
d_ino: 2025727, d_off: 1703281730 , d_reclen: 20, d_type: 4, d_name: rc2.d
d_ino: 2025724, d_off: 2147483647 , d_reclen: 20, d_type: 4, d_name: init.d
$
看一下 /etc/rc.d/ 目录下的东东
$ ls -il /etc/rc.d
总计 112
2025724 drwxr-xr-x 2 root root 4096 2009-02-17 init.d
2026607 -rwxr-xr-x 1 root root 2255 2006-09-22 rc
2025725 drwxr-xr-x 2 root root 4096 03-12 09:49 rc0.d
2025726 drwxr-xr-x 2 root root 4096 03-12 09:49 rc1.d
2025727 drwxr-xr-x 2 root root 4096 03-12 09:49 rc2.d
2025728 drwxr-xr-x 2 root root 4096 03-12 09:49 rc3.d
2025729 drwxr-xr-x 2 root root 4096 03-12 09:49 rc4.d
2025730 drwxr-xr-x 2 root root 4096 03-12 09:49 rc5.d
2025731 drwxr-xr-x 2 root root 4096 03-12 09:49 rc6.d
2026608 -rwxr-xr-x 1 root root 220 2003-06-24 rc.local
2026609 -rwxr-xr-x 1 root root 26958 2007-05-08 rc.sysinit
$
有一点,看得出来,readdir() 取的目录下的东东是根据 d_off 从小到大排序的
问题是,d_off 是“offset to the next dirent”,<>翻译成“目录文件开头至此目录进入点的位移”,不清楚这里的基点是什么?
暂时没功夫考虑这个问题,记下来,以后有功夫再细想,要是有人看到了,也欢迎分享你的想法,^_^
阅读(3198) | 评论(0) | 转发(0) |