Page 1 of 1

newlib read implementation

Posted: Sat Jan 08, 2011 5:14 pm
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.

Re: newlib read implementation

Posted: Sat Jan 08, 2011 6:10 pm
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.

Re: newlib read implementation

Posted: Sun Jan 09, 2011 6:14 am
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.