要点:
realpath()
将目录路径转换为绝对路径, 借此将目录路径规范为末尾无"/"glob()
检索指定模式的文件. 通过flag为目录路径加上"/", 从而实现和普通文件区分basename()
获取文件名部分,从而实现比较不同形式的同一路径下的文件remove()
删除文件或目录ELF
文件的第2-4个字节是"ELF"
#include <linux/limits.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <libgen.h>
#include <string.h>
#include <glob.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#define BUF_SIZE 128
int not_itself( char* cmd , char* filename){
char* cmd_base = strdup(basename(cmd) );
char* file_base = strdup(basename(filename) );
size_t min = strlen(cmd_base) > strlen(file_base) ? strlen(file_base) : strlen(cmd_base) ;
int ret = strncmp(cmd_base , file_base , min) ;
free(cmd_base);
free(file_base);
return ret;
}
int is_elf_file(const char * filename){
int fd= -1 ;
if(filename[strlen(filename)-1] == '/' )
return 0;
if( (fd=open(filename,O_RDONLY)) < 0){
perror("open() ");
exit(EXIT_FAILURE);
}
char rd_buf[40];
if(read(fd,rd_buf,39) < 0) {
perror("read()");
exit(1);
}
rd_buf[39] = '0';
// printf(" %s ",rd_buf) ;
if(rd_buf[1] == 'E' &&
rd_buf[2] == 'L' &&
rd_buf[3] == 'F')
{
return 1;
}
close(fd);
return 0;
}
int main(int argc , char** argv){
char* dir_path;
glob_t result;
if(argc<2){
fprintf(stdout,"Usage: %s [<dir>] \n" , argv[0]);
exit(EXIT_FAILURE);
}
dir_path = malloc(PATH_MAX); // limits.h
if(!dir_path){
perror("malloc() ");
exit(EXIT_FAILURE);
}
realpath(argv[1],dir_path);
strncat(dir_path,"/*",3);
if(0!= glob(dir_path,GLOB_NOSORT | GLOB_MARK,NULL,&result)){
perror("glob() ");
exit(EXIT_FAILURE);
}
for(size_t i =0 ;i<result.gl_pathc ; i++){
printf("%s --> %d \n" ,result.gl_pathv[i], is_elf_file(result.gl_pathv[i]));
if(is_elf_file(result.gl_pathv[i]) && not_itself(argv[0],result.gl_pathv[i] )){
remove(result.gl_pathv[i]);
}
}
globfree(&result);
free(dir_path);
return EXIT_SUCCESS;
}