AIX:struct dirent d_type
今天做一个小功能,实现起来时需要判断一个目录下各个目录项对应的类型:
可能是普通文件类型;
可能是目录类型;
可能是SOCKET类型…
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
int main()
{
DIR *dir;
struct dirent *item;
dir = opendir("/home/filter");
while ((item = readdir(dir)) != NULL)
{
if (item->d_type & DT_DIR)
{
printf("%s\n", item->d_name);
}
}
closedir(dir);
return 0;
}
这段代码在Linux上编译运行没啥问题,但是在AIX环境下就编译不通过。
Linux下:
[filter@localhost ~]$ uname -a
Linux localhost.localdomain 2.6.18-371.el5 #1 SMP Thu Sep 5 21:21:44 EDT 2013 x86_64 x86_64 x86_64 GNU/Linux
[filter@localhost ~]$ gcc -o main main.c
[filter@localhost ~]$ ./main
..
.
abc
.mozilla
.ssh
AIX下:
ismp@aix68113:/home/ismp>uname -a
AIX aix68113 1 6 00F7CAA94C00
ismp@aix68113:/home/ismp>cc -o main main.c
"main.c", line 16.27: 1506-022 (S) "d_type" is not a member of "struct dirent".
"main.c", line 16.36: 1506-045 (S) Undeclared identifier DT_DIR.
没有编译通过。
查看对应的头文件:
Linux下:
/usr/include/dirent.h
/usr/include/bits/dirent.h
struct dirent
{
#ifndef __USE_FILE_OFFSET64
__ino_t d_ino;
__off_t d_off;
#else
__ino64_t d_ino;
__off64_t d_off;
#endif
unsigned short int d_reclen;
unsigned char d_type;
char d_name[256]; /* We must not include limits.h! */
};
AIX下:
/usr/include/dirent.h
/usr/include/sys/dir.h
#define _D_NAME_MAX 255
struct dirent {
__ulong64_t d_offset; /* real off after this entry */
ino_t d_ino; /* inode number of entry */
ushort_t d_reclen; /* length of this record */
ushort_t d_namlen; /* length of string in d_name */
char d_name[_D_NAME_MAX+1]; /* name must be no longer than this */
/* redefine w/#define when name decided */
};
AIX下man手册:
man dirent.h
ulong_t d_offset; /* actual offset of this entry */
ino_t d_ino; /* inode number of entry */
ushort_t d_reclen; /* length of this entry */
ushort_t d_namlen; /* length of string in d_name */
char d_name[_D_NAME_MAX+1]; /* name of entry (filename) */
可见,在AIX下,struct dirent没有d_type这个字段。
附上Linux下d_type值表:
/usr/include/dirent.h
/* File types for `d_type'. */
enum
{
DT_UNKNOWN = 0,
# define DT_UNKNOWN DT_UNKNOWN
DT_FIFO = 1,
# define DT_FIFO DT_FIFO
DT_CHR = 2,
# define DT_CHR DT_CHR
DT_DIR = 4,
# define DT_DIR DT_DIR
DT_BLK = 6,
# define DT_BLK DT_BLK
DT_REG = 8,
# define DT_REG DT_REG
DT_LNK = 10,
# define DT_LNK DT_LNK
DT_SOCK = 12,
# define DT_SOCK DT_SOCK
DT_WHT = 14
# define DT_WHT DT_WHT
};
其他说明:
This member is a BSD extension.
The symbol _DIRENT_HAVE_D_TYPE is defined if this member is available.
On systems where it is used, it corresponds to the file type bits in the st_mode member of struct stat.
If the value cannot be determined the member value is DT_UNKNOWN.
来源:http://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html