Commit | Line | Data |
---|---|---|
586c39b1 DF |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
3 | * All rights reserved. The Berkeley software License Agreement | |
4 | * specifies the terms and conditions for redistribution. | |
5 | */ | |
6 | ||
2ce81398 | 7 | #if defined(LIBC_SCCS) && !defined(lint) |
40754574 | 8 | static char sccsid[] = "@(#)wbuf.c 5.3 (Berkeley) %G%"; |
2ce81398 | 9 | #endif LIBC_SCCS and not lint |
586c39b1 | 10 | |
62e7287b | 11 | #include <stdio.h> |
f4c06a32 KM |
12 | #include <sys/types.h> |
13 | #include <sys/stat.h> | |
62e7287b BJ |
14 | |
15 | char *malloc(); | |
16 | ||
17 | _flsbuf(c, iop) | |
94fe6243 | 18 | unsigned char c; |
62e7287b BJ |
19 | register FILE *iop; |
20 | { | |
21 | register char *base; | |
22 | register n, rn; | |
23 | char c1; | |
f4c06a32 KM |
24 | int size; |
25 | struct stat stbuf; | |
62e7287b | 26 | |
d8af6b8b MT |
27 | if (iop->_flag & _IORW) { |
28 | iop->_flag |= _IOWRT; | |
ec6ddbc1 | 29 | iop->_flag &= ~(_IOEOF|_IOREAD); |
d8af6b8b MT |
30 | } |
31 | ||
62e7287b BJ |
32 | if ((iop->_flag&_IOWRT)==0) |
33 | return(EOF); | |
34 | tryagain: | |
35 | if (iop->_flag&_IOLBF) { | |
36 | base = iop->_base; | |
37 | *iop->_ptr++ = c; | |
f4c06a32 | 38 | if (iop->_ptr >= base+iop->_bufsiz || c == '\n') { |
62e7287b BJ |
39 | n = write(fileno(iop), base, rn = iop->_ptr - base); |
40 | iop->_ptr = base; | |
94fe6243 | 41 | iop->_cnt = 0; |
62e7287b BJ |
42 | } else |
43 | rn = n = 0; | |
62e7287b BJ |
44 | } else if (iop->_flag&_IONBF) { |
45 | c1 = c; | |
46 | rn = 1; | |
47 | n = write(fileno(iop), &c1, rn); | |
48 | iop->_cnt = 0; | |
49 | } else { | |
50 | if ((base=iop->_base)==NULL) { | |
f4c06a32 KM |
51 | if (fstat(fileno(iop), &stbuf) < 0 || |
52 | stbuf.st_blksize <= NULL) | |
53 | size = BUFSIZ; | |
54 | else | |
55 | size = stbuf.st_blksize; | |
f4c06a32 | 56 | if ((iop->_base=base=malloc(size)) == NULL) { |
62e7287b BJ |
57 | iop->_flag |= _IONBF; |
58 | goto tryagain; | |
59 | } | |
60 | iop->_flag |= _IOMYBUF; | |
f4c06a32 | 61 | iop->_bufsiz = size; |
c528f54e RC |
62 | if (iop==stdout && isatty(fileno(stdout))) { |
63 | iop->_flag |= _IOLBF; | |
64 | iop->_ptr = base; | |
65 | goto tryagain; | |
66 | } | |
62e7287b BJ |
67 | rn = n = 0; |
68 | } else if ((rn = n = iop->_ptr - base) > 0) { | |
69 | iop->_ptr = base; | |
70 | n = write(fileno(iop), base, n); | |
71 | } | |
f4c06a32 | 72 | iop->_cnt = iop->_bufsiz-1; |
62e7287b BJ |
73 | *base++ = c; |
74 | iop->_ptr = base; | |
75 | } | |
76 | if (rn != n) { | |
77 | iop->_flag |= _IOERR; | |
78 | return(EOF); | |
79 | } | |
80 | return(c); | |
81 | } | |
82 | ||
83 | fflush(iop) | |
41e01b3e | 84 | register FILE *iop; |
62e7287b BJ |
85 | { |
86 | register char *base; | |
87 | register n; | |
88 | ||
89 | if ((iop->_flag&(_IONBF|_IOWRT))==_IOWRT | |
90 | && (base=iop->_base)!=NULL && (n=iop->_ptr-base)>0) { | |
91 | iop->_ptr = base; | |
f4c06a32 | 92 | iop->_cnt = (iop->_flag&(_IOLBF|_IONBF)) ? 0 : iop->_bufsiz; |
62e7287b BJ |
93 | if (write(fileno(iop), base, n)!=n) { |
94 | iop->_flag |= _IOERR; | |
95 | return(EOF); | |
96 | } | |
97 | } | |
98 | return(0); | |
99 | } | |
100 | ||
62e7287b | 101 | fclose(iop) |
41e01b3e | 102 | register FILE *iop; |
62e7287b | 103 | { |
c13cf752 | 104 | register int r; |
62e7287b BJ |
105 | |
106 | r = EOF; | |
d8af6b8b | 107 | if (iop->_flag&(_IOREAD|_IOWRT|_IORW) && (iop->_flag&_IOSTRG)==0) { |
62e7287b BJ |
108 | r = fflush(iop); |
109 | if (close(fileno(iop)) < 0) | |
110 | r = EOF; | |
111 | if (iop->_flag&_IOMYBUF) | |
112 | free(iop->_base); | |
62e7287b | 113 | } |
62e7287b | 114 | iop->_cnt = 0; |
c13cf752 CC |
115 | iop->_base = (char *)NULL; |
116 | iop->_ptr = (char *)NULL; | |
117 | iop->_bufsiz = 0; | |
118 | iop->_flag = 0; | |
119 | iop->_file = 0; | |
62e7287b BJ |
120 | return(r); |
121 | } | |
40754574 KB |
122 | |
123 | _cleanup() | |
124 | { | |
125 | extern int _fwalk(); | |
126 | ||
127 | _fwalk(fclose); | |
128 | } |