#I'm making a simple command prompt in C, is there a better way to make it?
1 messages · Page 1 of 1 (latest)
Yes, don't do main recursion 😛
Switch case 0: makes little sense
Just do if(strcmp(u_input, "q") == 0) {} else {}
gets(u_input); is unsafe
I would recommend not using globals
This can be cleaner if you pass an input string to a handle command function
What if I want to add more commands ?
I don't want the classic elseif ladder
switching won't help that
you can either make an if-else ladder (which imo is perfectly fine), or you can make a hash table of command names and function pointers (if you don't have many keys an array of pairs can be fine too)
I see a buffer overflow waiting to happen
Lol, I already know that this can happen, is there a way to a limit and print an eror message when the string is too large ?
A good way would be to check the length of the string with strlen() and ensure it doesn't exceed the buffer space
If it does then you'd throw and error and bail out