简体   繁体   English

如何为我的ls-al c程序添加权限?

[英]How do I add permissions to my ls-al c program?

I am writing code to implement the ls-al command using c program and I have gotten my code to implement it without printing permissions but I want to implement the permissions as well but cant figure out how. 我正在编写使用c程序实现ls-al命令的代码,我已经获得了无需打印权限即可实现它的代码,但是我也想实现这些权限,但无法弄清楚如何实现。 any suggestions? 有什么建议么? my code is below 我的代码如下

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <pwd.h>

// Last Modified
        time_t t = my_stat.st_mtime;
        localtime_r(&t, &lt);
        char timebuf[80];
        strftime(timebuf, sizeof(timebuf), "%c", &lt);
        if (pwd != 0) {
            printf("%s \t %ld \t %s \t %s", pwd->pw_name, (long)my_stat.st_size, timebuf, current_directory->d_name);
            printf("\n");
        } else {
            printf("%d \t %ld \t %s \t %s", my_stat.st_uid, (long)my_stat.st_size, timebuf, current_directory->d_name);
            printf("\n");
        }
    }
    closedir(directory);
    return 0;
}
int main(int argc, char* argv[]) {
    if ( argc == 1 ) {
        return list_dir ( "." );
    } else {
        int ret = 0;
        for (int i = 1; i < argc; i += 1 ) {
            if ( list_dir ( argv[i] ) != 0 ) {
                ret = 1;
            }
        }
        return ret;
    }
}

I have ben trying for a long time to be able to add permissions to this code but I seem to be getting stuck and am out of ideas here 我已经尝试了很长时间才能为该代码添加权限,但是我似乎陷入了困境,并且在这里没有主意

the output of my code is: 我的代码的输出是:

kev      0   Thu Jun 20 13:39:49 2019    .
kev      0   Thu Jun 20 13:39:46 2019    ..
kev      24147   Thu Jun 20 12:24:40 2019    CMakeCache.txt
kev      0   Thu Jun 20 13:39:53 2019    CMakeFiles
kev      1426    Thu Jun 20 12:24:41 2019    cmake_install.cmake
kev      5160    Thu Jun 20 12:24:41 2019    Makefile

the expected output is: 预期的输出是:

rw-r--r--  1 kev     0   Thu Jun 20 13:39:49 2019    .
rw-r--r--  1 kev     0   Thu Jun 20 13:39:46 2019    ..
-rw-------       24147   Thu Jun 20 12:24:40 2019    CMakeCache.txt
rw-r--r--   kev      0   Thu Jun 20 13:39:53 2019    CMakeFiles
-rw-------  kev      1426    Thu Jun 20 12:24:41 2019 cmake_install.cmake
rw-r--r-- kev    5160    Thu Jun 20 12:24:41 2019    Makefile

You will want to make use of the mode_t st_mode field of your struct stat . 您将要使用struct statmode_t st_mode字段。 See stat(2): 参见stat(2):

The stat structure 统计结构

All of these system calls return a stat structure, which contains the following fields: 所有这些系统调用均返回一个stat结构,其中包含以下字段:

 struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* file type and mode */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device ID (if special file) */ off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* blocksize for filesystem I/O */ blkcnt_t st_blocks; /* number of 512B blocks allocated */ /* Since Linux 2.6, the kernel supports nanosecond precision for the following timestamp fields. For the details before Linux 2.6, see NOTES. */ struct timespec st_atim; /* time of last access */ struct timespec st_mtim; /* time of last modification */ struct timespec st_ctim; /* time of last status change */ #define st_atime st_atim.tv_sec /* Backward compatibility */ #define st_mtime st_mtim.tv_sec #define st_ctime st_ctim.tv_sec }; 

[...] [...]

The file type and mode (st_mode) 文件类型和模式(st_mode)

POSIX refers to the st_mode bits corresponding to the mask S_IFMT (see below) as the file type, the 12 bits corresponding to the mask 07777 as the file mode bits and the least significant 9 bits (0777) as the file permission bits. POSIX将与掩码S_IFMT对应的st_mode位(请参见下文)称为文件类型,将与掩码07777对应的12位称为文件模式位,并将最低有效9位(0777)作为文件许可位。

The following mask values are defined for the file type of the st_mode field: 为st_mode字段的文件类型定义了以下掩码值:

  S_IFMT 0170000 bit mask for the file type bit field S_IFSOCK 0140000 socket S_IFLNK 0120000 symbolic link S_IFREG 0100000 regular file S_IFBLK 0060000 block device S_IFDIR 0040000 directory S_IFCHR 0020000 character device S_IFIFO 0010000 FIFO 

[...] [...]

The following mask values are defined for the file mode component of the st_mode field: 为st_mode字段的文件模式组件定义了以下掩码值:

  S_ISUID 04000 set-user-ID bit S_ISGID 02000 set-group-ID bit (see below) S_ISVTX 01000 sticky bit (see below) S_IRWXU 00700 owner has read, write, and execute permission S_IRUSR 00400 owner has read permission S_IWUSR 00200 owner has write permission S_IXUSR 00100 owner has execute permission S_IRWXG 00070 group has read, write, and execute permission S_IRGRP 00040 group has read permission S_IWGRP 00020 group has write permission S_IXGRP 00010 group has execute permission S_IRWXO 00007 others (not in group) have read, write, and execute per‐ mission S_IROTH 00004 others have read permission S_IWOTH 00002 others have write permission S_IXOTH 00001 others have execute permission 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM