列出指定目录下所有可被其他用户执行的文件

来源:互联网 发布:单目标粒子群算法代码 编辑:程序博客网 时间:2024/06/05 02:51

知识点:

stat中的st_mode中包含了文件的访问权限位,共有9个:

S_IRUSR: 用户读

S_IWUSR: 用户写

S_IXUSR: 用户执行

S_IRGRP: 组成员读

S_IWGRP: 组成员写

S_IXGRP: 组成员执行

S_IROTH: 其他用户读

S_IWOTH: 其他用户写

S_IXOTH: 其他用户执行

使用st_mode & S_IXOTH 可以获得文件的其他用户是否可执行的权限位,如果该位为1,则该文件可以被其他用户执行,否则不能被执行。

代码:

#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<unistd.h>
#include<errno.h>
#include<dirent.h>
#include<sys/types.h>
#define SIZE 1024
int dir_run(char *path)

    DIR *dir;  
    dir = opendir(path);   
    if (dir == NULL)
    {       
        return -1;  
    }
    struct stat st;
    struct dirent *entry;
    char fullpath[SIZE];
    while((entry = readdir(dir)) != NULL)
    {      
        if((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0))
        {        
            continue;   
        }
        sprintf(fullpath, "%s/%s", path, entry->d_name);     
        if(lstat(fullpath, &st) != 0)
        {          
            continue;
        }
        if(st.st_mode&S_IXOTH)
        {
            printf("%s\n",entry->d_name);
        }
        if(S_ISDIR(st.st_mode))
        {       
            printf("%s目录下可被其他用户执行的文件:\n",entry->d_name);
            dir_run(fullpath);  
            printf("\n");
        }
    }   
    closedir(dir);
    return 0;
}
int main(int argc,char*argv[])
{
    if(argc!=2)
    {
        printf("参数不正确!正确格式:./main filepath\n");
        exit(1);
    }
    dir_run(argv[1]);
    return 0 ;
}

0 0
原创粉丝点击