linux c 目录

1) fstat, stat, lstat 系统调用
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h> // Linux系统不需要,UNIX需要

int fstat(int fildes,struct stat *buf);
返回文件描述符相关的状态文件的信息,这些信息被保存在一个stat结构体中,stat结构体几乎保存了所有的文件状态信息。

int stat(const char *path, struct stat *buf);
int lstat(const char *path, struct stat *buf);
这两个系统调用提供了一样的结果,但是他们参数有些不同,他们接受一个路径,而不是一个文件描述符。另外 lstat和stat不一样的是,假如读取到了符号连接,lstat读取符号连接本身的状态信息,而stat读取的是符号连接指向文件的信息。

得到了stat的结构体指针,还需要了解如何使用它,首先来了解它的一些成员:

st_mode 文件的权限信息和类型信息
st_ino 文件相关的inode
st_dev 文件所属的设备
st_uid 文件所有者的ID
st_gid 文件所有者的组ID
st_atime 文件最后访问时间
st_ctime 文件最后修改时间(修改权限,用户,组或者内容)
st_mtime 最后修改内容的时间
st_nlink 硬连接的数母

由上表可见,stat结构体通过信息的丰富,关于如何利用没一个信息,这不是本文的主题。最常用的就是st_mode成员了
为了方便的获取文件状态信息,定义了一些操作st_mode的宏:
S_ISDIR  // 是不是目录
S_IFBLK //  文件是不是块设备
S_IFIFO //  文件是不是FIFO(命名管道)
S_IFLNK  //  文件是不是符号连接
定义的宏还有很多,全面的介绍似乎不符合本文的主题,这就不说了。

2) opendir()
#include <sys/types.h> // Linux系统不需要,UNIX需要
#include <dirent.h> // dirent是directory entry的缩写
DIR *opendir(const char *name);
通过路径打开一个目录,返回一个DIR结构体指针(也叫做目录流),和文件描述符以及文件流一样,它是对应一个特定的目录,并作为其他操作目录的系统调用的参数。系统调用失败返回NULL。

3) readdir()
#include <sys/types.h> // Linux系统不需要,UNIX需要
#include <dirent.h>
读取目录中的下一个目录实体(directory entry),readdir(DIR *)结构一个DIR结构体指针作为参数,返回一个dirent结构体指针,这个结构体的具体内容在:http://www.i170.com/user/killercat/Article_54617的最后一部分有描述过,包含了文件名字(d_name成员)和 Inode(d_ino成员)。这个系统调用失败或者没有目录实体可以读取时,返回为NULL。

4) chdir()
#include <unistd.h>  //  注意一下这个头文件和其他的不一样
int chdir(const char *path);
改变目录。于用户通过cd命令改变目录一样,程序也可以通过chdir来改变目录,这样使得 fopen(),opendir(),这里需要路径的系统调用,可以使用相对于当前目录的相对路径打开文件(目录)。

5) closedir(DIR*)
#include <dirent.h>
#include <sys/types.h> // Linux系统不需要,UNIX需要
关闭目录流。


下面是递归的打出一个目录下文件的程序(出自《Linux 程序设计》英文版第3版):
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>

void printdir(char *dir, int depth){
    DIR *dp;
    struct dirent *entry;
    struct stat statbuf;
   
    if((dp = opendir(dir)) == NULL){
        fprintf(stderr,"cannot open directory: %s\n",dir);
        return;
    }
    chdir(dir);
    while((entry = readdir(dp)) != NULL){
        lstat(entry->d_name,&statbuf);
        if(S_ISDIR(statbuf.st_mode)){
            if(strcmp("..",entry->d_name) == 0||
            strcmp(".",entry->d_name) == 0)
                continue;
            printf("%*s%s/\n",depth,"",entry->d_name);
            printdir(entry->d_name,depth+4);
        }
        else printf("%*s%s\n",depth,"",entry->d_name);
    }
    chdir("..");
    closedir(dp);
}

int main(){
    printdir("/home",0);
}

posted @ 2010-01-14 19:04  六不朽  阅读(626)  评论(0编辑  收藏  举报