9.7作业

将指定目录下所有文件的属性显示出来,类似 ls -l 一个指定目录

        打开目录,

        读取目录中的文件名

        将路径和文件名拼接

        传入给stat获取文件属性

y@DESKTOP-1DH5HNK:~/23071/IO/03day/stat$ ./fun /
drwxr-xr-x       4       root    root    4096   2023-08-16 18:41 home
drwxr-xr-x       2       root    root    4096   2023-08-14 14:43 srv
drwxr-xr-x       98      root    root    4096   2023-09-07 11:34 etc
drwxr-xr-x       2       root    root    4096   2023-08-14 14:43 opt
drwx------       2       root    root    4096   2023-08-14 14:43 root
drwxr-xr-x       20      root    root    4096   2023-08-14 14:51 lib
drwxr-xr-x       6       root    root    4096   2023-08-14 14:43 mnt
drwxr-xr-x       10      root    root    4096   2023-08-14 14:43 usr
drwxr-xr-x       2       root    root    4096   2023-08-14 14:43 media
drwxr-xr-x       2       root    root    4096   2023-08-14 14:43 lib64
dr-xr-xr-x       11      root    root    0      2023-09-07 08:49 sys
drwxr-xr-x       8       root    root    2720   2023-09-07 08:49 dev
drwxr-xr-x       2       root    root    12288  2023-08-14 14:50 sbin
drwxr-xr-x       2       root    root    4096   2023-08-14 14:43 boot
drwxr-xr-x       2       root    root    4096   2023-08-14 14:43 bin
drwxr-xr-x       9       root    root    180    2023-09-07 11:34 run
-rwxr-xr-x       4       root    root    644432 2023-08-15 08:55 init
dr-xr-xr-x       187     root    root    0      2023-09-07 08:49 proc
drwxr-xr-x       2       root    root    4096   2023-08-14 14:43 snap
drwxrwxrwx       5       root    root    4096   2023-09-07 19:48 tmp
drwxr-xr-x       13      root    root    4096   2023-08-14 14:43 var
drwx------       2       root    root    16384  2019-04-11 00:35 lost+found
y@DESKTOP-1DH5HNK:~/23071/IO/03day/stat$ ./fun /home/
drwxr-xr-x       9       y       y       4096   2023-09-06 22:56 y
drwxr-xr-x       3       ubuntu  ubuntu  4096   2023-08-16 19:37 ubuntu
y@DESKTOP-1DH5HNK:~/23071/IO/03day/stat$ ./fun /usr/
drwxr-xr-x       2       root    root    4096   2023-08-14 14:43 src
drwxr-xr-x       33      root    root    4096   2023-09-05 10:27 include
drwxr-xr-x       2       root    root    4096   2023-08-14 15:06 games
drwxr-xr-x       69      root    root    4096   2023-08-14 15:06 lib
drwxr-xr-x       136     root    root    4096   2023-09-07 11:34 share
drwxr-xr-x       2       root    root    4096   2023-08-14 15:00 sbin
drwxr-xr-x       2       root    root    28672  2023-09-07 11:34 bin
drwxr-xr-x       10      root    root    4096   2023-08-14 14:43 local
y@DESKTOP-1DH5HNK:~/23071/IO/03day/stat$ ./fun /home/y/23071/
-rw-r--r--       1       y       y       0      2023-08-15 10:16 2.c
-rw-r--r--       2       y       y       187    2023-08-15 11:04 1.c
-rwxrwxrwx       1       root    y       0      2023-08-15 10:41 11
-rw-r--r--       1       y       y       0      2023-08-15 10:16 3.c
drwxr-xr-x       10      y       y       4096   2023-09-01 10:29 数据结构
drwxr-xr-x       6       y       y       4096   2023-08-18 15:00 C高级
-rw-r--r--       2       y       y       187    2023-08-15 11:04 10.c
drwxr-xr-x       7       y       y       4096   2023-09-07 09:04 IO
-rwxr-xr-x       1       y       y       8296   2023-08-15 11:04 a.out
y@DESKTOP-1DH5HNK:~/23071/IO/03day/stat$ 
#include "stat.h"

int main(int argc, const char* argv[])
{
    char path[256];
    strcpy(path, argv[1]);
    DIR* dp = opendir(path);
    if (NULL == dp)
        PRINT_ERR("opendir");

    struct dirent* dr = NULL;
    while (1) {
        dr = readdir(dp);
        if (NULL == dr)
            break;
        if (*(dr->d_name) != '.')
            show_file(path, dr->d_name);
    }

    closedir(dp);
    return 0;
}
#include "stat.h"

char get_filetype(mode_t m)
{
    switch (m & __S_IFMT) {
    case __S_IFBLK:
        return 'b';
    case __S_IFCHR:
        return 'c';
    case __S_IFDIR:
        return 'd';
    case __S_IFIFO:
        return 'p';
    case __S_IFLNK:
        return 'l';
    case __S_IFREG:
        return '-';
    case __S_IFSOCK:
        return 's';
    default:
        return -1;
    }
}
void get_mode(char str[10], mode_t m)
{
    int i;
    for (i = 0; i < 9; i++) {
        if (m & (0400 >> i)) {
            if (i % 3 == 0)
                str[i] = 'r';
            if (i % 3 == 1)
                str[i] = 'w';
            if (i % 3 == 2)
                str[i] = 'x';
        } else
            str[i] = '-';
    }
    str[i] = 0;
}

int show_file( char* path,const char* fname)
{
    struct stat buf;
    char pathname[256];
    sprintf(pathname,"%s%s",path,fname);
   // printf("%s\n",path);
    if (stat(pathname, &buf) < 0)
        PRINT_ERR("stat");

    char str[10];
    get_mode(str, buf.st_mode);
    printf("%c%s\t", get_filetype(buf.st_mode), str);

    printf(" %ld\t", buf.st_nlink);

    struct passwd* u = getpwuid(buf.st_uid);
    printf(" %s\t", u->pw_name);

    struct group* g = getgrgid(buf.st_gid);
    printf(" %s\t", g->gr_name);

    printf(" %ld\t", buf.st_size);

    struct tm* tt = NULL;
    tt = localtime(&buf.st_ctime);
    printf("%d-%02d-%02d %02d:%02d %s\n", tt->tm_year + 1900, tt->tm_mon + 1,
        tt->tm_mday, tt->tm_hour, tt->tm_min, fname);
    return 0;
}
#ifndef __STAT_H__
#define __STAT_H__
#include 

void get_mode(char str[10], mode_t m);
char get_filetype(mode_t m);
int show_file( char* path,const char* fname);


#endif

你可能感兴趣的:(linux)