1. LS produces a list of file names, which is roughly how it works:
Open directory+-> Read entry -end of dir?-+|__ display file Info | Close Directory <--------------+
2, the directory is a special kind of file, its content is the name of the file and directory. Unlike normal files, catalog files are never empty, and each directory contains at least 2 special items,"."and "..", where"." Not current directory,"..." Represents the previous level of the directory.
3, man 3 Readdir
#include <sys/types.h> #include <dirent.h>dir *opendir (const char *dir_name); struct dirent *readdir (DIR * dir_name); int Readdir_r (dir *dir_ptr, struct dirent *entry, struct dirent **result); Long Telldir (dir *dir_ptr); void Seekdir (dir *dir_ptr, long location), void Rewinddir (dir *dir_ptr); int Closedir (dir *dir_ptr);
4, LS algorithm:
Main () opendir while (readdir) print d_name closedir
5. Get the file information with stat:
Man 2 stat
#include <sys/stat.h>int res = stat (const char *path, struct stat *buf); /* Return 0 for success, 1 for error */
struct Stat { dev_t St_dev; /* ID of device containing file */ ino_t St_ino; /* inode number */ mode_t st_mode; /* Protection */ nlink_t st_nlink; /* Number of hard links */ uid_t st_uid; /* User ID of Owner */ gid_t st_gid; /* Group ID of Owner */ dev_t St_rdev; /* Device ID (if special file) */ off_t st_size; /* Total size, in bytes * /blksize_t st_blksize;/* BlockSize for filesystem I/O */ blkcnt_t st_blocks;
/* number of 512B blocks allocated */ time_t st_atime; /* Time of last access */ time_t st_mtime; /* Time of last modification */ time_t st_ctime; /* Time of last status change * *};
6, man Getpwuid
7, SUID Bit tells the kernel, run this program is considered by the owner of the file to run the program, Sgid bit tells the kernel, run this program when the file belongs to the group to run the program, sticky bit for the file, in the early Exchange (swap) technology, The directory is designed to hold temporary files, such as/tmp.
Ls-l sample-rwsr-sr-t 1 root root 2345 Jul 00:45 sample
8. Setting and modifying properties of a file
#include <sys/types.h> #include <sys/stat.h>int res = chmod (const char *path, mod_t mode); int res = chmod (int f D, mode_t mode); /* On success, 0 is returned. On error, 1 was returned and errno is set appropriately */
#include <unistd.h>int chown (const char *path, uid_t owner, gid_t Group); int Fchown (int fd, uid_t owner, gid_t Group ); int Lchown (const char *path, uid_t owner, gid_t Group); /* On success, 0 is returned. On error, 1 was returned and errno is set appropriately */
9, modify the last modified time and last access time:
Mans Utime
10. Modify the file name
Mans 2 Rename