Linux ls -l命令 C++實現

前言

ls命令是我們在進行linux操作中經常用到的一個命令,在設計文件服務器的時候,我們需要瞭解到ls 的底層實現。
在這裏插入圖片描述


實現程序:

#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <string.h>
#include <dirent.h>
#include <iostream>
#include <iomanip>
#include <pwd.h>
using namespace std;

/*
實現 ls -l命令
傳遞任意一個目錄路徑,能夠顯示該目錄的ls -l的效果
*/

//打印文件權限
void printPriority(struct stat statinfo)
{
    mode_t mode = statinfo.st_mode;
    //判斷文件類型
    cout << (S_ISDIR(mode) ? 'd' : '-');

    //判斷USR權限
    cout << (mode & S_IRUSR ? 'r' : '-');
    cout << (mode & S_IWUSR ? 'w' : '-');
    cout << (mode & S_IXUSR ? 'x' : '-');

    //判斷GRP權限
    cout << (mode & S_IRGRP ? 'r' : '-');
    cout << (mode & S_IWGRP ? 'w' : '-');
    cout << (mode & S_IXGRP ? 'x' : '-');

    //判斷OTH權限
    cout << (mode & S_IROTH ? 'r' : '-');
    cout << (mode & S_IWOTH ? 'w' : '-');
    cout << (mode & S_IXOTH ? 'x' : '-');

    cout << " ";
}

//打印owner 與 group
void printOwner(struct stat statinfo)
{
    struct passwd *passwdinfo = getpwuid(statinfo.st_uid);
    cout << passwdinfo->pw_name << " ";
    passwdinfo = getpwuid(statinfo.st_gid);
    cout << passwdinfo->pw_name << " ";
}

//打印文件大小
void printSize(struct stat statinfo)
{
    cout << setw(6) << statinfo.st_size << " ";
}

//打印時間
void printTime(struct stat statinfo)
{
    time_t rawtime = statinfo.st_mtime;
    struct tm *timeinfo = localtime(&rawtime);
    cout << timeinfo->tm_mon + 1 << "月 " << timeinfo->tm_mday << "日 ";
    if (timeinfo->tm_hour < 9)
        cout << "0" << timeinfo->tm_hour << ":";
    else
        cout << timeinfo->tm_hour << ":";
    
    if (timeinfo->tm_min < 9)
        cout << "0" << timeinfo->tm_min << " ";
    else
        cout << timeinfo->tm_min << " ";
}

void printName(const char* name)
{
    cout << name << " ";
}

void ls_l(const char *path)
{
    chdir(path);
    DIR *dir = opendir(".");
    struct dirent *dirinfo;
    while (dirinfo = readdir(dir))
    {
        if (!strcmp(dirinfo->d_name, ".") || !strcmp(dirinfo->d_name, ".."))
            continue;
        struct stat statinfo;
        stat(dirinfo->d_name, &statinfo);
        //begin
        printPriority(statinfo);
        printOwner(statinfo);
        printSize(statinfo);
        printTime(statinfo);
        printName(dirinfo->d_name);        
        //end
        cout << endl;
    }
}

int main()
{
    ls_l("/home/wwx/Linux/Day04/fileSystem");
    return 0;
}

程序輸出效果:

wwx@VM-0-7-ubuntu:~/Linux/Day04/homework5$ cd "/home/wwx/Linux/Day04/homework5/" && g++ main.cpp -o main && "/home/wwx/Linux/Day04/homework5/"main
-rw-rw-r-- wwx wwx   3178 5月 18日 13:45 main1.cpp 
-rwxr-xr-x wwx wwx   1035 5月 14日 20:01 file3 
-rw-rw-r-- wwx wwx   2917 5月 18日 13:53 main2.cpp 
-rw-rw-r-- wwx wwx   1000 5月 14日 14:47 file1 
-rw-rw-r-- wwx wwx     11 5月 14日 20:01 file4 
-rwxr-xr-x wwx wwx     17 5月 14日 20:19 file6 
-rwxr-xr-x wwx wwx   4192 5月 14日 14:43 file2 
-rwxrwxr-x wwx wwx  14272 5月 18日 13:45 main1 
drwxrwxr-x wwx wwx   4096 5月 14日 10:06 newdir 

ls -l 命令效果:

wwx@VM-0-7-ubuntu:~/Linux/Day04/homework5$ ls -l /home/wwx/Linux/Day04/fileSystem
total 52
-rw-rw-r-- 1 wwx wwx  1000 May 14 14:47 file1
-rwxr-xr-x 1 wwx wwx  4192 May 14 14:43 file2
-rwxr-xr-x 1 wwx wwx  1035 May 14 20:01 file3
-rw-rw-r-- 1 wwx wwx    11 May 14 20:01 file4
-rwxr-xr-x 1 wwx wwx    17 May 14 20:19 file6
-rwxrwxr-x 1 wwx wwx 14272 May 18 13:45 main1
-rw-rw-r-- 1 wwx wwx  3178 May 18 13:45 main1.cpp
-rw-rw-r-- 1 wwx wwx  2917 May 18 13:53 main2.cpp
drwxrwxr-x 4 wwx wwx  4096 May 14 10:06 newdir
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章