|
|
1.1 root 1: /* Copyright AT&T Bell Laboratories, 1993 */
2: #include <stdio.h>
3:
4: /* same as fgets, but returns a char count instead of first arg.
5: * robust against null bytes in input */
6:
7: int
8: cgets(char *ptr, int n, FILE *iop)
9: {
10: int l;
11: char *s = ptr;
12: unsigned char *t;
13: while(--n > 0) {
14: l = iop->_cnt;
15: if(l > 0) {
16: if(l > n) l = n;
17: t = iop->_ptr;
18: while(--l >= 0 && (*s++ = *t++) != '\n')
19: continue;
20: l = t - iop->_ptr;
21: iop->_ptr = t;
22: iop->_cnt -= l;
23: if(t[-1] == '\n' || (n -= l) <= 0)
24: break;
25: }
26: l = getc(iop);
27: if(l == EOF)
28: break;
29: *s++ = l;
30: if(l == '\n')
31: break;
32: }
33: *s = 0;
34: return s - ptr;
35: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.