加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 18:46:01 所属栏目:安全 来源:网络整理
导读:目前我有一些代码(精简和删除一堆错误检查): 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 在编译时我收到以下错误
目前我有一些代码(精简和删除一堆错误检查):
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)遵循标准.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读