I seem to be getting a seg fault when using strcmp: weirdly the value seems to be the right value stored but still causes a seg fault. Does anyone have any ideas why this is my code?
Say for example I have 'xx' as the value I want to unalias in the 2 lines after the for loop they both = 'xx' but then this line causes a seg fault if (alias_array[i].alias != NULL && strcmp(alias_array[i].alias, arguments[1]) == 0) {
typedef struct alias {
char alias[50];
char command[50];
} alias;
alias alias_array[MAX_ALIAS_LEN];
void unalias_command(char *arguments[50]) {
char alias_name[50];
strcpy(alias_name, arguments[1]);
printf("args[1] %s\n", *arguments);
printf("Len of args = %ld", strlen(arguments[1]));
//if(strcmp(alias_name, NULL) != 0) {
if (arguments != NULL) {
int found = 0;
for (int i = 1; i < MAX_ALIAS_LEN; i++) {
printf("current name = %s\n", alias_array[1].alias);
printf("current argument = %s\n", arguments[1]);
if (alias_array[i].alias != NULL && strcmp(alias_array[i].alias, arguments[1]) == 0) {
// if alias is found and matches then we can use the free() function
found = 1;
strcpy(alias_array[i].alias, NULL);
strcpy(alias_array[i].command, NULL);
break;
}
}
if (found == 1) {
printf("Alias, %s has been removed as an alias\n", arguments);
} else {
printf("No alias with name %s\n", arguments[1] );
}
} else {
printf("Error: command 'unalias' requires 1 argument"); }
}
...
char *args[50] = {""}
unalias_command(args)