1. 程式人生 > >《unix環境高階程式設計》--- 檔案和目錄

《unix環境高階程式設計》--- 檔案和目錄

對每個命令函引數列印檔案型別

#include "apue.h"

int main(int argc, char *argv[])
{
    int i;
    struct stat buf;
    char *ptr;

    for(i=1; i<argc; i++)
    {
        printf("%s: ", argv[i]);
        /*
           int lstat(const char *restrict pathname, struct stat *restrict buf);
           返回改符號連結的有關資訊
           struct stat
           {
                mode_t st_mode;   file
type & mode (permissions) ino_t st_ino; i-node number (serial number) dev_t st_dev; device number (file system) dev_t st_rdev; device nummber for special files nlink_t st_nlink; number of links uid_t st_uid; user ID of owner gid_t st_gid; group ID of
owner off_t st_size; size in bytes, for regular files time_t st_atime; time of last modification time_t st_mtime; time of last modification time_t st_ctime; time of last file status change blksize_t st_blksize; best I/O block size blkcnt_t st_blocks; number
of disk blocks allocated } */ if(lstat(argv[i], &buf) < 0) { err_ret("lstat error"); continue; } if(S_ISREG(buf.st_mode)) ptr = "regular"; else if (S_ISDIR(buf.st_mode)) ptr = "directory"; else if(S_ISCHR(buf.st_mode)) ptr = "character special"; else if(S_ISBLK(buf.st_mode)) ptr = "block special"; else if(S_ISFIFO(buf.st_mode)) ptr = "fifo"; else if(S_ISLNK(buf.st_mode)) ptr = "symbolic link"; else if(S_ISSOCK(buf.st_mode)) ptr = "socket"; else ptr = "** unkown mode **"; printf("%s\n", ptr); } exit(0); }

這裡寫圖片描述

access函式例項

#include "apue.h"
#include  <fcntl.h>

int main(int argc, char *argv[])
{
    if(argc != 2)
        err_quit("usage: a.out <pathname>");

    /*
       int access(const char *pathname, int mode);
       按實際使用者ID和實際組ID進行訪問許可權測試
       mode:
       R_OK 測試讀許可權
       W_OK 測試寫許可權
       X_OK 測試執行許可權
       F_OK 測試檔案是否存在
    */
    if(access(argv[1], R_OK)< 0)
        err_ret("access error for %s", argv[1]);
    else
        printf("read access OK\n");

    /*
           open按程序的有效使用者ID和有效組ID程序訪問許可權測試
    */
    if(open(argv[1], O_RDONLY)  < 0)
        err_ret("open error fo %s", argv[1]);
    else
        printf("open for reading OK\n");
    exit(0);
}

這裡寫圖片描述

umask函式例項
這裡寫圖片描述

#include "apue.h"
#include <fcntl.h>

#define RWRWRW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)

int main(void)
{
    /*
       mode_t umask(mode_t cmask);
       為程序設定“檔案模式建立”遮蔽字,並返回以前的值
    */
    umask(0);
    if(creat("foo", RWRWRW) < 0)
        err_sys("create error for foo");

    /* 禁止所有組和其他使用者的訪問許可權 */
    umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
    if(creat("bar", RWRWRW) < 0)
        err_sys("create error for bar");
    exit(0);
}

這裡寫圖片描述
更改程序的檔案模式建立遮蔽字不影響父程序的遮蔽字
這裡寫圖片描述

chmod函式例項
這裡寫圖片描述

#include "apue.h"

