unix – 跨平台的方式来测试一个文件是否是一个目录

目前我有一些代码(精简和删除一堆错误检查):
dp = readdir(dir);
if (dp->d_type == DT_DIR) {
}

这在我的Linux机器上游泳.但是在另一台机器上(看起来像SunOS,sparc):

SunOS HOST 5.10 Generic_127127-11 sun4u sparc SUNW,Ultra-5_10

在编译时我收到以下错误

error: structure has no member named `d_type'
error: `DT_DIR' undeclared (first use in this function)

我以为dirent.h头是crossplatform(对于POSIX机器).有什么建议么.

参考 http://www.nexenta.org/os/Porting_Codefixes

The struct dirent deFinition in solaris does not contain the d_type field. You would need to make the changes as follows

if (de->d_type == DT_DIR)
{
   return 0;
}

changes to

struct stat s; /*include sys/stat.h if necessary */
..
..
stat(de->d_name,&s);
if (s.st_mode & S_IFDIR)
{
  return 0;
}

由于stat也是POSIX标准,应该是跨平台的.但是您可能想要使用if((s.st_mode& S_IFMT)== S_IFDIR)遵循标准.

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...