#include <stdio.h> #include <conio.h> #include <alloc.h> #include <dir.h> #include <dos.h> #ifndef __cplusplus typedef enum { false, true } bool; #endif typedef struct { int max, count; char **str; } TStrList; // Cria lista de strings TStrList *InitStrList() { // Cria e inicializa a lista TStrList *strlist = (TStrList *)malloc(sizeof(TStrList)); if(strlist) { strlist->count = 0; strlist->max = 0; strlist->str = 0; } return strlist; } // Adiciona strings em uma lista de strings TStrList *AddStr(TStrList *strlist, char *str) { char **temp; // Se estourou o limite máximo, expande a lista if(strlist->count >= strlist->max) { temp = (char **)realloc(strlist->str, sizeof(char *) * (strlist->max + 10)); if(temp) { strlist->max += 10; strlist->str = temp; } else return strlist; } // Duplica a string e adiciona na lista strlist->str[strlist->count++] = strdup(str); return strlist; } // Procura arquivos a partir de um diretorio base void SearchFile(const char *path, const char *file) { TStrList *dirlist = InitStrList(); struct ffblk ffblk; bool found; char curpath[MAXPATH]; printf("Diretorio: %s\n", path); fnmerge(curpath, 0, path, "*.*", 0); // Cria uma lista dos subdiretórios found = findfirst(curpath, &ffblk, FA_DIREC) == 0; while(found) { if((strcmp(ffblk.ff_name, ".") != 0) && (strcmp(ffblk.ff_name, "..") != 0) && (ffblk.ff_attrib == FA_DIREC)) AddStr(dirlist, ffblk.ff_name); found = findnext(&ffblk) == 0; } fnmerge(curpath, 0, path, file, 0); // Procura os arquivos found = findfirst(curpath, &ffblk, FA_ARCH) == 0; while(found) { printf(" Arquivo: %s\n", ffblk.ff_name); found = findnext(&ffblk) == 0; } // Se encontrou subdiretórios, varre a lista recursivamente if(dirlist->count) { int c; for(c = 0; c < dirlist->count; c++) { fnmerge(curpath, 0, path, dirlist->str[c], 0); SearchFile(curpath, file); free(dirlist->str[c]); } free(dirlist->str); } free(dirlist); } void main(int ArgC, char *ArgV[]) { printf("FindFile - Copyright (c) 1999 VIC\n" " Programmed by Wenderson Teixeira\n"); switch(ArgC) { case 3: SearchFile(ArgV[1], ArgV[2]); break; case 2: SearchFile("", ArgV[1]); break; default: printf("Usage: findfile [path] filename\n" " path - drive:\\directory\\, initial path, no wildcards allowed\n" " filename - filename.ext, you may use wildcards\n"); } }