Write your own ls command

Source: Internet
Author: User
Tags lstat posix

Author: Cao Zhongming, huaqing visionary embedded College lecturer.

ls command is the most commonly used in Linux under one of the commands, its use is very simple, but a lot of functions, there are many parameters, here we write a LS command, to achieve LS basic functions. Before that, let's start with a few of the functions that are used in implementing the LS process.

Stat/lstat function

These two functions are essentially the same, both to obtain the properties of the file, except that if the file is a symbolic link stat returns the attribute that the symbolic link points to the file, and Lstat returns the attributes of the symbolic link itself.

Function Prototypes:

int stat (const char *path, struct stat *buf);
int Lstat (const char *path, struct stat *buf);

Path is the file path, the BUF is the returned state, the type is struct stat, and the structure body content is:

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 file system 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 * *
};

The member St_mode in this structure is used to represent the type of file and the permissions of the file, which are defined as follows:

S_IFMT 0170000 bit mask for the file type bit fields
S_ifsock 0140000 Socket
S_iflnk 0120000 Symbolic Link
S_ifreg 0100000 Regular file
S_IFBLK 0060000 block Device
S_ifdir 0040000 Directory
S_IFCHR 0020000 character device
S_ififo 0010000 FIFO
S_isuid 0004000 set UID bit
S_isgid 0002000 set-group-id bit (= below)
S_isvtx 0001000 sticky bit (= below)
S_irwxu 00700 Mask for file owner permissions
S_IRUSR 00400 owner has Read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
S_IRWXG 00070 mask for group permissions
S_IRGRP 00040 Group has Read permission
S_IWGRP 00020 Group has write permission
S_IXGRP 00010 Group has execute permission
S_irwxo 00007 Mask for permissions to others (not in group)
S_iroth 00004 others have Read permission
S_iwoth 00002 others have write permission
S_ixoth 00001 others have execute permission

We can use the following macro to determine the type of file

S_isreg (M) is it a regular file?
S_isdir (m) directory?
S_ISCHR (m) character device?
S_isblk (m) block device?
S_isfifo (M) FIFO (named pipe)?
S_islnk (m) symbolic link? (not in posix.1-1996.)
S_issock (m) socket? (not in posix.1-1996.)

We then implement the first step of LS: Get the properties of the file


This example can only be used to view the properties of a particular file, and LS implementation of the function is if the file will display the file properties, if the directory is to display the contents of the file properties.
The functions required by these functions are:

Opendir/readdir function to get directory entries

Prototype:

DIR *opendir (const char *name);
struct Dirent *readdir (DIR *dirp);
Struct Dirent describes the contents of each item in the directory
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 supported by the all file system types * *
Char d_name[256]; * FileName * *
};

Getopt is used to implement the command options feature:

Prototype:

int getopt (int argc, char * const argv[], const char *optstring);
extern char *optarg;
extern int Optind, opterr, optopt;

Readlink read Symbolic link content:

Prototype:

ssize_t readlink (const char *path, char *buf, size_t bufsiz);
Path for symbolic link paths
BUF for Symbolic link content
BufSize to get the content length
File displays colors by property
033[mode;foreground;backgroundmhello/033[0m

Mode is display mode:
0, 1, 22, 4, 24, 5, 25, 7, 27, respectively, the set color, bold, non-black, drawing line, not under the line, flashing, not flashing, flip, not flip.

Foreground for foreground color:
30 (Black), 31 (red), 32 (green), 33 (yellow), 34 (blue), 35 (magenta), 36 (cyan) and 37 (white)

Background for background color:
40 (Black), 41 (red), 42 (green), 43 (yellow), 44 (blue), 45 (magenta), 46 (cyan) and 47 (white)

Implemented as follows:








Results:

Only part of LS is implemented here, and the rest of the functionality will continue to be completed later.

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.