getting the end of an web address in c? -
say pass argument www.bbc.co.uk/news/world-us-canada-11893886 need separate www.bbc.co.uk /news/world-us-canada-11893886 http get
i have tried using strtok , strcat come across weird splits @ runtime. can www.bbc.co.uk fine using strtok( host, "/");
i have tried using combination of strtok , strcat try , rest of string first "/" output this... request: da-11893886 tempstring: news/world! host: www.bbc.co.uk path: news/world!da-11893886
if @ output, strangest part cuts out middle section. in case, "-us-cana"
the section of code attached below
// testing purposes printf("argv[1]: %s\n", argv[1] ); host = malloc(sizeof(argv[1])); strcpy(host, argv[1]); host = strtok(host, "/"); // request request = malloc(sizeof(argv[1]) + sizeof(char)*6); char *tok, *tempstring; tempstring = malloc(sizeof(argv[1])); tok = strtok( null, "\0"); while( tok ) { strcpy(tempstring, tok); printf("request: %s\n", request); request = strcat(tempstring, request); tok = strtok(null, "\0"); } printf("host: %s\n", host); printf("path: %s\n", request);
thanks looking on this. direction or link site can figure out how appreciated.
have modified code work way expecting
main(int argc, char *argv[]) { char *request,*host,*req; char *tok, *tempstring; printf("argv[1]: %s\n", argv[1] ); host = malloc(strlen(argv[1])); strcpy(host, argv[1]); host = strtok(host, "/"); tempstring = malloc(strlen(argv[1])); tok = strtok( null, "\0"); printf("sizeof(tok) %d\n",strlen(tok)); strncpy(tempstring, tok,strlen(tok)); while( tok ) { tok = strtok(null, "\0"); if (tok != null) { strncat(tempstring, tok,strlen(tok)); }else { break; } } request = tempstring; printf("host: %s\n", host); printf("path: %s\n", request); } ~
output
./tmp www.bbc.co.uk/news/world-us-canada-11893886/tmp.htmlargv[1]: www.bbc.co.uk/news/world-us-canada-11893886/tmp.html sizeof(tok) 38 host: www.bbc.co.uk path: news/world-us-canada-11893886/tmp.html bash-2.03$
~
Comments
Post a Comment