Dateien/Verzeichnisse auflisten (Unicode)
-
Hi,
ich versuche gerade ein Programm zu schreiben, das alle Dateien und Unterordner in einem Ordner in eine html datei schreibt und auch mit Unicode zurecht kommt.
Allerdings hab ich so ein paar Probleme:
Ich benutze WinXP 32bit und GCC (MinGW) 3.4.2 und NTFS, daher die #defines
1. Die Funktion "swprintf" wird in meiner Headerdatei so "int swprintf (wchar_t *s, const wchar_t *format, ...);"
und auf vielen Internetseiten so "int swprintf(wchar_t *s, size_t n, const wchar_t *format, ...);" deklariert.
2. Bekomme ich beim 2. swprintf Aufruf (in main) einen Bufferoverflow bzw. einen SIGSEGV
3. Wenn ich das size_t weglasse, bekomme ich dafür beim fputws() in writeHeaderW() einen bufferoverflow
3. Wenn man das ganze mit Visual Studio kompiliert, läuft das ganze zwar, aber nach ein paar dateien bekommt man einen "Stackoverflow"
EDIT: Das Problem wurde jetzt (glaub ich) gelöst, indem ich den Speicher "mallociere"#include <windows.h> #include <stdio.h> #include <time.h> #define MAX_PATH_SIZE 32767 + 1 #define MAX_FILE_NAME 255 + 1 wchar_t ed2k; FILE *f; int parse_directory(wchar_t *directory) { wchar_t *currentDirectory = (wchar_t *)malloc(MAX_PATH_SIZE * sizeof(wchar_t)); wchar_t *nextDirectory = (wchar_t *)malloc(MAX_PATH_SIZE * sizeof(wchar_t)); HANDLE hFind = INVALID_HANDLE_VALUE; WIN32_FIND_DATAW lpFindFileData; fwprintf(f, L"Current directory: %s\n", directory); swprintf(currentDirectory, MAX_PATH_SIZE, L"%s\\*", directory); hFind = FindFirstFileW(currentDirectory, &lpFindFileData); if (hFind == INVALID_HANDLE_VALUE) { fwprintf(f, L"Invalid file handle. Error#: %lu.\n", GetLastError()); free(currentDirectory); free(nextDirectory); return 1; } do { if (!(lpFindFileData.cFileName[0] == L'.' && ((lpFindFileData.cFileName[1] == L'.' && lpFindFileData.cFileName[2] == L'\0') || lpFindFileData.cFileName[1] == L'\0'))) { if (lpFindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { fwprintf(f, L"Dir: %s\n", lpFindFileData.cFileName); swprintf(nextDirectory, MAX_PATH_SIZE, L"%s\\%s", directory, lpFindFileData.cFileName); parse_directory(nextDirectory); } else { fwprintf(f, L"File: %s\n", lpFindFileData.cFileName); } } } while (FindNextFileW(hFind, &lpFindFileData) != 0); free(currentDirectory); free(nextDirectory); FindClose(hFind); if (GetLastError() != ERROR_NO_MORE_FILES) { fwprintf(f, L"FindNextFile error. Error is %lu.\n", GetLastError()); return 1; } return 0; } void writeHeaderW(wchar_t *directory, wchar_t *cLocalTime) { fputws(L"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" L"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" " L"\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n" L"<html>\n" L"<head>\n", f); fwprintf(f, L"<title>Contents of: \"%s\" (\"%s\")</title>\n" L"<link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\"" L" title=\"Default-Style\" media=\"screen\" />\n" L"</head>\n" L"<body>\n", directory, cLocalTime); fwprintf(f, L"<h1>Contents of: \"%s\" (\"%s\")</h1>\n" L"<table>\n" L" <tr>\n" L" <th>Name</th>\n" L" <th>Filesize</th>\n" L" <th>InodeChangeTime</th>\n" L" <th>ModificationTime</th>\n" L" <th>AccessTime</th>\n", directory, cLocalTime); if (ed2k == L'1') { fputws(L" <th>ed2k-Hash</th>\n", f); } fputws(L" </tr>\n", f); } void writeFooterW() { fputws(L"</table>\n" L"</body>\n" L"</html>", f); } int main(int argc, char **argv) { if (argc != 3) { fprintf(stdout, "usage: %s [directory] [ed2k 0/1]\n", argv[0]); return 1; } wchar_t *directory = (wchar_t *) malloc(MAX_PATH_SIZE * sizeof(wchar_t)); wchar_t filename[MAX_FILE_NAME], cLocalTime[20]; swprintf(directory, MAX_PATH_SIZE, L"\\\\?\\%s", L"C:"); // argv[1] ed2k = L'0'; // argv[2] time_t tTime; time(&tTime); struct tm *tmLocalTime = localtime(&tTime); wcsftime(cLocalTime, 20, L"%Y-%m-%d_%H-%M-%S", tmLocalTime); swprintf(filename, MAX_FILE_NAME, L"file_list_%s_ed2k-%c_%s.htm", L"$Dir", ed2k, cLocalTime); wcsftime(cLocalTime, 20, L"%Y-%m-%d %H:%M:%S", tmLocalTime); if (f = _wfopen(filename, L"wb") == NULL) { fwprintf(stdout, L"Could not open file: %s\n", filename); free(directory); return 1; } writeHeaderW(directory, cLocalTime); if (parse_directory(directory) != 0) { fwprintf(stdout, L"Could not parse directory: %s\n", directory); } free(directory); writeFooterW(); fclose(f); return 0; }Ich hoffe ich hab da jetzt nicht zu viel Sourcecode gepostet, aber das Problem liegt irgendwie immer an einer anderen Stelle.
MFG
Long Long Double
-
Vielleicht hast Du einen Reparse-Point auf Deiner Festplatte... sowas kann schnell zu einer Endlosschleife führen...
-
Bei GCC:
Wenn das Programm überhaupt so weit laufen würde ... der erste Bufferflow kommt schon bevor ich die "parse_directory()" funktion aufrufe, wie ich oben geschrieben habe.Bei Visual Studio:
Mit VS geht seltsamerweise jetzt alles, also muss das ganze irgendwie am Compiler liegen, und nicht am Sourcecode.