|
|
1.1 root 1: /*
2: * libc/stdio/finit.c
3: * Standard i/o library internals.
4: * Initialize file for i/o:
5: * decide if buffered
6: * allocate buffer if required
7: * stuff appropriate get/put functions
8: * reset char count
9: */
10:
11: #include <stdio.h>
12: #include <errno.h>
13:
14: extern long lseek();
15:
16: extern int _fgetc();
17: extern int _fputc();
18: extern int _fgetb();
19: extern int _fputb();
20: extern int _fputt();
21:
22: void
23: finit(fp) register FILE *fp;
24: {
25: register int tty, sav;
26:
27: sav = errno; /* because isatty() can set errno */
28: if (fp->_bp == NULL
29: && (fp->_ff&_FSTBUF || (tty=isatty(fileno(fp))) && fp!=stdout
30: || (fp->_bp = malloc(BUFSIZ)) == NULL)) {
31: fp->_gt = &_fgetc;
32: fp->_pt = &_fputc;
33: } else if ((fp->_ff&_FSTBUF)==0 && tty && fp==stdout) {
34: fp->_gt = &_fgetb;
35: fp->_pt = &_fputt;
36: fp->_dp = fp->_cp = fp->_bp;
37: fp->_cc = 0;
38: } else {
39: fp->_gt = &_fgetb;
40: fp->_pt = &_fputb;
41: fp->_dp = fp->_cp = fp->_bp + boffset(fileno(fp));
42: fp->_cc = 0;
43: }
44: errno = sav;
45: }
46:
47: static
48: int
49: boffset(fd) int fd;
50: {
51: register long off;
52:
53: if ((off=lseek(fd, 0L, SEEK_CUR))==-1L)
54: return 0;
55: else
56: return (unsigned)off%BUFSIZ;
57: }
58:
59: int
60: _fginit(fp) register FILE *fp;
61: {
62: finit(fp);
63: return (*fp->_gt)(fp);
64: }
65:
66: int
67: _fpinit(c, fp) register char c; register FILE *fp;
68: {
69: finit(fp);
70: return (*fp->_pt)(c, fp);
71: }
72:
73:
74: /*
75: * Close all files.
76: * Called from exit().
77: */
78: void
79: _finish()
80: {
81: register FILE **fpp;
82:
83: for (fpp = &_fp[_NFILE-1]; fpp>=&_fp[0]; --fpp)
84: if (*fpp != NULL)
85: fclose(*fpp);
86: }
87:
88: /* end of libc/stdio/finit.c */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.