This is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
int parse (char **buf, const char *src, const char *ext)
{
const char *cpy = src; /* make a copy */
int len = 0; /* total size of buf */
int pos = 0; /* grab the last dot found */
int index = 0; /* current iterator character */
int stop = 1; /* stop the loop */
while (*cpy)
{
/* find the last dot in the string */
if (*cpy == '.')
{
pos = index; /* save the position */
}
cpy++; /* next char */
index++; /* len of the string */
}
len = pos; /* save the position of the last dot in the string */
index = 0;/* reset the counter */
for (;*src || !stop;pos++,src++)
{
/* now locate the position of the dot to ... */
if (pos == index)
{
/* copy the new file extension */
while (*ext)
{
**buf = *ext;
ext++;
*buf++;
len++;
}
**buf++ = '\0'; /* add null terminated char */
stop = 0; /* since we done, stop the loop */
}
**buf = *src; /* copying ... */
*buf++;
}
return len; /* return len of bufy */
}
int main (int argc, char *argv[])
{
char *bufy = (char *) malloc (256);
int ret = parse (&bufy, "/home/joel/foo.flv", "mp3");
printf ("New path: %s\n", bufy);
free (bufy);
return 0;
}
"bufy", should return something like: /home/joel/foo.mp3, but instead it crashes
Any ideas?