获取目录文件权限

#include
#include
#include
#include
void get_filePermission(mode_t m)
{
    char b[]="rwx";
    for(int i=0;i<9;i++)
    {
        if((m&(0400>>i))==0)
        {
            putchar('-');
            continue;
        }
        printf("%c",b[i%3]);
    }
    return;
}
void get_fileType(mode_t m)
{
    if(S_ISREG(m))
        putchar('-');
    else if(S_ISDIR(m))
        putchar('d');
    else if(S_ISCHR(m))
        putchar('c');
    else if(S_ISBLK(m))
        putchar('b');
    else if(S_ISFIFO(m))
        putchar('p');
    else if(S_ISLNK(m))
        putchar('l');
    else if(S_ISSOCK(m))
        putchar('s');
}
void file_time(time_t t)
{
    struct tm* info=localtime(&t);
    printf("%d月 %d %d:%d",info->tm_mon+1,info->tm_mday,info->tm_hour,info->tm_min);
}
void file_uid(uid_t uid)
{
    struct passwd* pwd=getpwuid(uid);
    if(NULL==pwd)
    {
        ERR_MSG("getpwuid");
        return ;
    }
    printf("%s ",pwd->pw_name);
}
void file_gid(gid_t gid)
{
    struct group* grp=getgrgid(gid);
    if(NULL==grp)
    {
        ERR_MSG("getgrgid");
        return ;
    }
    printf("%s ",grp->gr_name);
}
void wjqx(mode_t Mode,nlink_t Nlink,uid_t Uid,gid_t Gid,off_t Size,time_t t)
{
    get_fileType(Mode);
    get_filePermission(Mode);
    printf(" %ld ",Nlink);
    file_uid(Uid);
    file_gid(Gid);
    printf("%ld ",Size);
    file_time(t);
    printf("\n");
}
int main(int argc, const char *argv[])
{
    char str[128]="";
    printf("请输入文件路径以及名字:\n");
    scanf("%s",str);
    struct stat buf;
    if(stat(str,&buf)<0)
    {
        ERR_MSG("stat");
        return -1;
    }
    if(S_ISDIR(buf.st_mode))
    {
        DIR *dp=opendir(str);
        if(NULL==dp)
        {
            ERR_MSG("opendir");
            return -1;
        }
        printf("%s目录打开成功\n",str);
        int i=1;
        while(1)
        {
            struct dirent* rp=readdir(dp);
            if(NULL==rp)
            {
                if(0==errno)
                {
                    printf("目录读取完毕\n");
                    break;
                }
                else
                {
                    ERR_MSG("readdir");
                    return -1;
                }
            }
            if((rp->d_name[0])!='.')
            {
                struct stat auf;
                char c[128]="";
                strcat(c,str);
                strcat(c,"/");
                strcat(c,rp->d_name);
                if(stat(c,&auf)<0)
                {
                    ERR_MSG("stat");
                    return -1;
                }
                wjqx(auf.st_mode,auf.st_nlink,auf.st_uid,auf.st_gid,auf.st_size,auf.st_ctime);
                printf("[%d]%s ",i++,rp->d_name);
            }
        }
        if(closedir(dp)<0)
        {
            ERR_MSG("closedir");
            return -1;
        }
        printf("目录关闭成功\n");
    }
    else
        wjqx(buf.st_mode,buf.st_nlink,buf.st_uid,buf.st_gid,buf.st_size,buf.st_ctime);
        return 0;
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             

你可能感兴趣的:(算法)