Getline function

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
Cmaranec
Member
Member
Posts: 45
Joined: Sat Oct 21, 2006 1:07 pm
Location: Czech Republic

Getline function

Post 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?
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post 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...
C8H10N4O2 | #446691 | Trust the nodes.
INF1n1t
Member
Member
Posts: 60
Joined: Fri Dec 22, 2006 5:32 pm
Location: Somewhere Down...

Post 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:

Code: Select all

 ret[i-1] = '\0';

And then return a pointer to the array.
I think, I have problems with Bochs. The biggest one: Bochs hates me!
urxae
Member
Member
Posts: 149
Joined: Sun Jul 30, 2006 8:16 am
Location: The Netherlands

Post 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.
Cmaranec
Member
Member
Posts: 45
Joined: Sat Oct 21, 2006 1:07 pm
Location: Czech Republic

ehhh...

Post 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'
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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...
Every good solution is obvious once you've found it.
INF1n1t
Member
Member
Posts: 60
Joined: Fri Dec 22, 2006 5:32 pm
Location: Somewhere Down...

Post 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.
I think, I have problems with Bochs. The biggest one: Bochs hates me!
Cmaranec
Member
Member
Posts: 45
Joined: Sat Oct 21, 2006 1:07 pm
Location: Czech Republic

thx

Post by Cmaranec »

Thanx to all, getline is working 8)
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post 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. :wink:
C8H10N4O2 | #446691 | Trust the nodes.
Post Reply