传入一个文件,显示文件详细信息

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

//传入一个文件,显示文件详细信息
//-rwxrwxrwx 1 root root      1723 Nov 30 09:49 27_lstat.c
//权限    链接数 所有者 用户组 文件容量 修改日期 文件名
//文件所有者权限 文件所属用户组的权限 其他人对此文件的权限

#if 0
struct stat {
               dev_t     st_dev;     /* ID of device containing file */
               ino_t     st_ino;     /* inode number */
               mode_t    st_mode;    /* protection 权限*/
               nlink_t   st_nlink;   /* number of hard links 链接数*/
               uid_t     st_uid;     /* user ID of owner 所有者ID*/
               gid_t     st_gid;     /* group ID of owner 用户组ID*/
               dev_t     st_rdev;    /* device ID (if special file) */
               off_t     st_size;    /* total size, in bytes 文件容量*/
               blksize_t st_blksize; /* blocksize for file system I/O */
               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
               time_t    st_atime;   /* time of last access */
               time_t    st_mtime;   /* time of last modification */
               time_t    st_ctime;   /* time of last status change 修改日期*/
           };
#endif


#if 0
    struct group *getgrgid(gid_t gid);
struct group {
               char   *gr_name;       /* group name */
               char   *gr_passwd;     /* group password */
               gid_t   gr_gid;        /* group ID */
               char  **gr_mem;        /* group members */
           };
#endif

int main(int argc,char *argv[])
{
    struct stat buf;
    if(lstat(argv[1],&buf) < 0)//lstat获取文件信息
    {
        perror("lstat failed");//需要加""
        exit(1);
    }
    //打印文件类型
    if(S_ISREG(buf.st_mode))
        printf("-");
    if(S_ISDIR(buf.st_mode))
        printf("d");
    if(S_ISCHR(buf.st_mode))
        printf("c");
    if(S_ISBLK(buf.st_mode))
        printf("b");
    if(S_ISFIFO(buf.st_mode))
        printf("p");
    if(S_ISLNK(buf.st_mode))
        printf("l");
    if(S_ISSOCK(buf.st_mode))
        printf("s");

    //打印文件权限    -rwxrwxrwx 421421421 
    char str[] = "xwr-";
    int i;
    for(i=8;i>0;i--)
    {
        if(buf.st_mode & 1 << i)//左移
            printf("%c",str[i%3]);
        else 
            printf("%c",str[3]);
    }

    //打印文件链接数
    printf(" %d ",buf.st_nlink);

    //打印文件所有者
    printf("%s ",getpwuid(buf.st_uid)->pw_name);//

    //打印文件用户组
    printf("%s ",getgrgid(buf.st_gid)->gr_name);

    //打印文件容量
    printf("%ld ",buf.st_size);

    //打印修改日期
    struct tm *tp;
    tp = localtime(&buf.st_ctime);//
    printf("%d-%d-%d %d:%d:%d\n",tp->tm_year+1900, tp->tm_mon+1, \
                        tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);

    //打印文件名
    printf("-----filename is %s-----\n",argv[1]);

    return 0;

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值