前言
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