http://www.ousob.com --- Legacy Redefined // // spider_path() Written By : Paul Maker // Modified by me for this example // // cl %1.c /nologo /V1.0001 /O2 /Ot /G5 /Gd /TC /link // #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <io.h> #include <direct.h> #include <fcntl.h> #include <sys\stat.h> #include <stdlib.h> #include <string.h> #include <process.h> #include <errno.h> #include <time.h> #include <ctype.h> #include <memory.h> #define SPACE 0x20 #define TAB 0x09 #define ESC 0x1b #define IS_SAME(x,y) (strcmpi((x),(y)) == 0) #define BEEP printf("\a"); #define NOT_ALPHA(x) ( ((x<65) || (x>91 && x<97) || (x>123)) == 1 ) #define IS_DIR(x) ((access((x),0)) == 0) ? 1 : 0 #define WHITE 7 #define BLACK 0 #define BLUE 1 #define GREEN 2 #define CYAN 3 #define BRIGHT_GREEN 10 #define BRIGHT_CYAN 11 #define BRIGHT_RED 12 #define BRIGHT_MAGENTA 13 #define BRIGHT_YELLOW 14 #define BRIGHT_WHITE 15 // buffer for sprintf() char tcmd[1024]; // misc text message buffer char txt[1024]; // misc text message buffer char the_file[1024]; // bool for system() int sys_flag; // for current working directory char cwd[256]; // for temp working directory char tdir[256]; // prefix for zip file char file_prefix[256]; // for temp working directory(2) char new_dir[512]; // for searching OS path char path[1024]; // for time calc char time_buffr[128]; // for argv[1] char *look_for; // for argv[2] char *file_spec; // for argv[3] char *the_dir; // for search_file() char *lower_sp; char *lower_look_for; char s_f[32768]; char *sp; FILE *f; int line_num; int found_flag; long find_count = 0; long files_searched = 0; long lines_read = 0; // for tran() char tranRetVal[64]; char tranTmpBuffer[64]; // for _splitpath() char t_drive[_MAX_DRIVE + 1]; char t_dir[_MAX_DIR + 1]; char t_fname[_MAX_FNAME + 1]; char t_ext[_MAX_EXT + 1]; // function prototypes void spider_path(char *path); void setcolor(unsigned short fgcolor); void cls(void); char *uniquePrefix(void); void err(char *msg); void banner(void); void search_file(char *a_file); char *tran(long num); char *ltrim(char *aString); void msg(unsigned short tcolor, char *fmt, ...); void gotoxy(int x, int y); void clrscr(int x, int y); void get_files(char *a_dir); char *minSec(float seconds); void usage(void); //----------------------------------------------------------------------------------------- MAIN int main(int argc, char *argv[]) { // for operation timer time_t start, finish; // set em all null memset(s_f ,'\0',32768); memset(path ,'\0',1024); memset(t_drive ,'\0',_MAX_DRIVE + 1); memset(t_dir ,'\0',_MAX_DIR + 1); memset(t_fname ,'\0',_MAX_FNAME + 1); memset(t_ext ,'\0',_MAX_EXT + 1); // clear the screen cls(); // throw something on screen banner(); // check for arguments if( argc < 3 ){ usage(); } look_for = argv[1]; file_spec = argv[2]; if( !argv[3] ){ // get current working directory if( (getcwd(cwd,256) == NULL) ){ err("Unable to get current working directory"); } the_dir = cwd; }else{ the_dir = argv[3]; } // lower case dir strlwr(the_dir); // make sure path not root if( IS_SAME(the_dir,"c:\\\0") || IS_SAME(the_dir,"c:\0") || IS_SAME(the_dir,"\\\0") ){ err("Not enough memory for search from root directory!\0"); exit(1); } // make sure path exists if( !IS_DIR(the_dir) ){ msg(BRIGHT_YELLOW,"\n\nCannot find %s\n\n",strlwr(the_dir)); exit(1); } // show em what they typed msg(BRIGHT_GREEN,"%s \"%s\" %s %s\n\n",argv[0],look_for,file_spec,the_dir); // show em what they typed msg(BRIGHT_CYAN,"%s","Searching......"); // let's rock & roll start = time(NULL); get_files(the_dir); spider_path(the_dir); finish = time(NULL); // print total hit count printf("\n"); msg(BRIGHT_CYAN,"Hits : %s\n",tran(find_count)); msg(BRIGHT_CYAN,"Lines: %s\n",tran(lines_read)); msg(BRIGHT_CYAN,"Files: %s\n",tran(files_searched)); msg(BRIGHT_CYAN,"Time : %s\n", minSec(difftime(finish,start))); return 1; } // recursive function void spider_path(char *path) { struct _finddata_t ff; long fhandle; char spath[1024]; sprintf(spath,"%s\\*.*\0",path); if( (fhandle = _findfirst(spath,&ff)) != -1L ){ while( !_findnext(fhandle,&ff) ){ if( ff.attrib & _A_SUBDIR ){ if( strcmp(ff.name,"..") ){ sprintf(spath,"%s\\%s\0",path,ff.name); get_files(spath); spider_path(spath); } } } _findclose(fhandle); } } // look for filespec => *.c void get_files(char *a_dir) { struct _finddata_t c_file; long hFile; sprintf(txt,"%s\\%s\0",a_dir,file_spec); /* Find first file_spec file in current directory */ hFile = _findfirst(txt, &c_file); if( hFile != -1L ){ memset(the_file,'\0',1024); sprintf(the_file,"%s\\%s\0",a_dir,c_file.name); files_searched++; search_file(the_file); while(_findnext(hFile, &c_file) == 0){ sprintf(the_file,"%s\\%s\0",a_dir,c_file.name); files_searched++; search_file(the_file); } _findclose(hFile); } } // open file & look for text void search_file(char *a_file) { lower_look_for = strdup(look_for); found_flag = 0; line_num = 0; strlwr(lower_look_for); if( (f = fopen(a_file,"rb")) ){ while( (sp = fgets(s_f,32768,f)) ){ line_num++; lower_sp = strdup(sp); strlwr(lower_sp); if( strstr(lower_sp,lower_look_for) ){ if( found_flag == 0 ){ _splitpath(a_file,t_drive,t_dir,t_fname,t_ext); msg(BRIGHT_WHITE,"\n %s - ","LINE"); msg(BRIGHT_CYAN,"%s%s",strlwr(t_drive),strlwr(t_dir)); msg(BRIGHT_YELLOW,"%s%s\n",strlwr(t_fname),strlwr(t_ext)); found_flag = 1; } find_count++; printf("%6i - %s",line_num,ltrim(sp)); } } fclose(f); }else{ msg(BRIGHT_MAGENTA,"Cannot open %s\n",strlwr(a_file)); } free(lower_sp); free(lower_look_for); lines_read += line_num; } // clear console winfow void cls(void) { COORD coordScreen = { 0, 0 }; DWORD cCharsWritten; CONSOLE_SCREEN_BUFFER_INFO csbi; DWORD dwConSize; HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hConsole, &csbi); dwConSize = csbi.dwSize.X * csbi.dwSize.Y; FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten); GetConsoleScreenBufferInfo(hConsole, &csbi); FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten); SetConsoleCursorPosition(hConsole, coordScreen); } // change screen text color void setcolor(unsigned short fgcolor) { HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hCon,fgcolor); } // generates unique file name prefix yyyy-mm-dd_hhmmss_amsHtdocs.zip char *uniquePrefix(void) { long elapsetime; struct tm *greentime; memset(file_prefix,'\0',256); time(&elapsetime); greentime = gmtime(&elapsetime); sprintf(file_prefix,"%04d-%02d-%02d_%02d%02d%02d_\0",(greentime->tm_year+1900) ,greentime->tm_mon ,greentime->tm_mday ,greentime->tm_hour ,greentime->tm_min ,greentime->tm_sec); return file_prefix; } // print error messages void err(char *msg) { setcolor(BRIGHT_GREEN); fprintf(stdout,"\n\n%s\n\n",msg); setcolor(WHITE); exit(1); } // left trim a character string char *ltrim(char *aString) { while( (*aString == SPACE || *aString == TAB) ){ *aString++; } return(aString); } // like clipper transform9) char *tran(long num) { int i = 0, j = 0, len = 0, spaces = 0; char tranTmpBuffer[64]; char *ptr; long value; int radix = 10; memset(tranRetVal,0,64); memset(tranTmpBuffer,0,64); ptr = ltoa(num,tranTmpBuffer,radix); if( strlen(ptr) <= 3 ){ sprintf(tranRetVal,"%s\0",ptr); return(tranRetVal); } len = strlen(tranTmpBuffer); if( (len % 3) == 0 ){ spaces = (len+(len/3)) -1; }else{ spaces = (len+(len/3) ); } for( i = spaces, j = strlen(ptr); i >= 0; i-- ){ if( !( (spaces-i) % 4) && ( (spaces - i) != 0 ) ){ tranRetVal[i] = ','; }else{ tranRetVal[i] = ptr[j--]; } } return(tranRetVal); } // banner for intro screen void banner(void) { setcolor(BRIGHT_YELLOW); printf("cFind32 Version 1.045.c - From Mikey$oft - "); setcolor(BRIGHT_CYAN); printf("mMassing@OuSob.com\n\n"); setcolor(WHITE); } // formats/prints variable argument message void msg(unsigned short tcolor,char *fmt, ...) { va_list args; setcolor(tcolor); va_start( args, fmt ); vprintf( fmt, args ); va_end( args ); setcolor(WHITE); } // // Moves the cursor to x, y in console window // ie x=left\right y=top\bottom // void gotoxy(int x, int y) { COORD point; point.X = x; point.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point); } // clear screen from x,y down void clrscr(int x, int y) { COORD coordScreen = { x, y }; DWORD cCharsWritten; CONSOLE_SCREEN_BUFFER_INFO csbi; DWORD dwConSize; HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hConsole, &csbi); dwConSize = csbi.dwSize.X * csbi.dwSize.Y; FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten); GetConsoleScreenBufferInfo(hConsole, &csbi); FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten); SetConsoleCursorPosition(hConsole, coordScreen); } // calc operation time char *minSec(float seconds) { int minutes, remainder; // make em null memset(time_buffr,'\0',128); minutes = (int)seconds / 60; if( minutes > 0 ){ remainder = (int)seconds % 60; sprintf(time_buffr,"%d minute(s) %d second(s)\0",minutes,remainder); }else{ remainder = (int)seconds; sprintf(time_buffr,"%d second(s)\0",remainder); } return(time_buffr); } // show usage info void usage(void) { setcolor(BRIGHT_WHITE); printf("Usage: "); setcolor(BRIGHT_CYAN); printf("cFind32 \"future version\" *.ini [c:\\php] > xyz.txt\n\n"); setcolor(BRIGHT_WHITE); printf("If optional path"); setcolor(BRIGHT_CYAN); printf(" [c:\\php] "); setcolor(BRIGHT_WHITE); printf("not included, only current folder\nand below will be searched\n\n"); printf("If not piped to file,"); setcolor(BRIGHT_CYAN); printf(" > xyz.txt "); setcolor(BRIGHT_WHITE); printf("you won't be able to read it\n\n"); setcolor(WHITE); exit(1); }