|
|
1.1 ! root 1: #include "u.h" ! 2: #include "../port/lib.h" ! 3: #include "mem.h" ! 4: #include "dat.h" ! 5: #include "fns.h" ! 6: #include "io.h" ! 7: ! 8: void ! 9: initq(IOQ *q) ! 10: { ! 11: lock(q); ! 12: unlock(q); ! 13: q->in = q->out = q->buf; ! 14: q->puts = puts; ! 15: q->putc = putc; ! 16: q->gets = gets; ! 17: q->getc = getc; ! 18: } ! 19: ! 20: int ! 21: putc(IOQ *q, int c) ! 22: { ! 23: uchar *nextin; ! 24: if(q->in >= &q->buf[sizeof(q->buf)-1]) ! 25: nextin = q->buf; ! 26: else ! 27: nextin = q->in+1; ! 28: if(nextin == q->out) ! 29: return -1; ! 30: *q->in = c; ! 31: q->in = nextin; ! 32: return 0; ! 33: } ! 34: ! 35: int ! 36: getc(IOQ *q) ! 37: { ! 38: int c; ! 39: ! 40: if(q->in == q->out) ! 41: return -1; ! 42: c = *q->out; ! 43: if(q->out == q->buf+sizeof(q->buf)-1) ! 44: q->out = q->buf; ! 45: else ! 46: q->out++; ! 47: return c; ! 48: } ! 49: ! 50: void ! 51: puts(IOQ *q, void *buf, int n) ! 52: { ! 53: int m; uchar *nextin; ! 54: uchar *s = buf; ! 55: ! 56: while(n){ ! 57: m = q->out - q->in - 1; ! 58: if(m < 0) ! 59: m = &q->buf[NQ] - q->in; ! 60: if(m == 0) ! 61: break; ! 62: if(m > n) ! 63: m = n; ! 64: memmove(q->in, s, m); ! 65: n -= m; ! 66: s += m; ! 67: nextin = q->in + m; ! 68: if(nextin >= &q->buf[NQ]) ! 69: q->in = q->buf; ! 70: else ! 71: q->in = nextin; ! 72: } ! 73: } ! 74: ! 75: int ! 76: gets(IOQ *q, void *buf, int n) ! 77: { ! 78: int m, k = 0; uchar *nextout; ! 79: uchar *s = buf; ! 80: ! 81: while(n){ ! 82: m = q->in - q->out; ! 83: if(m < 0) ! 84: m = &q->buf[NQ] - q->out; ! 85: if(m == 0) ! 86: return k; ! 87: if(m > n) ! 88: m = n; ! 89: if(buf) ! 90: memmove(s, q->out, m); ! 91: s += m; ! 92: k += m; ! 93: n -= m; ! 94: nextout = q->out + m; ! 95: if(nextout >= &q->buf[NQ]) ! 96: q->out = q->buf; ! 97: else ! 98: q->out = nextout; ! 99: } ! 100: return k; ! 101: } ! 102: ! 103: int ! 104: cangetc(void *arg) ! 105: { ! 106: IOQ *q = (IOQ *)arg; ! 107: int n = q->in - q->out; ! 108: if (n < 0) ! 109: n += sizeof(q->buf); ! 110: return n; ! 111: } ! 112: ! 113: int ! 114: canputc(void *arg) ! 115: { ! 116: IOQ *q = (IOQ *)arg; ! 117: return sizeof(q->buf)-cangetc(q)-1; ! 118: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.