Purge warnings from prism2 drivers
[gpxe.git] / contrib / tftp / tftpsubs.c
blob608d64ec5625c5416edbed42204fdc9b4317a5c1
1 /*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 #ifndef lint
19 static char sccsid[] = "@(#)tftpsubs.c 5.4 (Berkeley) 6/29/88";
20 #endif /* not lint */
22 /* Simple minded read-ahead/write-behind subroutines for tftp user and
23 server. Written originally with multiple buffers in mind, but current
24 implementation has two buffer logic wired in.
26 Todo: add some sort of final error check so when the write-buffer
27 is finally flushed, the caller can detect if the disk filled up
28 (or had an i/o error) and return a nak to the other side.
30 Jim Guyton 10/85
33 #include <errno.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/ioctl.h>
37 #include <netinet/in.h>
38 #include <arpa/tftp.h>
39 #include <stdio.h>
41 #define PKTSIZE (1432+4) /* SEGSIZE+4 */ /* should be moved to tftp.h */
43 struct bf {
44 int counter; /* size of data in buffer, or flag */
45 char buf[PKTSIZE]; /* room for data packet */
46 } bfs[2];
48 /* Values for bf.counter */
49 #define BF_ALLOC -3 /* alloc'd but not yet filled */
50 #define BF_FREE -2 /* free */
51 /* [-1 .. SEGSIZE] = size of data in the data buffer */
53 extern int segsize;
55 static int nextone; /* index of next buffer to use */
56 static int current; /* index of buffer in use */
58 /* control flags for crlf conversions */
59 int newline = 0; /* fillbuf: in middle of newline expansion */
60 int prevchar = -1; /* putbuf: previous char (cr check) */
62 struct tftphdr *rw_init();
64 struct tftphdr *w_init() { return rw_init(0); } /* write-behind */
65 struct tftphdr *r_init() { return rw_init(1); } /* read-ahead */
67 struct tftphdr *
68 rw_init(x) /* init for either read-ahead or write-behind */
69 int x; /* zero for write-behind, one for read-head */
71 newline = 0; /* init crlf flag */
72 prevchar = -1;
73 bfs[0].counter = BF_ALLOC; /* pass out the first buffer */
74 current = 0;
75 bfs[1].counter = BF_FREE;
76 nextone = x; /* ahead or behind? */
77 return (struct tftphdr *)bfs[0].buf;
81 /* Have emptied current buffer by sending to net and getting ack.
82 Free it and return next buffer filled with data.
84 readit(file, dpp, convert)
85 FILE *file; /* file opened for read */
86 struct tftphdr **dpp;
87 int convert; /* if true, convert to ascii */
89 struct bf *b;
91 bfs[current].counter = BF_FREE; /* free old one */
92 current = !current; /* "incr" current */
94 b = &bfs[current]; /* look at new buffer */
95 if (b->counter == BF_FREE) /* if it's empty */
96 read_ahead(file, convert); /* fill it */
97 /* assert(b->counter != BF_FREE); /* check */
98 *dpp = (struct tftphdr *)b->buf; /* set caller's ptr */
99 return b->counter;
103 * fill the input buffer, doing ascii conversions if requested
104 * conversions are lf -> cr,lf and cr -> cr, nul
106 read_ahead(file, convert)
107 FILE *file; /* file opened for read */
108 int convert; /* if true, convert to ascii */
110 register int i;
111 register char *p;
112 register int c;
113 struct bf *b;
114 struct tftphdr *dp;
116 b = &bfs[nextone]; /* look at "next" buffer */
117 if (b->counter != BF_FREE) /* nop if not free */
118 return;
119 nextone = !nextone; /* "incr" next buffer ptr */
121 dp = (struct tftphdr *)b->buf;
123 if (convert == 0) {
124 int i;
125 b->counter = 0;
126 do {
127 i = read(fileno(file), dp->th_data + b->counter,
128 segsize - b->counter);
129 if (i > 0)
130 b->counter += i;
131 } while (i != 0 && !(i < 0 && errno != EINTR) &&
132 b->counter < segsize);
133 return;
136 p = dp->th_data;
137 for (i = 0 ; i < segsize; i++) {
138 if (newline) {
139 if (prevchar == '\n')
140 c = '\n'; /* lf to cr,lf */
141 else c = '\0'; /* cr to cr,nul */
142 newline = 0;
144 else {
145 c = getc(file);
146 if (c == EOF) break;
147 if (c == '\n' || c == '\r') {
148 prevchar = c;
149 c = '\r';
150 newline = 1;
153 *p++ = c;
155 b->counter = (int)(p - dp->th_data);
158 /* Update count associated with the buffer, get new buffer
159 from the queue. Calls write_behind only if next buffer not
160 available.
162 writeit(file, dpp, ct, convert)
163 FILE *file;
164 struct tftphdr **dpp;
165 int convert;
167 bfs[current].counter = ct; /* set size of data to write */
168 current = !current; /* switch to other buffer */
169 if (bfs[current].counter != BF_FREE) /* if not free */
170 write_behind(file, convert); /* flush it */
171 bfs[current].counter = BF_ALLOC; /* mark as alloc'd */
172 *dpp = (struct tftphdr *)bfs[current].buf;
173 return ct; /* this is a lie of course */
177 * Output a buffer to a file, converting from netascii if requested.
178 * CR,NUL -> CR and CR,LF => LF.
179 * Note spec is undefined if we get CR as last byte of file or a
180 * CR followed by anything else. In this case we leave it alone.
182 write_behind(file, convert)
183 FILE *file;
184 int convert;
186 char *buf;
187 int count;
188 register int ct;
189 register char *p;
190 register int c; /* current character */
191 struct bf *b;
192 struct tftphdr *dp;
194 b = &bfs[nextone];
195 if (b->counter < -1) /* anything to flush? */
196 return 0; /* just nop if nothing to do */
198 count = b->counter; /* remember byte count */
199 b->counter = BF_FREE; /* reset flag */
200 dp = (struct tftphdr *)b->buf;
201 nextone = !nextone; /* incr for next time */
202 buf = dp->th_data;
204 if (count <= 0) return -1; /* nak logic? */
206 if (convert == 0)
207 return write(fileno(file), buf, count);
209 p = buf;
210 ct = count;
211 while (ct--) { /* loop over the buffer */
212 c = *p++; /* pick up a character */
213 if (prevchar == '\r') { /* if prev char was cr */
214 if (c == '\n') /* if have cr,lf then just */
215 fseek(file, -1, 1); /* smash lf on top of the cr */
216 else
217 if (c == '\0') /* if have cr,nul then */
218 goto skipit; /* just skip over the putc */
219 /* else just fall through and allow it */
221 putc(c, file);
222 skipit:
223 prevchar = c;
225 return count;
229 /* When an error has occurred, it is possible that the two sides
230 * are out of synch. Ie: that what I think is the other side's
231 * response to packet N is really their response to packet N-1.
233 * So, to try to prevent that, we flush all the input queued up
234 * for us on the network connection on our host.
236 * We return the number of packets we flushed (mostly for reporting
237 * when trace is active).
241 synchnet(f)
242 int f; /* socket to flush */
244 int i, j = 0;
245 char rbuf[PKTSIZE];
246 struct sockaddr_in from;
247 int fromlen;
249 while (1) {
250 (void) ioctl(f, FIONREAD, &i);
251 if (i) {
252 j++;
253 fromlen = sizeof from;
254 (void) recvfrom(f, rbuf, sizeof (rbuf), 0,
255 (struct sockaddr *)&from, &fromlen);
256 } else {
257 return(j);