IO-day3-(read、write、stat.......)

作业

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <time.h>

char *get_filePermssion(mode_t m, char ptr[10]);
void Aget_filePermssion(mode_t m);

int main(int argc, const char *argv[])
{
    struct stat buf;
    if (stat("./1.txt", &buf) < 0)
    {
        perror("stat");
        return -1;
    }

    //=====================================
    // 第一种
    /*
    Aget_filePermssion(buf.st_mode);
    printf("\n");
    */
    // 第二种
    char str[10] = "";
    char *s = get_filePermssion(buf.st_mode, str);
    printf("%s\n", str);
    //=====================================

    // 文件的硬链接数
    printf("link:%ld\n", buf.st_nlink);

    // 文件所属用户
    printf("uid:%d\n", buf.st_uid);

    // 文件所属用户组
    printf("gid:%d\n", buf.st_gid);

    // 文件大小
    printf("size:%ld\n", buf.st_size);

    // 文件修改的时间
    time_t t;
    time(&t);
    struct tm *info = NULL;
    info = localtime(&buf.st_ctime);
    fprintf(stderr, "%2d月 %02d %02d:%02d\n",
            info->tm_mon + 1, info->tm_mday,
            info->tm_hour, info->tm_min);



    return 0;
}

char *get_filePermssion(mode_t m, char ptr[10]) // 注意这里ptr[10]只是为了提醒用户需要传入一个容量为10的组
{
    char per[] = "rwx";
    for (int i = 0; i < 9; i++)
    {
        if ((m & (0400 >> i) )== 0)
        {
            ptr[i]='-';
            continue;
        }

        ptr[i] = per[i % 3];
    }

    return ptr;
}

void Aget_filePermssion(mode_t m)
{
    for (int i = 0; i < 9; i++)
    {
        if ((m & (0400 >> i)) == 0)
        {
            putchar('-');
            continue;
        }

        switch (i % 3)
        {
        case 0:
            putchar('r');
            break; // 0、3、6
        case 1:
            putchar('w');
            break; // 1、4、7
        case 2:
            putchar('x');
            break; // 2、5、8
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值