Linux下用c语言实现whereis.
2024-08-30 23:07:33
简单的一个whereis的实现,代码如下:
#include <stdio.h>
#include <errno.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
void dir_scan(char *path, char *file, char * fileToSearch);
int count = ; int main(int argc, char *argv[])
{
struct stat s;
if(argc != ){
printf("error argu!");
exit();
}
if(lstat(argv[], &s) < ){
printf("lstat error!");
exit();
}
if(!S_ISDIR(s.st_mode)){
printf("This is not a dir name!\n");
exit();
}
dir_scan("", argv[], argv[]);
exit();
} void dir_scan(char * path, char * file, char * fileToSearch)
{
struct stat s;
DIR * dirPtr;
struct dirent * dtPtr;
char dirName[];
memset(dirName, , * sizeof(char));
strcpy(dirName, path); if(lstat(file, &s) < ){
printf("asdasdad");
// fprintf(stdout, "The reason is %s\t ", strerror(errno));
exit(());
}
if(S_ISDIR(s.st_mode)){
strcat(dirName, file);
strcat(dirName, "/");
// printf("now the dir name is %s\n", file);
if((dirPtr = opendir(file)) == NULL){
printf("open dir error!\n");
exit();
printf("The count now is %d\n", count);
}
if(chdir(file) < ){
printf("chdir error!\n");
exit();
}
while((dtPtr = readdir(dirPtr)) != NULL){
if(dtPtr->d_name[] == '.')
continue;
dir_scan(dirName, dtPtr->d_name, fileToSearch);
}
if(dirPtr != NULL)
closedir(dirPtr);
if(chdir("..") < ){
printf("chdir error!\n");
exit();
}
}else{
if(strstr(file, fileToSearch) != NULL && (!strstr(file, "."))){
printf("%s%s\n", dirName, file);
// printf("adasdadad\n");
}
count++;
}
}
最新文章
- Eclipse 使用技巧
- *POJ 1222 高斯消元
- iar 错误解决
- Tomcat启动后快逸报表报错的解决方法
- [原]如何在Android用FFmpeg+SDL2.0解码声音
- MySQL添加字段和删除字段
- 关于C++对汉字拼音的处理——终结篇
- 网页增重不可控?试试 OneAPM Cloud Test
- android-意图Intent
- PHPCMS快速建站系列之自定义分页函数
- docker 常用命令和常用容器启动
- List的分组,求和,过滤操作
- Froms 认证 二级域名共享session登录凭证
- 解决代理池的问题AttributeError: &#39;int&#39; object has no attribute &#39;items&#39;
- 大众点评selfxss结合两个csrf变废为宝(已修复,故公开,不涉及真实参数)
- H5自动准备杂记
- Ubuntu 14.04 LTS 安装Docker(转)
- 大数据入门第二十二天——spark(二)RDD算子(2)与spark其它特性
- 洛谷P3613 睡觉困难综合征(LCT,贪心)
- R 语言 相关入门资料