File: flsbuf.c

package info (click to toggle)
magic 7.5.220-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 17,860 kB
file content (202 lines) | stat: -rw-r--r-- 4,777 bytes parent folder | download | duplicates (2)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*    NOTE: THIS CODE ONLY WORKS FOR THE VAX IMPLEMENTATION OF 4.2 BSD    */

/* flsbuf.c --
 *
 *     ********************************************************************* 
 *     * Copyright (C) 1985, 1990 Regents of the University of California. * 
 *     * Permission to use, copy, modify, and distribute this              * 
 *     * software and its documentation for any purpose and without        * 
 *     * fee is hereby granted, provided that the above copyright          * 
 *     * notice appear in all copies.  The University of California        * 
 *     * makes no representations about the suitability of this            * 
 *     * software for any purpose.  It is provided "as is" without         * 
 *     * express or implied warranty.  Export of this software outside     * 
 *     * of the United States of America may require an export license.    * 
 *     *********************************************************************
 *
 * This file is a modified version of the standard system
 * routine, the big difference being that it can deal gracefully
 * with interrupts that cause I/O to be incomplete.  The version
 * from which this file is taken is:
 * @(#)flsbuf.c	4.5 (Berkeley) 12/16/82
 */

#ifndef lint
static char rcsid[] __attribute__ ((unused)) = "$Header: /usr/cvsroot/magic-7.5/utils/flsbuf.c,v 1.1.1.1 2006/04/10 22:03:14 tim Exp $";
#endif  /* not lint */


/* NOTE: do not move the #ifdef vax up since the loader doesn't like files
 * with no symbols.
 */

#if (defined(vax) && defined(BSD4_2))

#include	<stdio.h>
#include	<sys/param.h>
#include	<sys/stat.h>

/*
 * To prevent infinite loops if every attempt to write is
 * interrupted, we only retry an interrupted write a fixed number
 * of times, NWRITEATTEMPTS
 */

#define	NWRITEATTEMPTS	10

/* NOTE: we use the stdio version of malloc here! */
char	*malloc();

int
_flsbuf(c, iop)
    unsigned c;
    FILE *iop;
{
	char *base;
	n, rn;
	ntries;
	char c1;
	int size;
	struct stat stbuf;

	if (iop->_flag & _IORW) {
		iop->_flag |= _IOWRT;
		iop->_flag &= ~_IOEOF;
	}

	if ((iop->_flag&_IOWRT)==0)
		return(EOF);
tryagain:
	if (iop->_flag&_IOLBF) {
		base = iop->_base;
		*iop->_ptr++ = c;
		if (iop->_ptr >= base+iop->_bufsiz || c == '\n') {
			rn = iop->_ptr - base;
			iop->_ptr = base;
			ntries = NWRITEATTEMPTS;
			for (n=0; (rn > 0) && (n >= 0) && (ntries-- > 0);
				    rn -= n, base += n)
				n = write(fileno(iop), base, rn);
		} else
			n = 0;
		iop->_cnt = 0;
	} else if (iop->_flag&_IONBF) {
		c1 = c;
		ntries = NWRITEATTEMPTS;
		while ((n = write(fileno(iop), &c1, 1)) == 0 && ntries-- > 0)
			;
		iop->_cnt = 0;
	} else {
		if ((base=iop->_base)==NULL) {
			if (fstat(fileno(iop), &stbuf) < 0 ||
			    stbuf.st_blksize <= NULL)
				size = BUFSIZ;
			else
				size = stbuf.st_blksize;
			if ((iop->_base=base=malloc(size)) == NULL) {
				iop->_flag |= _IONBF;
				goto tryagain;
			}
			iop->_flag |= _IOMYBUF;
			iop->_bufsiz = size;
			if (iop==stdout && isatty(fileno(stdout))) {
				iop->_flag |= _IOLBF;
				iop->_ptr = base;
				goto tryagain;
			}
			rn = n = 0;
		} else if ((rn = n = iop->_ptr - base) > 0) {
			iop->_ptr = base;
			ntries = NWRITEATTEMPTS;
			for (n=0; (rn > 0) && (n >= 0) && (ntries-- > 0);
				    rn -= n, base += n)
				n = write(fileno(iop), base, rn);
			base = iop->_ptr;
		}
		iop->_cnt = iop->_bufsiz-1;
		*base++ = c;
		iop->_ptr = base;
	}
	if (n < 0) {
		iop->_flag |= _IOERR;
		return(EOF);
	}
	return(c);
}

int
fflush(iop)
    struct _iobuf *iop;
{
	char *base;
	n, rn;
	ntries;

	if ((iop->_flag&(_IONBF|_IOWRT))==_IOWRT
	 && (base=iop->_base)!=NULL && (rn=iop->_ptr-base)>0) {
		iop->_ptr = base;
		iop->_cnt = (iop->_flag&(_IOLBF|_IONBF)) ? 0 : iop->_bufsiz;
		ntries = NWRITEATTEMPTS;
		for (n = 0; (n >= 0) && (rn > 0) && (ntries-- > 0);
			    base += n, rn -= n)
			n = write(fileno(iop), base, rn);
		if (n < 0) {
			iop->_flag |= _IOERR;
			return(EOF);
		}
	}
	return(0);
}

int
fclose(iop)
    struct _iobuf *iop;
{
	int r;

	r = EOF;
	if (iop->_flag&(_IOREAD|_IOWRT|_IORW) && (iop->_flag&_IOSTRG)==0) {
		r = fflush(iop);
		if (close(fileno(iop)) < 0)
			r = EOF;
		if (iop->_flag&_IOMYBUF)
			free(iop->_base);
	}
	iop->_cnt = 0;
	iop->_base = (char *)NULL;
	iop->_ptr = (char *)NULL;
	iop->_bufsiz = 0;
	iop->_flag = 0;
	iop->_file = 0;
	return(r);
}

/*
 * Flush buffers on exit.  Note:  this procedure should only be present
 * in 4.2 Unix.  In 4.3 it will be ifdef'ed out.
 */

#ifdef	BSD4_2
void
_cleanup()
{
	struct _iobuf *iop;
	extern struct _iobuf *_lastbuf;

	for (iop = _iob; iop < _lastbuf; iop++)
		fclose(iop);
}
#endif	/* BSD4_2 */

#else  /* vax */

/* Dummy proc just so the file will have a proc in it to make things easier
 * on the linker.
 */

void
dummyFlsbuf()
{
}

#endif /* vax */