1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
/*
* pANS stdio -- _IO_putc, _IO_cleanup
*/
#include "iolib.h"
void _IO_cleanup(void){
fflush(NULL);
}
/*
* Look this over for simplification
*/
int _IO_putc(int c, FILE *f){
int cnt;
static int first=1;
switch(f->state){
case RD:
f->state=ERR;
case ERR:
case CLOSED:
return EOF;
case OPEN:
_IO_setvbuf(f);
/* fall through */
case RDWR:
case END:
f->rp=f->buf+f->bufl;
if(f->flags&LINEBUF){
f->wp=f->rp;
f->lp=f->buf;
}
else
f->wp=f->buf;
break;
}
if(first){
atexit(_IO_cleanup);
first=0;
}
if(f->flags&STRING){
f->rp=f->buf+f->bufl;
if(f->wp==f->rp){
if(f->flags&BALLOC)
f->buf=realloc(f->buf, f->bufl+BUFSIZ);
else{
f->state=ERR;
return EOF;
}
if(f->buf==NULL){
f->state=ERR;
return EOF;
}
f->rp=f->buf+f->bufl;
f->bufl+=BUFSIZ;
}
*f->wp++=c;
}
else if(f->flags&LINEBUF){
if(f->lp==f->rp){
cnt=f->lp-f->buf;
if(f->flags&APPEND) lseek(f->fd, 0L, SEEK_END);
if(cnt!=0 && write(f->fd, f->buf, cnt)!=cnt){
f->state=ERR;
return EOF;
}
f->lp=f->buf;
}
*f->lp++=c;
if(c=='\n'){
cnt=f->lp-f->buf;
if(f->flags&APPEND) lseek(f->fd, 0L, SEEK_END);
if(cnt!=0 && write(f->fd, f->buf, cnt)!=cnt){
f->state=ERR;
return EOF;
}
f->lp=f->buf;
}
}
else if(f->buf==f->unbuf){
f->unbuf[0]=c;
if(f->flags&APPEND) lseek(f->fd, 0L, SEEK_END);
if(write(f->fd, f->buf, 1)!=1){
f->state=ERR;
return EOF;
}
}
else{
if(f->wp==f->rp){
cnt=f->wp-f->buf;
if(f->flags&APPEND) lseek(f->fd, 0L, SEEK_END);
if(cnt!=0 && write(f->fd, f->buf, cnt)!=cnt){
f->state=ERR;
return EOF;
}
f->wp=f->buf;
f->rp=f->buf+f->bufl;
}
*f->wp++=c;
}
f->state=WR;
/*
* Make sure EOF looks different from putc(-1)
* Should be able to cast to unsigned char, but
* there's a vc bug preventing that from working
*/
return c&0xff;
}
|