Command line stuff & issues

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Command line stuff & issues

Post 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?
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post 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...
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Post 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??
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post 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.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post 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..
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Post by piranha »

Thanks you guys, it really helped! :D
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
Post Reply