和标准IO访问文件类似,Linux本着万物皆文件的原则,对文件进行访问。

涉及相关函数:DIR * opendir     closedir  readdir  chmod fchmod stat lstat   fstat  getpwuid  getgrpid

练习demo:按照ls命令的输出,使用stat函数实现。

#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>

int main(int argc, const char *argv[])
{
DIR *dirp;
struct dirent *dent;
struct stat buf;
struct tm *t;
char *month[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug",
"Sept","Oct","Nov","Dec"};

dirp = opendir("/home/linux/step4/day2");
if(dirp == NULL){
perror("opendir");
return -1;
}
while((dent = readdir(dirp)) != NULL){

lstat(dent->d_name,&buf);

if(S_ISREG(buf.st_mode)) printf("-");
else if(S_ISDIR(buf.st_mode)) printf("d");
else if(S_ISCHR(buf.st_mode)) printf("c");
else if(S_ISBLK(buf.st_mode)) printf("b");
else if(S_ISFIFO(buf.st_mode)) printf("p");
else if(S_ISLNK(buf.st_mode)) printf("l");
else printf("s");

if(S_IRUSR & buf.st_mode) printf("r");
else printf("-");
if(S_IWUSR & buf.st_mode) printf("w");
else printf("-");
if(S_IXUSR & buf.st_mode) printf("x");
else printf("-");
if(S_IRGRP & buf.st_mode) printf("r");
else printf("-");
if(S_IWGRP & buf.st_mode) printf("w");
else printf("-");
if(S_IXGRP & buf.st_mode) printf("x");
else printf("-");
if(S_IROTH & buf.st_mode) printf("r");
else printf("-");
if(S_IWOTH & buf.st_mode) printf("w");
else printf("-");
if(S_IXOTH & buf.st_mode) printf("x");
else printf("-");

printf("%2d",buf.st_nlink);
printf(" %s %s",getpwuid(buf.st_uid)->pw_name,getgrgid(buf.st_gid)->gr_name);
printf(" %6ld",buf.st_size);
t = localtime(&(buf.st_atime));
printf(" %s %2d %02d:%02d",month[t->tm_mon],t->tm_mday,t->tm_hour,t->tm_sec);

printf(" %s\n",dent->d_name);
}
closedir(dirp);
return 0;
}

转载于:https://www.cnblogs.com/huiji12321/p/11305698.html

step4 . day3 目录访问函数相关推荐

  1. Linux系统编程.NO7——目录操作函数

    5. 目录文件管理函数 5.1. 目录文件概念 目录在Linux中也是文件,也有对应的文件指针,每次读目录之前也要先打开目录,然后目录指针就出现了,第一次读就是第一个文件的信息,然后第二次读就是第二个 ...

  2. LDAP 协议入门(轻量目录访问协议)

    什么是 LDAP? LDAP 的全称是 Lightweight Directory Access Protocol,「轻量目录访问协议」. 划重点,LDAP 「是一个协议」,约定了 Client 与 ...

  3. FatFs目录访问接口中文版

    ▼我是阿荣,关注我,在技术路上一起精进!▼ 目录访问 f_opendir - 打开目录 函数原型 FRESULT f_opendir (DIR* dp, /* [OUT] Pointer to the ...

  4. php导航遍历代码,目录遍历函数_php

    php中的目录遍历功能本来也很普通,但它却具有一种"面向对象"的形式, 所以也提一下: (1)dir, opendir 用法: http://www.gaodaima.com/45 ...

  5. 磁盘出现“位置不可用无法访问函数不正确”的解决方法

    当进入"我的电脑"后会发现出现问题的硬盘中的分区会无法显示相关信息:如容量大小.可用空间等.磁盘出现"位置不可用无法访问函数不正确"的解决方法"的错误 ...

  6. apache禁止访问html,apache 限制目录访问

    比如说登陆nagios或者是其它监控软件等,都会提示输入用户名密码,才能登陆,其实就是在apache中限制目录访问权限. 通过用户名和密码的方式进行验证. 比如我这里将/var/www/html/a目 ...

  7. 时间:2014年3月27日文件和目录操作函数

    主要内容: 目录操作:打开.读取.关闭.创建.重命名.删除 文件操作:打开.读.写.关闭.重命名.删除 信息获取函数 filemtime() filesize() file_exists()  is_ ...

  8. 轻量目录访问协议 工具 openldap 简介

    目录 为什么会有本文? 先了解一些概念 什么是ldap ldap的软件 ldap的信息模型 centos7上的安装 安装openldap ldap的配置文件 创建olcRootDN作为管理员账号 添加 ...

  9. LDAP 轻量级目录访问协议 介绍

    LDAPS 和 LDAP 区别 LDAP(轻型目录应用程序协议)和安全LDAP(LDAPS)是应用程序与基础结构中的网络目录或域控制器之间使用的连接协议.  注意,LDAP以明文形式传输通信,而LDA ...

最新文章

  1. qt 将不同的数据类型组成一个新类型_SQL 通用数据类型
  2. Python ndarray.strides用法
  3. 列表推导式与生成表达式的区别
  4. 二叉堆的操作总结(insert+deleteMin+increaseKey+decreaseKey+percolateDown+percolateUp)
  5. 保存点云数据_3D点云配准(二多幅点云配准)
  6. 一个很详细的web.xml讲解(转)
  7. MIT“35岁以下科技创新35人”榜单揭晓
  8. 图像主观质量评价 评分_视频质量评价算法 之 客观评价的性能指标
  9. poj 3017 Cut the Sequence(DP+单调队列)
  10. 道硕改革宗神学院课表简略
  11. 【数据结构笔记45】通过中间步骤区分简单插入排序和归并排序
  12. 【0702作业】输出九九乘法表
  13. 如何在 Mac 上的“地图”中自定地图视图?
  14. linux 脚本 格式化,Shell脚本批量格式化文件名
  15. python爬取qq音乐
  16. 计算机10秒后重新启动,电脑主机开机几秒或者几十秒自动断电关机
  17. 【微信每日早安推送】每日天气推送
  18. mybatis-sqlserver批量新增返回id
  19. 3.云计算基础篇---云计算优势
  20. 2022年全球市场智能纺织品总体规模、主要生产商、主要地区、产品和应用细分研究报告

热门文章

  1. linux运维面板_phpstudy linux web面板(小皮面板)V0.2版本正式发布
  2. Win Api 有名管道通信--扫雷辅助(四)
  3. 关于jsp web项目,jsp页面与servlet数据不同步的解决办法(报错404、405等)即访问.jsp和访问web.xml中注册的/servlet/的区别
  4. 每天一道LeetCode-----找到给定序列中所有和为某个值的集合或集合个数,序列中可以有/无重复项,集合元素顺序不同算不同集合等
  5. 三数之和—leetcode15
  6. float与double类型区别比较
  7. layui登录页面写入数据_layui 页面保存数据
  8. linux dns 攻击,DNSlog攻击技巧 | CN-SEC 中文网
  9. 2021-02-25
  10. C++11 unique_ptr用法