Without the context in which you are using the function, it is a bit difficult to debug.
Sorry if I'm bypassing the question a little, but assuming you're still doing this, would it be easier to make sscanf do the parsing? (The fields you are after may be different from my guesses; this is just an example.)
Code:
#include <stdio.h>
struct sPackage
{
char name[32];
int version[4];
int rc;
char ext[4];
};
struct sPackage *package_parse(struct sPackage *parsed, const char *package)
{
char format[64];
sprintf(format, "%%%d[^-]-%%d.%%d.%%d.%%d_rc%%d.%%%ds",
(int)(sizeof(parsed->name) - 1), (int)(sizeof(parsed->ext) - 1));
if ( sscanf(package, format,
parsed->name,
&parsed->version[0],
&parsed->version[1],
&parsed->version[2],
&parsed->version[3],
&parsed->rc,
parsed->ext) == 7 )
{
return parsed;
}
return NULL;
}
void package_print(struct sPackage *parsed)
{
printf("name = %s\n", parsed->name);
printf("version: release = %d, ", parsed->version[0]);
printf("major = %d, ", parsed->version[1]);
printf("minor = %d, ", parsed->version[2]);
printf("build = %d\n", parsed->version[3]);
printf("rc = %d\n", parsed->rc);
printf("ext = %s\n", parsed->ext);
}
int main(void)
{
const char package[] = "no1willno-4.5.1.24_rc4.tar";
struct sPackage parsed;
if ( package_parse(&parsed, package) != NULL )
{
package_print(&parsed);
}
return 0;
}
/* my output
name = no1willno
version: release = 4, major = 5, minor = 1, build = 24
rc = 4
ext = tar
*/
Or are you going through the exercise of parsing out the 'package' string?
Or are 'package' strings not using a set format?
I'm just presenting a possible alternative.