【问题标题】:get the permission of the file in a header of a tar with struct stat使用 struct stat 在 tar 的标头中获取文件的权限
【发布时间】:2013-09-22 14:58:45
【问题描述】:

我正在创建一个程序来管理 tar 文件。 我使用 struct stat 打印文件的权限:

printf("%d\n", buff->st_mode); /* I get a number like 33152 */

但我需要将权限放在我的 struct header 模式 [8] 中:

typedef struct  s_head
{
    char        name[100];
    char        mode[8];
    char        uid[8];
    char        gid[8];
    char        size[12];
    char        mtime[12];
    char        chksum[8];
    char        linkflag;
    char        linkname[100];
    char        magic[8];
    char        uname[32];
    char        gname[32];
    char        devmajor[8];
    char        devminor[8];
}               t_head;

当我打印模式时,我应该得到类似 000600 的东西(对于 rw-------) 但是我怎么能用正确的书写方式将我的 st_mode 转换为 char *

【问题讨论】:

    标签: c tar


    【解决方案1】:

    使用八进制。

    OP 的数字是 int,值为 33152。通过将其打印为八进制数,通常的“rwx”设置会更加明显:100600。

    // printf("%d\n", buff->st_mode); // OP gets a number like 33152
    printf("%o\n", buff->st_mode);    // 100600
    snprintf(s_head.mode, sizeof(s_head.mode), "%06o", buff->st_mode); // "100600"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-02
      • 2010-11-26
      • 2012-05-31
      • 2012-05-06
      • 2013-12-06
      • 1970-01-01
      • 2011-10-28
      相关资源
      最近更新 更多