int main(void)
{
    struct stat statbuf;

    /* turn on set-group-ID and turn off group-execute */
    if(stat("foo", &statbuf) < 0)
        err_sys("stat error for foo");

    /*
           int chmod(const char *pathname, mode_t mode);
       更改檔案訪問許可權
       程序的有效使用者ID必須等於檔案的所有者ID,或該程序必須具有超級使用者許可權
    */
    if(chmod("foo", (statbuf.st_mode & ~S_IXGRP) | S_ISGID) < 0)
        err_sys("chmod error for foo");

    /* set absolute mode to "rw-r--r--" */
    if(chmod("bar", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0)
        err_sys("chmod error for bar");
    exit(0);
}

這裡寫圖片描述
組執行位為S,表示設定組ID位已設定,同時,組執行位則未設定

開啟一個檔案,然後unlink

#include "apue.h"
#include <fcntl.h>

int main(void)
{
    if(open("tempfile", O_RDWR) < 0)
        err_sys("open error");

    /*
       int unlink(const char *pathname);
       刪除一個現有的目錄項
       當開啟檔案程序數為0, 連結計數等於0時,才可被刪除
    */
    if(unlink("temfile") < 0)
        err_sys("unlink error");
    printf("file unlinked\n");
    sleep(2);
    printf("down\n");
    exit(0);
}

這裡寫圖片描述

utime函式例項
目的:將檔案長度截段為0,但並不更改起訪問時間及修改時間

#include "apue.h"
#include <fcntl.h>
#include <utime.h>

int main(int argc, char *argv[])
{
    int i, fd;
    struct stat statbuf;
    struct utimbuf timebuf;

    for(i=1; i<argc; i++)
    {
        /* fetch current times */
        if(stat(argv[i], &statbuf) < 0)
        {
            err_ret("%s: stat error", argv[i]);
            continue;
        }

        /* truncate */
        if((fd = open(argv[i], O_RDWR | O_TRUNC)) < 0)
        {
            err_ret("%s: open error", argv[i]);
            continue;
        }
        close(fd);

        timebuf.actime = statbuf.st_atime;  /* access time */
        timebuf.modtime = statbuf.st_mtime; /* modification time */

        /*
           int utime(const char *pathname, const struct utimbuf *times);
           修改檔案的訪問和修改時間
           struct utimebuf
           {
            time_t actime;   access time 
            time_t modtime;  modification time
           }
        */
        if(utime(argv[i], &timebuf) < 0)
        {
            err_ret("%s: utime error", argv[i]);
            continue;
        }
    }
    exit(0);
}
yjp@yjp-VirtualBox:~/apue/4filedir$ ls -l changemode   檢視長度和最後修改時間
-rwxr-x--- 1 yjp yjp 13712 516 21:18 changemode
yjp@yjp-VirtualBox:~/apue/4filedir$ ls -lu changemode  檢視最後訪問時間
-rwxr-x--- 1 yjp yjp 13712 516 21:19 changemode
yjp@yjp-VirtualBox:~/apue/4filedir$ date               列印當天日期
20180517日 星期四 13:45:18 CST
yjp@yjp-VirtualBox:~/apue/4filedir$ ./utime changemode 執行程式
yjp@yjp-VirtualBox:~/apue/4filedir$ ls -l changemode   檢查最後修改時間
-rwxr-x--- 1 yjp yjp 0 516 21:18 changemode
yjp@yjp-VirtualBox:~/apue/4filedir$ ls -lu changemode  檢查最後訪問時間
-rwxr-x--- 1 yjp yjp 0 516 21:19 changemode
yjp@yjp-VirtualBox:~/apue/4filedir$ ls -lc changemode  檢查更改狀態時間
-rwxr-x--- 1 yjp yjp 0 517 13:45 changemode

遞迴降序遍歷目錄層次結構,並按檔案型別計數

#include "apue.h"
#include <dirent.h>
#include <limits.h>

/* function type that is called for each filename */
typedef int Myfunc(const char *, const struct stat *, int);

static Myfunc myfunc;
static int myftw(char *, Myfunc *);
static int dopath(Myfunc *);

static long nreg, ndir, nblk, nchr, nfifo, nslink, nsock, ntot;

int main(int argc, char *argv[])
{
    int ret;

    if(argc != 2)
        err_quit("usage: ftw <starting-pathname>");

    ret = myftw(argv[1], myfunc);

    ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock;
    if(ntot == 0)   
        ntot = 1;  /* avoid divide by 0; print 0 for all counts */

    printf("regular files = %7d, %5.2f %%\n", nreg, nreg*100.0/ntot);
    printf("directories = %7d, %5.2f %%\n", ndir, ndir*100.0/ntot);
    printf("block special = %7d, %5.2f %%\n", nblk, nblk*100.0/ntot);
    printf("char special = %7d, %5.2f %%\n", nchr, nchr*100.0/ntot);
    printf("FIFOs = %7d, %5.2f %%\n", nfifo, nfifo*100.0/ntot);
    printf("symbolic links = %7d, %5.2f %%\n", nslink, nslink*100.0/ntot);
    printf("socks = %7d, %5.2f %%\n", nsock, nsock*100.0/ntot);
    exit(ret);
}

/*
   Descend through the hierarchy, starting at "pathname".
   The caller's func() is called for every file.
*/
#define FTW_F 1  /* file other than directory */
#define FTW_D 2  /* directory */
#define FTW_DNR 3 /* directory that can't be read */
#define FTW_NS 4 /* file that we can't stat */

static char *fullpath;  /* contains full pathname for every file */

static int myftw(char *pathname, Myfunc *func)
{
    int len = 4096;

    /* malloc's for PATH_MAX+1 bytes 
       用來存放整個路徑,包括子資料夾
        */

    fullpath = malloc(len);

    strncpy(fullpath, pathname, len);  /* protect against */
    fullpath[len-1] = 0;

    printf("len: %d\n", len);
    printf("%s\n", pathname);
    printf("%s\n", fullpath);
    printf("%d\n", strlen(fullpath));

    return(dopath(func));
}

/*
  Descend through the hierarchy, starting at "fullpath".
  If "fullpath" is anyting other than a directory, we lstat() it,
  call func(), and return. For a directory, we call ourself
  recursively for each name in the directory.
*/
static int dopath(Myfunc *func)
{
    struct stat statbuf;

    /*
      struct dirent
      {
         ino_t d_ino;  i-node number
         char d_name[NAME_MAX+1]  null-terminated filename
      }
    */
    struct dirent *dirp;

    DIR *dp;
    int ret;
    char *ptr;

    if(lstat(fullpath, &statbuf) < 0) /* stat error */
        return (func(fullpath, &statbuf, FTW_NS));
    if(!S_ISDIR(statbuf.st_mode)) /* not a directory */
        return (func(fullpath, &statbuf, FTW_F));

    /*
       It's a directory. First call func() for the directory,
       then process each filename in the directory.
    */
    if((ret = func(fullpath, &statbuf, FTW_D)) != 0)
        return ret;

    ptr = fullpath + strlen(fullpath);  /* point to end of fullpath */
    *ptr++ = '/';
    *ptr = 0;

    /*
      DIR *opendir(const char *pathname);
      執行初始化操作,時第一個readdir讀目錄的第一個目錄項
    */
    if((dp = opendir(fullpath)) == NULL)  /* can't read directory */
        return(func(fullpath, &statbuf, FTW_DNR));

    /*
      struct dirent *readdir(DIR *dp);
    */
    while((dirp = readdir(dp)) != NULL)
    {
        if(strcmp(dirp->d_name, ".") == 0 ||
            strcmp(dirp->d_name, "..") == 0)
            continue;  /* ignore dot and dot-dot */

        strcpy(ptr, dirp->d_name);  /* append name after slash */
        printf("%s\n", fullpath);

        if((ret = dopath(func)) != 0) /* recursive */
            break;  /* time to leave */
    }
    ptr[-1] = 0;  /* erase everything from slash onwards */

    /*
      int closedir(DIR *dp);
    */
    if(closedir(dp) < 0)
        err_ret("can't close directory %s", fullpath);

    return ret;
}

static int myfunc(const char *pathname, const struct stat *statptr, int type)
{
    switch(type)
    {
        case FTW_F:
            switch(statptr->st_mode & S_IFMT)
            {
            case S_IFREG: nreg++; break;
            case S_IFBLK: nblk++; break;
            case S_IFCHR: nchr++; break;
            case S_IFIFO: nfifo++; break;
            case S_IFLNK: nslink++; break;
            case S_IFDIR: err_dump("for S_IFDIR for %s", pathname);
            /* directories should have type = FTW_D */
            default:
            err_dump("wrong type %d for pathname %s", type, pathname);
                break;
            }
            break;
        case FTW_D:
            ndir++;
            break;
        case FTW_DNR:
            err_ret("can't read directory %s", pathname);
            break;
        case FTW_NS:
            err_ret("stat error for %s", pathname);
            break;
        default:
            err_dump("unkown type %d for pathname %s", type, pathname);
            break;

    }
    return 0;
}

這裡寫圖片描述

chdir函式例項
當前工作目錄是程序的一個屬性,所以隻影響呼叫chdir的程序本身,不影響其他程序,即呼叫該程式不會得到希望的結果

#include "apue.h"

int main(void)
{
    /*
      int chdir(const char *pathname);
      更改工作目錄 
    */
    if(chdir("/tmp") < 0)
        err_sys("chdir failed");
    printf("chdir to /tmp successed\n");
    exit(0);
}

這裡寫圖片描述

getcwd函式例項

#include "apue.h"

int main(void)
{
    char *ptr;
    int size;

    if(chdir("/tmp") < 0)
        err_sys("chdir failed");

    ptr = path_alloc(&size);  /* our own function */

    /*
      char *getcwd(char *buf, size_t size);
      得到當前工作目錄
      從當前工作目錄.開始,用..目錄找上一級目錄,然後讀目錄項,
      直到該目錄項中的i節點編號與工作目錄i節點編號相同,這就找到了對應的檔名
      buf: 容納絕對路徑名+null終止字元
      size: buf長度
    */
    if(getcwd(ptr, size) == NULL)
        err_sys("getcwd failed");

    printf("cwd = %s\n", ptr);
    exit(0);
}

這裡寫圖片描述

列印st_dev和st_rdev值

#include "apue.h"
#ifdef SOLAPRIS
#include <sys/mkdev.h>
#endif

int main(int argc, char *argv[])
{
    int i;
    struct stat buf;

    for(i=1; i<argc; i++)
    {
        printf("%s: ", argv[i]);
        if(stat(argv[i], &buf) < 0)
        {
            err_ret("stat error");
            continue;
        }

        /* 列印主裝置號和從裝置號 */
        printf("dev = %d/%d", major(buf.st_dev), minor(buf.st_dev));

        if(S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode))
        {
            printf("(%s) rdev = %d/%d",
                (S_ISCHR(buf.st_mode)) ? "character" : "block",
                major(buf.st_rdev), minor(buf.st_rdev));
        }

        printf("\n");
    }
    exit(0);
}

這裡寫圖片描述
第一個是目錄,後兩個是裝置
裝置號不同,說明位於不同的檔案系統
兩個終端裝置(st_dev)的檔名和i節點在裝置0/6上(devfs偽檔案系統),實際裝置號是4/0和4/1