How to get extended attributes of a file(UNIX/C)?

后端 未结 2 1903
旧时难觅i
旧时难觅i 2021-01-05 23:19

When I type ls -l in the command line, sometimes an @ or + symbol comes up alongside the file permissions(btw, I am on OS X), as shown

2条回答
  •  醉酒成梦
    2021-01-06 00:01

    Following is some code I scraped off of the official implementation of ls given by Apple you will find here. The code is long so do CMD + F and search for "printlong".

    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main () {
        acl_t acl = NULL;
        acl_entry_t dummy;
        ssize_t xattr = 0;
        char chr;
        char * filename = "/Users/john/desktop/mutations.txt";
    
        acl = acl_get_link_np(filename, ACL_TYPE_EXTENDED);
        if (acl && acl_get_entry(acl, ACL_FIRST_ENTRY, &dummy) == -1) {
            acl_free(acl);
            acl = NULL;
        }
        xattr = listxattr(filename, NULL, 0, XATTR_NOFOLLOW);
        if (xattr < 0)
            xattr = 0;
    
        if (xattr > 0)
            chr = '@';
        else if (acl != NULL)
            chr = '+';
        else
            chr = ' ';
    
        printf("%c\n", chr);
     }
    

    Depending on the file used, the output will be a blank, @, or + in exactly the same manner ls -l displays it. Hope this helps !

提交回复
热议问题