linux--Basic article--dir dirent Stat

Source: Internet
Author: User

The dir struct under Linux is defined as

1 struct__dirstream2    {3     void*__fd;4     Char*__data;5     int__entry_data;6     Char*__ptr;7     int__entry_ptr;8 size_t __allocation;9 size_t __size;Ten __libc_lock_define (, __lock) One    }; Atypedefstruct__dirstream DIR;

The struct holds the directory-related information, such as opendir (const char *), and returns the type of Dir *;

The functions used in this structure are also:

1 struct dirent *readdir (DIR *dp); 2 void Rewinddir (DIR *dp); 3 int closedir (DIR *dp); 4 long telldir (DIR *dp); 5 void Seekdir (DIR *dp,long Loc);

When you get dir *, the Readdir (dir *) function gives you a pointer to the Dirent type Dirent structure not only saves directory information, but also holds specific information about a file

The dirent structure is defined as

1 structdirent2 {3     LongD_ino;/*inode Number Index node*/4off_t D_off;/*offset to this dirent in the directory file*/5Unsigned ShortD_reclen;/*length of this d_name filename long*/6UnsignedCharD_type;/*The type of d_name file type*/7     CharD_name [name_max+1];/*File name (null-terminated) file name, maximum 255 characters*/8}

Through Dirent's d_name, use the following function

int stat (const char *file_name, struct stat *buf);

You can get more detailed information about a file, and you can implement the LS function in Linux.

The stat structure is defined as

1 structStat {2mode_t St_mode;//File access Permissions3ino_t St_ino;//Index Node Number4dev_t St_dev;//the device number used by the file5dev_t St_rdev;//Device number of the device file6nlink_t St_nlink;//number of hard connections to a file7uid_t St_uid;//owner user identification number8gid_t St_gid;//Group Identification Number9off_t st_size;//file capacity in bytesTentime_t St_atime;//the last time the file was accessed Onetime_t St_mtime;//the last time the file was modified Atime_t St_ctime;//The last time the file state was changed -blksize_t st_blksize;//The size of the disk block that contains the file -blkcnt_t st_blocks;//the disk block that the file occupies the};

Here is a simple implementation of LS (for advanced programming of the UNIX environment):

By: http://www.liweifan.com/2012/05/13/linux-system-function-files-operation/

linux--Basic article--dir dirent Stat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.