C kernel string concat and comparison

c, kernel, string

Solution

Why can't you compare with a literal? Change your code to the following:

unsigned char current_cmd[100];

char tmp[] = {kbdus[scancode], '\0'};
if (scancode != 0x1C) // enter key
    strcat((char*)current_cmd, tmp);

if (strcmp((const char*)cmd, "help") == 0)
    printf("You can't do anything yet.\n");

current_cmd[0] = '\0';

Problem

For reference the implementation I have of strcat and strcmp is: ``` char * strcat(char *dest, const char *src) { int i,j; for (i = 0; dest[i] != '\0'; i++) ; for (j = 0; src[j] != '\0'; j++) dest[i+j] = src[j]; dest[i+j] = '\0'; return dest; } int strcmp(const char* s1, const char* s2) { while(*s1 && (*s1==*s2)) s1++,s2++; return *(const unsigned char*)s1-*(const unsigned char*)s2; } ``` I'm working on a kernel and I've tripped up on several gotchas. Basically I'm building a string like this: ``` unsigned char current_cmd[100]; char tmp[] = {kbdus[scancode], '\0'}; if (scancode != 0x1C) // enter key strcat((char*)current_cmd, tmp); ``` Then I do a comparison to see if it matches a command: ``` if (strcmp((const char*)cmd, "help") == 0) puts((unsigned char*)"You can't do anything yet.\n"); ``` Then I do: ``` current_cmd = (unsigned char)'\0'; ``` to reset it for use. It works but I don't really understand why or how. Can anyone give me an explanation of why what I'm doing works and if there's anything wrong with my code? Third, are `char check[10] = {"help"};` and `{'h', 'e', 'l', 'p', ...}` the same or am I missing something here?

Original source