os/libc/devbuf.c

38 lines
641 B
C
Raw Normal View History

2019-02-09 12:52:45 -06:00
#include "devbuf.h"
#include "stdlib.h"
#include "stdio.h"
devbuf* devbuf_init() {
devbuf* buf=malloc(sizeof(devbuf));
buf->rd=0;
buf->wr=0;
for (int i=0;i<256;i++) {
buf->buf[i]=EOF;
}
}
void devbuf_add(char byte,devbuf* buf) {
buf->buf[buf->wr]=byte;
buf->wr++;
if (buf->wr==buf->rd) {
buf->wr--;
}
}
int devbuf_get(devbuf* buf) {
if (buf->buf[buf->rd]==-1) {
buf->rd++;
if (buf->buf[buf->rd]==-1) {
buf->rd--;
while (buf->buf[buf->rd]==-1);
}
}
int data=buf->buf[buf->rd];
buf->buf[buf->rd]=-1;
buf->rd++;
if (buf->rd>buf->wr) {
buf->rd=buf->wr-1;
}
return data;
}