C语言模拟实现ls -al命令

友链

C代码如下:

#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 -al命令
// -rwxrwxrwx 1 x x 12 Feb 10 18:49 1.txt
int main(int argc, char* argv[]) {
    if (argc < 2) {
       printf("usage:\n\t %s filename\n", argv[0]);
       return -1;
    }

    // 通过stat函数获取用户传入的文件的信息
    struct stat st;
    int ret = stat(argv[1], &st);
    if (ret == -1) {
        perror("stat");
        return -1;
    }
    
    // 获取文件类型和文件权限,一共10位
    // perms字符串数组用于保存文件类型和权限
    char perms[11] = {0};

    // S_IFMT是一个掩码,和它进行与运算能够获得文件类型
    switch (st.st_mode & S_IFMT) {
        // 符号链接
        case S_IFLNK:
            perms[0] = 'l';
            break;
        // 目录
        case S_IFDIR:
            perms[0] = 'd';
            break;
        // 普通文件
        case S_IFREG:
            perms[0] = '-';
            break;
        // 块设备
        case S_IFBLK:
            perms[0] = 'b';
            break;
        // 字符设备
        case S_IFCHR:
            perms[0] = 'c';
            break;
        // 管道
        case S_IFIFO:
            perms[0] = 'p';
            break;
        // 套接字
        case S_IFSOCK:
            perms[0] = 's';
            break;
        default:
            perms[0] = '?';
            break; 
    }

    // 获取文件的权限
    // 获取文件所有者的读、写、执行权限
    perms[1] = st.st_mode & S_IRUSR ? 'r' : '-';
    perms[2] = st.st_mode & S_IWUSR ? 'w' : '-';
    perms[3] = st.st_mode & S_IXUSR ? 'x' : '-';
    
    // 获取文件所属组的读、写、执行权限
    perms[4] = st.st_mode & S_IRGRP ? 'r' : '-';
    perms[5] = st.st_mode & S_IWGRP ? 'w' : '-';
    perms[6] = st.st_mode & S_IXGRP ? 'x' : '-';
    
    // 获取其他人对文件的读、写、执行权限
    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 link_num = st.st_nlink;

    // 文件所有者
    char* file_user = getpwuid(st.st_uid)->pw_name;

    // 文件所属组
    char* file_group = getgrgid(st.st_gid)->gr_name;

    // 获取文件大小
    long int file_size = st.st_size; 

    // 获取修改时间
    char* temp_file_time = ctime(&st.st_mtime);

    // 去除时间字符串中的换行符
    char file_time[512] = {0};
    strncpy(file_time, temp_file_time, strlen(temp_file_time) - 1);

    char buf[1024];
    sprintf(buf, "%s %d %s %s %ld %s %s", perms, link_num, file_user, file_group, file_size, file_time, argv[1]);

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

    return 0;
}

效果:

x@DESKTOP-94KT1VQ:/mnt/c/Users/x/Pictures/code/lesson12$ gcc ls-l.c -o ll
x@DESKTOP-94KT1VQ:/mnt/c/Users/x/Pictures/code/lesson12$ ./ll 1.txt
-rwxrwxrwx 1 x x 12 Thu Feb 10 18:49:38 2022 1.txt
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值