Page 1 of 1

Command line stuff & issues

Posted: Fri Feb 02, 2007 7:57 pm
by piranha
I need to make a simple command line. I have some code but it doesn't work.

char i[128];
char *inp;
inp = i;
while(1)
{
puts("SeaOS 1.0~>");
gets(inp);
if (inp == "yo")
puts("wazzup\n");
}



Can you help?

Posted: Fri Feb 02, 2007 8:00 pm
by Alboin
I do believe you are forgetting to use a strcmp function when comparing the strings. Simply using an equal sign will not work. That should fix it...assuming everything is in working order...

Posted: Fri Feb 02, 2007 8:07 pm
by piranha
of course!!!!!!! :oops: :lol: :roll: Thanks!!!!!!!!!



.................................but I dont have a strcmp function :oops: and I have no idea how to write one...........could you help??

Posted: Fri Feb 02, 2007 8:21 pm
by pcmattman
Loop through the string and check each character (here you can use ==). If one is not the same, return false. Otherwise, return true.

Posted: Fri Feb 02, 2007 9:32 pm
by Brynet-Inc
This is a quick implementation, Assuming a few other things work in your OS.

Code: Select all

int strlen(const char *s1) {
	int i = 0;
	while(*s1) {
		i++;
		++s1;
	}
	return i;
}

int strcmp(const char *s1, const char *s2) {
	int i;
	if(strlen(s1) != strlen(s2)) {
		return 1;
	}
	for(i = 0; i < strlen(s1); i++) {
		if(s1[i] != s2[i]) {
			return 1;
		}
	}
	return 0;
}
Hope that helps..

(BTW this is kinda how it could be done on any other standard system..That has fgets and stdin support anyway.. :oops:)

Code: Select all

#include <stdio.h>

int main(void) {
        char inp[128];
        while(1) {
                printf("SeaOS 1.0~> ");
                fgets(inp, sizeof(inp), stdin);
                inp[strlen(inp) - 1] = '\0';
                if(strcmp(inp, "yo") == 0) {
                        printf("wazzup\n");
                }
        }
        return 0;
}
From this you should be able to see how to use what your OS calls "gets" instead..

Posted: Sat Feb 03, 2007 12:16 am
by piranha
Thanks you guys, it really helped! :D