ls命令的實現

來源:互聯網
上載者:User

轉自:http://blog.chinaunix.net/space.php?uid=25909722&do=blog&id=2856481

http://blog.chinaunix.net/uid-25909722-id-2876133.html


ls - list directory contents (man ls)
ls的功能是列出目錄中的內容。那目錄中都有寫什麼內容呢。我們知道磁碟上的檔案系統由檔案和目錄組成,檔案的內容可以是任意的資料,而目錄的內容只能是檔案名稱的列表和子目錄名的列表。 也就是說:目錄中的內容 是該目錄下的檔案和子目錄它們的 名字的列表。
我們先來看一看關於 目錄的結構體:(/usr/include/bits/dirent.h) struct dirent {
       ino_t          d_ino;         /* inode number */
       off_t          d_off;         /* offset to the next dirent */
       unsigned short d_reclen;      /* length of this record */
       unsigned char  d_type;        /* type of file; not supportedby all file system types */
       char           d_name[256];   /* filename */
}; d_ino是目錄中的檔案的i-node節點號,通過d_ino可以找到該檔案的i-node節點,而i-node節點中儲存了檔案的屬性等資訊。通過i-node節點就可以訪問到檔案的內容。
關於目錄訪問的一些函數:(/usr/include/dirent.h) #include <sys/types.h>
#include <dirent.h>

DIR *opendir(const char *name);
DIR *fdopendir(int fd);
struct dirent *readdir(DIR *dirp);
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
int closedir(DIR *dirp);
void rewinddir(DIR *dirp); 關於目錄中檔案的屬性等資訊使用 struct stat 結構體來描述的,其結構的定義如下: (/usr/include/bits/stat.h) struct stat
{
    __dev_t st_dev;                 /* Device. */
    unsigned short int __pad1;      /* 用於填充對齊 */
    __ino_t   st_ino;               /* 32bit file serial number. */     __mode_t  st_mode;              /* File mode. */
    __nlink_t st_nlink;             /* Link count. */
    __uid_t   st_uid;               /* User ID of the file's owner. */
    __gid_t   st_gid;               /* Group ID of the file's group.*/     __dev_t   st_rdev;              /* Device number, if device. */

    unsigned short int __pad2;      /* 用於填充對齊 */
    __off_t     st_size;            /* Size of file, in bytes. */     __blksize_t st_blksize;         /* Optimal block size for I/O. */     __blkcnt_t  st_blocks;          /* Number 512-byte blocks allocated. */

    struct timespec st_atim;        /* Time of last access. */
    struct timespec st_mtim;        /* Time of last modification. */
    struct timespec st_ctim;        /* Time of last status change. */

# define st_atime st_atim.tv_sec    /* Backward compatibility. */
# define st_mtime st_mtim.tv_sec
# define st_ctime st_ctim.tv_sec
    __ino64_t st_ino;               /* File serial number. */
}; 該結構體提供了關於檔案(或者裝置)的如下重要訊息: st_mode      檔案類型和許可許可權 st_nlink      檔案連結數 st_uid         檔案屬主id st_gid         檔案屬主所在組的id st_size        檔案的位元組數 st_blocks    檔案所佔的塊數
“ ACM”三個時間: st_atime     檔案最後訪問時間 ( access time) st_mtime    檔案最後修改時間 ( modification time) st_ctime     檔案屬性/狀態最後改變的時間 ( change status time)
另外在/usr/include/sys/stat.h中提供了下列關於檔案資訊的宏定義: typedef __dev_t     dev_t;
typedef __gid_t     gid_t;
typedef __ino_t     ino_t;
typedef __mode_t    mode_t;
typedef __nlink_t   nlink_t;
typedef __off_t     off_t;
typedef __uid_t     uid_t;
typedef __blkcnt_t  blkcnt_t;
typedef __blksize_t blksize_t;
/* Test macros for file types. */
#define __S_ISTYPE(mode, mask) (((mode) & __S_IFMT) == (mask))

#define S_ISDIR(mode) __S_ISTYPE(

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.