使用系统函数实现ls -l 的一种方法

使用linux下的系统函数,可以实现ls -l 命令显示的内容。

参考测试源码:

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>


int main(int argc, char* argv[])
{
    if(argc < 2)
    {
        printf("./a.out filename\n");
        exit(1);
    }

    struct stat st;
    int ret = stat(argv[1], &st);
    if(ret == -1)
    {
        perror("stat");
        exit(1);
    }

    // 存储文件类型和访问权限
    char perms[11] = {0};
    // 判断文件类型
    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_IFSOCK:
            perms[0] = 's';
            break;
        case S_IFIFO:
            perms[0] = 'p';
            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 linkNum = st.st_nlink;
    // 文件所有者
    char* fileUser = getpwuid(st.st_uid)->pw_name;
    // 文件所属组
    char* fileGrp = getgrgid(st.st_gid)->gr_name;
    // 文件大小
    int fileSize = (int)st.st_size;
    // 修改时间
    char* time = ctime(&st.st_mtime);
    char mtime[512] = {0};
    strncpy(mtime, time, strlen(time)-1);

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

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

    return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <grp.h>
#include <pwd.h>


#define N 1024

char perm[] = {'r','w','x'};

int perm_macor[] = {S_IRUSR , S_IWUSR , S_IXUSR , \
					S_IRGRP , S_IWGRP , S_IXGRP , \
					S_IROTH , S_IWOTH , S_IXOTH };


int main(int argc, char const *argv[])
{
	DIR *dirp = NULL;
	int i = 0;
	struct dirent *rdir;
	struct stat st_buf;
	
	stat(argv[1], &st_buf);

	switch(S_IFMT & (st_buf.st_mode)){
		
		case S_IFSOCK:	printf("s");
						break;
		case S_IFLNK:	printf("l");
						break;
		case S_IFREG:	printf("-");
						break;
		case S_IFBLK:	printf("b");
						break;
		case S_IFDIR:   printf("d");
						break;
		case S_IFCHR:	printf("c");
						break;
		case S_IFIFO:	printf("p");
						break;
		default:
					break;
	}

	for(i = 0; i < sizeof(perm_macor)/sizeof(int);i++){
		if((st_buf.st_mode)&perm_macor[i]){
				printf("%c",perm[i%3]);
		}else{
				printf("-");
		}
	}

	printf(" %d ",st_buf.st_nlink);

	struct group *group = getgrgid(st_buf.st_gid);
	struct passwd *pwd = getpwuid(st_buf.st_uid);
	printf("%s ",group->gr_name);
	printf("%s ",pwd->pw_name);

	printf("%d %d ",st_buf.st_gid,st_buf.st_uid);
	printf("%lu ",st_buf.st_size);
	printf("%s",(char *)ctime(&(st_buf.st_ctime)));

	return 0;		
}

测试结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值