基于linux下的高并发服务器开发(第一章)- 模拟实现 ls-l 命令

这一小节会用到上面两张图的红色框里面的变量

任务:

模拟实现 ls -l 指令

-rw-rw-r-- 1 nowcoder nowcoder 12 12月 3 15:48 a.txt

cpp 复制代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <string.h>

// 模拟实现 ls -l 指令
// -rw-rw-r-- 1 nowcoder nowcoder 12 12月  3 15:48 a.txt
int main(int argc, char * argv[]) {

    // 判断输入的参数是否正确
    if(argc < 2) {
        printf("%s filename\n", argv[0]);
        return -1;
    }

    // 通过stat函数获取用户传入的文件的信息
    struct stat st;
    int ret = stat(argv[1], &st);
    if(ret == -1) {
        perror("stat");
        return -1;
    }

    // 获取文件类型和文件权限
    char perms[11] = {0};   // 用于保存文件类型和文件权限的字符串
    
    //(st_mode & S_IFMT) == S_IFREG
    switch(st.st_mode & S_IFMT) { //S_IFMT:掩码 
        case S_IFLNK:         // S_IFLNK:符号链接(软链接)
            perms[0] = 'l';
            break;
        case S_IFDIR:         // S_IFDIR:目录
            perms[0] = 'd';
            break;
        case S_IFREG:         // S_IFREG:符号链接
            perms[0] = '-';
            break; 
        case S_IFBLK:         // S_IFBLK:块设备
            perms[0] = 'b';
            break; 
        case S_IFCHR:         // S_IFCHR:字符设备
            perms[0] = 'c';
            break; 
        case S_IFSOCK:         // S_IFSOCK:套接字
            perms[0] = 's';
            break;
        case S_IFIFO:         // S_IFIFO:管道
            perms[0] = 'p';
            break;
        default:
            perms[0] = '?';
            break;
    }

    // 判断文件的访问权限

    // 文件所有者 User
    perms[1] = (st.st_mode & S_IRUSR) ? 'r' : '-';//st.st_mode文件的类型和存取的权限
    perms[2] = (st.st_mode & S_IWUSR) ? 'w' : '-';
    perms[3] = (st.st_mode & S_IXUSR) ? 'x' : '-';

    // 文件所在组 Group
    perms[4] = (st.st_mode & S_IRGRP) ? 'r' : '-';
    perms[5] = (st.st_mode & S_IWGRP) ? 'w' : '-';
    perms[6] = (st.st_mode & S_IXGRP) ? 'x' : '-';

    // 其他人 Others
    perms[7] = (st.st_mode & S_IROTH) ? 'r' : '-';
    perms[8] = (st.st_mode & S_IWOTH) ? 'w' : '-';
    perms[9] = (st.st_mode & S_IXOTH) ? 'x' : '-';

    // 硬连接数
    int linkNum = st.st_nlink;//st_nlink 连到该文件的硬连接数目

    // 文件所有者
    char * fileUser = getpwuid(st.st_uid)->pw_name;//st_uid 用户ID

    // 文件所在组
    char * fileGrp = getgrgid(st.st_gid)->gr_name;//st_gid 组ID

    // 文件大小
    long int fileSize = st.st_size;//st.st_size 文件字节数(文件大小)

    // 获取修改的时间
    char * time = ctime(&st.st_mtime);//st.st_mtime最后一次修改事件

    //去除换行符
    char mtime[512] = {0};
    strncpy(mtime, time, strlen(time) - 1);

    char buf[1024];
    sprintf(buf, "%s %d %s %s %ld %s %s", perms, linkNum, fileUser, fileGrp, fileSize, mtime, argv[1]);

    printf("%s\n", buf);

    return 0;
}
相关推荐
gongfeng30007 小时前
福州企业级AI自动化获客解决方案品牌梳理与适用场景分析
运维·人工智能·自动化·g-claw ai员工
码农客栈7 小时前
linux 交叉编译tslib
linux
鱼月半8 小时前
两台电脑直连网络不通怎么办?
linux·网络协议
光电笑映9 小时前
网络通信基础:从协议分层到 Socket 编程预备
linux·运维·服务器·网络
酸菜。9 小时前
dw_apb_gpio总结
linux
wunaiqiezixin9 小时前
MIT 6.S081 file system 实验篇(lab9):Large files (moderate)
linux·unix·os·xv6·mit6.s081
GeW9 小时前
RHCE 考试通过率提升:核心考点、实验环境与排错技巧
linux
微小冷10 小时前
用Mermaid画时序图
运维·流程图·时序图·mermaid·生命线
其实防守也摸鱼10 小时前
Codex 下载与本地部署实战:从安装到运行全指南
android·大数据·运维·安全·自动化
wdfk_prog10 小时前
ROS教程07:从 ros::start() 顺着源码读懂 Master、XML-RPC 与 Topic 注册发现
运维·缓存·docker·容器·ros