I need to extract the directory ****tion from a filename string (e.g.
c:\foo\bar.txt).
I came across the following splitpath function through a bit of
searching around. I figured that the second and third parameters are
pointers to character arrays which will be filled when the function
returns. But I can't understand how those pointers have to be passed to
it. I tried this which fails -
void main()
{
char **dirpart;
char **filepart;
splitpath("c:\\foo\\bar.txt", dirpart, filepart);
}
int splitpath(char *path, char **dirpart, char **filepart)
{
static char DOT[] = ".";
char *dpart;
char *fpart;
if ((fpart = strrchr(path, '/')) == NULL)
{
if ((dpart = strdup(DOT)) == NULL)
return -1;
if ((fpart = strdup(path)) == NULL)
{
free(dpart);
return -1;
}
}
else
{
if ((dpart = strdup(path)) == NULL)
return -1;
dpart[fpart - path] = '0';
if ((fpart = strdup(++fpart)) == NULL)
{
free(dpart);
return -1;
}
}
*dirpart = dpart;
*filepart = fpart;
return 0;
}
Somebody shed some light here?
..p