I have written the following code in order to:
- Find begin and end of FTP user and password from a given URL (
ftp://[user[:password]@]host[:port]/url-path) - Allocate memory to hold this string
- Copy the sub-string into the allocated memory
Here is the code for your review:
static char * swd_parseUsrPass(void)
{
char *usr_pass = NULL;
char *url_str = NULL;
char *beginIndx = NULL;
char *endIndx = NULL;
int str_len = 0;
url_str = getURLaddr();
/* Find begin and end indexs */
beginIndx = url_str + strlen(FTP_PREFIX);
endIndx = strchr(beginIndx, '@');
if ((endIndx == NULL) || endIndx <= beginIndx) {
printf("Could not locate user and password in URL\n");
return NULL;
}
str_len = endIndx - beginIndx;
/* Allocate maximum possible string */
usr_pass = malloc((sizeof(char) * str_len) + 1);
if (usr_pass == NULL) {
printf("Failed to allocated memory!\n");
return NULL;
}
/* Copy user and password to new string */
strncpy(usr_pass, beginIndx, str_len);
*(endIndx + 1) = '\0';
return usr_pass;
}
@character can appear in theurl-path\$\endgroup\$url-pathmay not be there so you have to check for all the common mistakes.?,&,#,:\$\endgroup\$