N
Noah Roberts
There is some windows specific code in this, but I believe the issue to
be something standard so...
The problem is that after attaching the name of the subdirectory I have
something like "d:\db\db-4.1.25\", which has 16 characters; but when it
arrives in the next call it looks more like "d:\db\db-4.1.25\(clubs)",
which has 17 characters. What is even more interesting is that the test
"if (dir[strlen(dir) - 1] != '\\')" fails when there is a clubs at the
end of the string!
Where am I screwing up?
NR - code follows
void search_directory(char *dir, char *match, void (*deal)(const
char*,const char *))
{
WIN32_FIND_DATA find_data;
HANDLE h_search;
int finished = 0;
char *search_string;
if (dir[strlen(dir) - 1] != '\\') dir[strlen(dir) - 1] = '\0';
else fprintf(stderr, "It is %c", dir[strlen(dir) - 1]);
search_string = malloc(strlen(dir) + 1);
sprintf(search_string, "%s*", dir);
fprintf(stderr, "%d", strlen(dir)); putchar('\n');
fprintf(stderr, search_string);
h_search = FindFirstFile(search_string, &find_data);
if (h_search == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Can't do stuff\n");
exit(1);
}
while (!finished)
{
if (!(find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
int begin = strlen(find_data.cFileName) - strlen(match);
if (!strcmp(find_data.cFileName + begin, match))
{
deal(dir, find_data.cFileName);
}
}
else if (strcmp(find_data.cFileName, ".") &&
strcmp(find_data.cFileName, ".."))
{
fprintf(stderr, "Going down...");
char *newdir = malloc(strlen(dir) + strlen(find_data.cFileName));
memset(newdir, 0, strlen(dir) + strlen(find_data.cFileName));
sprintf(newdir, "%s%s\\", dir, find_data.cFileName);
fprintf(stderr, "%d", strlen(newdir)); putchar('\n');
search_directory(newdir, match, deal);
}
if (!FindNextFile(h_search, &find_data))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
finished = 1;
}
else
{
fprintf(stderr, "BROKEN\n");
exit(1);
}
}
}
}
Output:
D:\progs>a.exe d:\ file.txt
It is \3
d:\*Going down...11
It is \11
d:\.xemacs\*Going down...8
It is \8
d:\argo\*Going down...15
It is \15
d:\convert-biz\*Going down...6
It is \6
d:\db\*Going down...16
It is \17
d:\db\db-4.1.25\?*Can't do stuff
be something standard so...
The problem is that after attaching the name of the subdirectory I have
something like "d:\db\db-4.1.25\", which has 16 characters; but when it
arrives in the next call it looks more like "d:\db\db-4.1.25\(clubs)",
which has 17 characters. What is even more interesting is that the test
"if (dir[strlen(dir) - 1] != '\\')" fails when there is a clubs at the
end of the string!
Where am I screwing up?
NR - code follows
void search_directory(char *dir, char *match, void (*deal)(const
char*,const char *))
{
WIN32_FIND_DATA find_data;
HANDLE h_search;
int finished = 0;
char *search_string;
if (dir[strlen(dir) - 1] != '\\') dir[strlen(dir) - 1] = '\0';
else fprintf(stderr, "It is %c", dir[strlen(dir) - 1]);
search_string = malloc(strlen(dir) + 1);
sprintf(search_string, "%s*", dir);
fprintf(stderr, "%d", strlen(dir)); putchar('\n');
fprintf(stderr, search_string);
h_search = FindFirstFile(search_string, &find_data);
if (h_search == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Can't do stuff\n");
exit(1);
}
while (!finished)
{
if (!(find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
int begin = strlen(find_data.cFileName) - strlen(match);
if (!strcmp(find_data.cFileName + begin, match))
{
deal(dir, find_data.cFileName);
}
}
else if (strcmp(find_data.cFileName, ".") &&
strcmp(find_data.cFileName, ".."))
{
fprintf(stderr, "Going down...");
char *newdir = malloc(strlen(dir) + strlen(find_data.cFileName));
memset(newdir, 0, strlen(dir) + strlen(find_data.cFileName));
sprintf(newdir, "%s%s\\", dir, find_data.cFileName);
fprintf(stderr, "%d", strlen(newdir)); putchar('\n');
search_directory(newdir, match, deal);
}
if (!FindNextFile(h_search, &find_data))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
finished = 1;
}
else
{
fprintf(stderr, "BROKEN\n");
exit(1);
}
}
}
}
Output:
D:\progs>a.exe d:\ file.txt
It is \3
d:\*Going down...11
It is \11
d:\.xemacs\*Going down...8
It is \8
d:\argo\*Going down...15
It is \15
d:\convert-biz\*Going down...6
It is \6
d:\db\*Going down...16
It is \17
d:\db\db-4.1.25\?*Can't do stuff