Page 1 of 1
Getline function
Posted: Sat Jan 06, 2007 12:12 pm
by Cmaranec
How can i create simple getline function (like readln in Pascal) in OS written in C ? I have got keyboard driver (irq 1) and timer (irq 0). I am using DJGPP to compile and link. Can you write a simple function for me?
Posted: Sat Jan 06, 2007 12:42 pm
by Alboin
It's not that difficult, as long as you have a getchar like function. It would be something like this.
Code: Select all
char *getline() {
int i = 0;
char c;
char ret[64];
while(c != '\n') [
c = getchar();
ret[i] = c;
i++;
}
return ret;
}
Note: This code isn't tested or even proven to work...
Posted: Sat Jan 06, 2007 1:45 pm
by INF1n1t
I think the code might need a little optimization. Putting a terminating null at the end of the string would be good. You should put a terminating null at the end, where the '\n' character stays.
When you read with this while cycle, the '\n' character will be stored at the end. You should write the following statement after the cycle:
And then return a pointer to the array.
Posted: Sat Jan 06, 2007 1:52 pm
by urxae
Alboin wrote:Code: Select all
char *getline() {
int i = 0;
char c;
char ret[64];
while(c != '\n') [
c = getchar();
ret[i] = c;
i++;
}
return ret;
}
Note: This code isn't tested or even proven to work...
Some errors here:
* No null termination (so unless the caller is careful to only read up to \n it's going to read too much).
* Returns a local variable (the array).
* Doesn't check if the line fits into the buffer, so there's a buffer overflow if the line is longer than 64 chars (including \n).
So don't use that version.
ehhh...
Posted: Sat Jan 06, 2007 2:19 pm
by Cmaranec
Can you write repaired code? Now linker said:
- main.o(.text+0x72):main.c: undefined reference to `_getchar'
main.o(.text+0xa0):main.c: undefined reference to `_getchar'
Posted: Sat Jan 06, 2007 2:57 pm
by Solar
...which means you have no getchar() function (see Alboin's precondition). If you have a keyboard driver working, that should be pretty easy to do...
Posted: Sat Jan 06, 2007 3:44 pm
by INF1n1t
I think the whole idea about the getchar is wrong. Or if it is not wrong, then you must find a way to use the library file (where the getchar definition stays)...However, I think also Alboin just wanted to show an example about reading lines from somewhere. And then Cmaranec - your question is answered. If somebody give you the right code, it won't work. You must adapt the algorithm to your operating system.
thx
Posted: Sat Jan 06, 2007 4:03 pm
by Cmaranec
Thanx to all, getline is working
Posted: Sat Jan 06, 2007 4:31 pm
by Alboin
INF1n1t wrote:I think the whole idea about the getchar is wrong. Or if it is not wrong, then you must find a way to use the library file (where the getchar definition stays)...However, I think also Alboin just wanted to show an example about reading lines from somewhere. And then Cmaranec - your question is answered. If somebody give you the right code, it won't work. You must adapt the algorithm to your operating system.
Very true. It was meant to be more pseudo-code than anything else.