简体   繁体   中英

How to find all the file names in a specific directory without using “dirent.h” (visual studio 2017 windows) in C

I have a mid-semester project, to make an anti-virus, that get as main arguments a directory and a path: The path is to a file that contains a virus signature. The anti-virus's goal is to scan all the files in the given directory for the virus signature. I've finished the whole project, except the part that finds all the files in a specific given directory. Since this project is checked by an auto-feedback system that doesn't have any external libraries and operates on an unknown operating system (even my instructor don't know, it's the course's system), it has to be a cross-platform solution that doesn't rely on any libraries but stdio.h, stdlib.h, string.h and windows.h

Almost every tutorial and example of doing this is with HANDLE (which we haven't learned yet, and not supposed to use) or with . The problem is to find a way of doing it without dirent library.

I need some function that gets all the files in a directory

Something like this:

char** fileNames = somefunc(char* dir)

There is no way to do this with the standard library alone; standard C has no knowledge of directories. Every OS has a way of doing this, though, so you'll want to find the OS family for your checker (Windows, Linux, etc.) and look up how to do it for that OS.

Libraries built into the OS must be available, since they come with the system. You cannot run any program at all without them. (This is technically not true, but it's accurate enough to cover all interesting cases.)

EDIT: if your checker runs under Windows (judging from your comments), you'll want to use FindFirstFile and FindNextFile . They do rely on handles, though (like virtually all of the Windows API), so you'll have to look into that. (A handle is just an opaque pointer with special meaning to the Windows kernel. You can treat it pretty much like a void * . Don't get too fixated on the data type.)

EDIT 2: if you're looking for something to get you started:

char ** list_directory (const char * directory) {
  char * search_path = malloc(strlen(directory) + 3);
  strcpy(search_path, directory);
  strcat(search_path, "\\*");
  WIN32_FIND_DATA fd;
  SetLastError(0);
  HANDLE hsrch = FindFirstFile(search_path, &fd);
  free(search_path);
  if (hsrch == INVALID_HANDLE_VALUE) {
    if (GetLastError() != ERROR_FILE_NOT_FOUND) return NULL;
    SetLastError(0);
    return calloc(1, sizeof(char *)); // no files
  }
  char ** result = malloc(sizeof(char *));
  unsigned count = 0;
  while (!GetLastError()) {
    result[count] = malloc(strlen(fd.cFileName) + 1);
    strcpy(result[count ++], fd.cFileName);
    FindNextFile(hsrch, &fd);
    result = realloc(result, (count + 1) * sizeof(char *));
  }
  if (GetLastError() == ERROR_NO_MORE_FILES)
    SetLastError(0);
  FindClose(hsrch);
  result[count] = NULL;
  return result;
}

The dirent.h header is a POSIX implementation of traversing directories, meaning it runs only on UNIX-like systems. For Windows you should use the _findfirst and _findnext functions as specified here . Unlike FindFirstFile and FindNextFile these do not use a HANDLE .

For example:

intptr_t ffhandle;
struct _finddata_t ffinfo;

if ((ffhandle = _findfirst("c:\\path\\to\\dir\\*.*", &ffinfo)) == -1) {
    perror("findfirst failed");
    exit(1);
}
do {
    printf("found file %s\n", ffinfo.name);
} while (_findnext(ffhandle, &ffinfo) == 0);
_findclose(ffhandle);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM