newlib read implementation

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
robo3
Posts: 2
Joined: Sat Jan 08, 2011 5:00 pm

newlib read implementation

Post by robo3 »

Welcome.
I have problem with newlib read function:
Here is my own function which depends on newlib

Code: Select all

int kread(int fd,char *buf, int nbytes)
{
  int i = 0;

  /* checking if buf contains 'THE TEXT' */
 printf("%s",buf);

  for (i = 0; i < nbytes; i++) {
    buf[i] = kgetchar();
    kputchar(buf[i]);
    if(buf[i] =='\n') {buf[i] ='\0'; break;}
  }
  
  return i;
}
somewhere in main i've

Code: Select all

char buffer[512] = "THE TEXT";
scanf("%s",buffer);
printf("%s",buffer);
and the problem is neigher buf contains 'THE TEXT' nor entered value. What am I doing wrong ?

please help
Robert.
Tosi
Member
Member
Posts: 255
Joined: Tue Jun 15, 2010 9:27 am
Location: Flyover State, United States
Contact:

Re: newlib read implementation

Post by Tosi »

For one, that is definitely NOT how I would implement a read() function. A file descriptor is passed for a reason - use it.
Even if your OS doesn't support files yet, you can still check if the file descriptor is equal to that of stdin/stdout, and whether
stdin and stdout are currently mapped to the keyboard and text mode monitor respectively.

Second, the problem could be almost anywhere, most likely your keyboard driver. It could also be the text mode functions as well.
Have you checked to make sure your keyboard interrupt works? Just put a call to a putchar() function temporarily in your handler,
after you've translated the scancode to whatever locale you're currently using.
robo3
Posts: 2
Joined: Sat Jan 08, 2011 5:00 pm

Re: newlib read implementation

Post by robo3 »

Thanks for your reply.
The problem is, that write function works good, but kread() doesn't add the char to an array but it writes the chars on the screen while I'm typing them.
For example if I have in char array text "TEXT" it writes it on the screen without any problems. Also, when I want to type it to array - it writes chars while typing, but when i push enter, the array is clear (when I want to write the array's content is nothing). The kgetchar() function works fine.

Code: Select all



int kread(int fd,char *buf, int nbytes)
{
  int i = 0;
  
  char *ptr = buf;
  
  printf("\n-------Reads ");
  for (i = 0; i < nbytes; i++) {
    *ptr = kgetchar();
    putchar(*ptr);
    if(buf[i] =='\n') {buf[i] ='\0'; break;}
    ptr++;
  }
  
  return i;
}

void kmain() 
{
att = 0x0700;
memset(videoram,0,80*25*2);
idt_install(); 
irq_install();
isrs_install();
gdt_install();

char buffer[512] = "THE TEXT\0";
scanf(buffer);
printf("\nShowing array :");

char *k = buffer; 

while(*k)
{
	kputchar(*k);
	k++;
}

while(1);
}



Where i've made a mistake? Please help
Robert.
Attachments
Screenshot of VB
Screenshot of VB
Screenshot-1.png (6.91 KiB) Viewed 1191 times
Post Reply