0

This is my code:

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <windows.h>
void filesearch(char* path,char* fname);


int main()
{
    filesearch("C:\\Users\\Admin Local\\Documents","test.txt");
    int choice;
    char folder[80],fname[80];
    printf("\nWhich directory will I search in?    ");
    gets(folder);
    printf("\nWhat is the filename of the required file?     ");
    gets(fname);
    filesearch(folder,fname);
}

void filesearch(char* folder,char* fname){
    char path[80];
    HANDLE filehandle;
    WIN32_FIND_DATA ffd;


    strcpy(path,folder);
    strcat(path,"\\*");
   // printf("%s\n",path);

    filehandle=FindFirstFile(path,&ffd);

    do{
        if(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
            if(strcmp(ffd.cFileName,".")==0){
                FindNextFile(filehandle,&ffd);
                continue;
            }
            char subpath[80];
            strcpy(subpath,folder);
            strcat(subpath,"\\");
            strcat(subpath,ffd.cFileName);
            filesearch(subpath,fname);
            continue;
        }
        if(strcmp(ffd.cFileName,fname)==0)
            printf("\n%s\\%s\n",folder,ffd.cFileName);
    }while(FindNextFile(filehandle,&ffd)!=0);
    FindClose(filehandle);
    return;
}

When I put a directory like: C:\Users\Admin Local\Documents and wildcard *.txt nothing happens. The program abruptly halts and windows shows error and quits.

Yet when I put: C:\Users\Admin Local\Documents and file name test.txt it outputs as it should.

I have a test call in line 10 to the function so that I can check if it properly works without having to worry about user input and errors in input handling.

the test call works fine.

Is there any problem in the code or is this a anti-virus issue and most importantly, how do I fix it?

dxiv
  • 16,984
  • 2
  • 27
  • 49
debanshu das
  • 141
  • 2
  • 9
  • 2
    `strcmp` won't recognize wildcards. – MikeCAT Oct 05 '20 at 17:54
  • 2
    Not tested, won't this program go `C:\Users\Admin Local\Documents\..\..\..\..\..\..\..\..\..` and so on? – MikeCAT Oct 05 '20 at 17:55
  • @MikeCAT Actually not, by either luck or a (risky) trick. When `"."` is encountered the code runs *two* `FindNextFile` iterations in a row, which happens to skip over `".."`. – dxiv Oct 05 '20 at 18:24

1 Answers1

0

First problem is that all those 80-character buffers are far too small for a safe search. They should be replaced with at least char folder[_MAX_PATH] and similar. Then the main issue is the following:

    if(strcmp(ffd.cFileName,fname)==0)
        printf("\n%s\\%s\n",folder,ffd.cFileName);

This compares a filename against a wildcard and will fail for true wildcards. It should rather be:

    if(PathMatchSpec(ffd.cFileName,fname))
        /* ... */

Using PathMatchSpec requires #include <shlwapi.h> and linking to shlwapi.lib.

dxiv
  • 16,984
  • 2
  • 27
  • 49
  • I got shlwapi.h but cannot find shlwapi.lib. Where can it be found in codeblocks mingw version 20.03? – debanshu das Oct 06 '20 at 07:15
  • 1
    @debanshudas You can refer to [this](https://stackoverflow.com/questions/5862757/how-do-i-link-to-a-library-with-codeblocks) thread. – Zeus Oct 06 '20 at 08:33
  • 1
    Hi,if this answer did help to you, please feel free to mark it to help people with the same issue. – Zeus Oct 07 '20 at 00:36