I have been going crazy trying to get this code to work for days now.
all i want to do is compare two strings.
when i do a strcmp, it throws a segmentation falult
i could really use some help with my code.
my main function looks like this:
Code:
int main(int argc, char *args[]){
//char ch1[] = "word";
//char ch2[] = "(,)";
//char * result;
//printf("here\n");
//strcat(ch1,ch2);
//printf("printf: %s\n", ch1);
///*
start_database();
char buffer[250];
char *p;
printf("please enter in a command:");
while(fgets(buffer, sizeof(buffer), stdin)!=NULL){
printf("%s\n",run_database_command(buffer));
printf("\n");
printf("please enter in a command:");
}
//*/
return 0;
}
and my code looks like this... look for the comment where it fails:
Code:
char * run_database_command(char * command){
if(!db_on) return "Database not on";
int num;
char c1[100]= malloc(strlen(command));
char ** tokens;
if(strpos(command, ",")==-1 && strpos(command, "(")==-1){
snprintf(c1, sizeof(c1),"%s",command);
}
else{
tokens = split( command, &num, ",()");
snprintf(c1, sizeof(c1),"%s",tokens[0]);
}
printf("crazy %s\n", c1);
printf("crazy %s\n", strcmp(c1,"help")); // FAILS HERE!!!!!!!!!!!!!!!!!!!!!!
///*
if(! strcmp(c1, "help")){
return "--commands--\n help - shows help screen\n show - shows contents of database\n insert(title, description, quanty) - this commands inserts a item into the database. \n delete(id) - deletes item by id. use show or search to get id. search(title) - search for exact match title. \n update(id, feild, value) - where feild is title description or quanty. \n to exit, <clt> + c\n";
}
else if(! strcmp(c1, "show")){
return showdata();
}
else if(! strcmp(c1, "insert")){
return insert(tokens[1],tokens[2],tokens[3]);
}
else if(! strcmp(c1, "update")){
return update(atoi(tokens[1]),tokens[2],tokens[3]);
}
else if(! strcmp(c1, "delete")){
return deleted(atoi(tokens[1]));
}
else if(! strcmp(c1, "search")){
return search(tokens[1]);
}
for(int i=0;i<num;i++) free(tokens[i]);
free(tokens);
free(command);
free(num);
free(c1);
return "something went wrong.. type help";
//*/
}
please let me know if you need to see more code