int foreach_path(const char *path, const struct stat *sp, int f) { ++n_files; n_bytes += sp->st_size; switch (f) { case FTW_F: pqueue_put(&pqb, (void *) strdup(path)); return 0; case FTW_D: return 0; case FTW_DNR: fprintf(stderr, "%s: %s: Can't read directory.\n", argv0, path); return 1; case FTW_NS: fprintf(stderr, "%s: %s: Can't stat object.\n", argv0, path); return 1; default: fprintf(stderr, "%s: %s: Internal error (invalid ftw code)\n", argv0, path); } return 1; }
void scan_root(char *dir, char* dirFullPath, int* depth, int maxDepth) // 定义目录扫描函数 { DIR *dp; // 定义子目录流指针 struct dirent *entry; // 定义dirent结构指针保存后续目录 struct stat statbuf; // 定义statbuf结构保存文件属性 char* path = NULL; int currentDepth; (*depth)++; if (*depth == maxDepth) { return; } currentDepth = *depth; if((dp = opendir(dir)) == NULL) // 打开目录,获取子目录流指针,判断操作是否成功 { puts("can't open dir."); return; } chdir (dir); // 切换到当前目录 while((entry = readdir(dp)) != NULL) // 获取下一级目录信息,如果未否则循环 { path = createFullName(dirFullPath, entry->d_name); lstat(entry->d_name, &statbuf); // 获取下一级成员属性 if(S_IFDIR & statbuf.st_mode) // 判断下一级成员是否是目录 { if (strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0) continue; // printf("%s\n", path); // 输出目录名称 scan_root(entry->d_name, path, depth, maxDepth); // 递归调用自身,扫描下一级目录的内容 *depth = currentDepth; } else if (S_IFREG & statbuf.st_mode) { //判断下一级成员是否是一般文件 //printf("%s\n", path); // 输出文件名称 char* copy = malloc((strlen(path) + 1) * sizeof(char)); strcpy(copy, path); pqueue_put(&pqb, (void *) copy); } //fprintf(stdout, "%s\n", path); // free(path); } chdir(".."); // 回到上级目录 closedir(dp); // 关闭子目录流 }
/* Interactively put/extract queue items. */ int main(int argc, char *const argv[]) { int rc = 0, cap = 0; long lval = 0; queue_t q = 0; char input[MAX_INPUT] = "\0", *p = NULL; FILE *fp = stdin; if (argc < 2) { fprintf(stderr, "Usage: %s capacity\n", argv[0]); return 1; } cap = atoi(argv[1]); if (cap < MIN_CAP || cap > MAX_CAP) { fprintf(stderr, "%s: invalid capacity: %s, stay within %d..%d\n", argv[0], argv[1], MIN_CAP, MAX_CAP); return 1; } q = pqueue_create((size_t)cap); if (-1 == q) { perror("pqueue_create"); return 1; } setvbuf(stdout, (char*)NULL, _IOLBF, 0); printf("Q(%d/%d) ready\n", cap, (int)pqueue_count(q)); for (;;) { if (NULL == fgets(input, sizeof(input)-1, fp)) break; if (NULL != (p = strrchr(input, '\n'))) *p = 0; if (NULL != (p = strrchr(input, '\r'))) *p = 0; if (0 == strcasecmp(input, "get")) { errno = 0; lval = (long) pqueue_get(q); if (-1 == lval && errno) { perror("pqueue_get"); continue; } printf("Q(%d/%d) >> %ld\n", cap, (int)pqueue_count(q), lval); } else if (0 == strcasecmp("dump", input) || 0 == strcasecmp("list", input)) { pqueue_dump(q, stdout); } else { lval = atol(input); if (0 == lval && strcmp("0", input)) { printf("Invalid input, try again\n"); continue; } rc = pqueue_put(q, (void*)lval); if (rc) { perror("pqueue_put"); continue; } printf("Q(%d/%d) << %ld\n", cap, (int)pqueue_count(q), lval); } } pqueue_free(q); printf("%s: exiting with rc=%d\n", argv[0], rc); return rc; }