Linux命令:chmod opendir readdir closedir

我们在用shell敲命令的时候有一个chmod,大家都很熟悉了,其实chmod还是一个函数,我们可以在程序中给文件权限:
我们先创建一个hello文件,看一下权限

然后看代码

Linux chmod opendir readdir closedir

执行,再看hello的权限

Linux chmod opendir readdir closedir

下面我们来看下opendir,readdir closedir

opendir
函数原型
  DIR* opendir (const char * path ); (获取path子目录下的所由文件和目录的列表,如果path是个文件则返回值为NULL)
功能
  打开一个目录,在失败的时候返回一个空的指针。
返回值(DIR)
DIR 结构体的原型为:struct_dirstream
在Linux系统中:
typedef struct __dirstream DIR;
struct __dirstream
{
void __fd; / struct hurd_fd' pointer for descriptor. */
char *__data; /* Directory block. */
int __entry_data; /* Entry number__data’ corresponds to. */
char __ptr; / Current pointer into the block. */
int __entry_ptr; /* Entry number `__ptr’ corresponds to. */
size_t __allocation; /* Space allocated for the block. */
size_t __size; /* Total valid data in the block. */
__libc_lock_define (, __lock) /* Mutex lock for this structure. */
};

readdir
函数原型
struct dirent* readdir(DIR* dir_handle); (个人理解循环读取dir_handle,目录和文件都读)
功能
读取opendir 返回值的那个列表
返回值
返回dirent结构体指针,dirent结构体成员如下,(文件和目录都行)
struct dirent
  {
  long d_ino; /* inode number 索引节点号 */
  off_t d_off; /* offset to this dirent 在目录文件中的偏移 */
  unsigned short d_reclen; /* length of this d_name 文件名长 */
  unsigned char d_type; /* the type of d_name 文件类型 */
  char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */
  }

下面来看一个例子,把当前目录中的文件打印到shell终端:

Linux chmod opendir readdir closedir

Linux chmod opendir readdir closedir

好了,Linux chmod opendir readdir closedir就总结到这里,如有问题,欢迎指正,谢谢。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/14689.html