|
发表于 2003-12-21 13:23:25
|
显示全部楼层
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <stdio.h>
- #include <dirent.h>
- #include <unistd.h>
- int main(int argc, char** argv)
- {
- struct stat file_stat;
- struct dirent * direntp;
- DIR * dirp;
- char tmp[256];
- long size=0;
- if(argc !=3)
- {
- printf("usage: %s directory size\n", argv[0]);
- return -1;
- }
- if(access(argv[1], R_OK ) == -1)
- {
- printf("%s permission denied\n", argv[1]);
- return -1;
- }
- if(stat(argv[1], & file_stat) == -1)
- {
- printf("read %s error\n", argv[1]);
- return -1;
- }
- if(S_ISDIR( file_stat.st_mode) != 1)
- {
- printf("%s is not a directory\n", argv[1]);
- return -1;
- }
- if( (dirp = opendir(argv[1])) == NULL)
- {
- printf("open directory %s error", argv[1]);
- return -1
- }
- size = atoi(argv[2]);
- if (size < 0)
- {
- printf("arg 2 size error\n");
- return -1;
- }
- while( (direntp = readdir(dirp)) != NULL )
- {
- if (strcmp(direntp->d_name, ".") == 0 \
- || strcmp(direntp->d_name, "..") == 0 )
- continue;
- // if(direntp->d_type == 4 )
- // continue;
-
- strncpy(tmp, argv[1], strlen(argv[1]) + 1 );
- if(strcmp(tmp, "/") != 0 )
- strncat(tmp, "/", 1);
- strncat(tmp, direntp->d_name, strlen(direntp->d_name) );
-
- if(access(tmp, R_OK ) == -1)
- {
- printf("file: %s permission denied\n", tmp);
- continue;
- }
- if(stat(tmp, &file_stat) == -1 )
- {
- printf("file: %s, read info error\n", tmp);
- continue;
- }
- if( S_ISDIR(file_stat.st_mode) == 1) continue;
- if (file_stat.st_size >= size)
- {
- printf("file: %s, size=%d\n", tmp, file_stat.st_size);
- }
- }
- }
复制代码
gcc -g main.c -o main
应该可以,你再完善一下 |
|