This is a function that is supposed to copy a token from a string into another string. When the second token in a string ie: "cd .." is requested, the start and end pointers point to some gibberish value.

I have stared at this for hours and for the life of me cannot find the problem. Any help would be great, if you need more info let me know.


This functions takes in a string s, a delimiter d, a token number n, and another string for the copy c. It copies the nth token as delimited by d in s to c.

Code:
int cp_delimited_token( char *string, char delimiter, int token_nr,
 			char *token, int max_token )
 {
 	char *start;
 	char *end;
 	int token_len, i;
 
 	// Get token start
 	start = string;
 	for ( i = 0; i < token_nr; i++ ) {
 		if (( start = strchr( string, delimiter )) == NULL ) {
 			return 0;
 		}
 	}
 
 	// Find end of token
 	if (( end = strchr( start, delimiter )) == NULL )
 		end = start + strlen(start);
 
 	token_len = end - start;
 
 	// Check bounds
 	if ( token_len > max_token )
 		token_len = max_token;
 
 	// Copy token, then add '\0'
 	strncpy( token, start, token_len );
 	*(token + token_len + 1 ) = '\0';
 
 	// Replace '\n' with '\0' to end of token
 	for ( i = 0; i < strlen( token ); i++ ) {
 		if ( token[i] == '\n' )
 			token[i] = '\0';
 	}
 	
 	return token_len;
 }
thanks in advance
Mitch