123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
-
-
- #ifndef lint
- static char sccsid[] = "@(#)tftpsubs.c 5.4 (Berkeley) 6/29/88";
- #endif
-
-
-
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/ioctl.h>
- #include <netinet/in.h>
- #include <arpa/tftp.h>
- #include <stdio.h>
-
- #define PKTSIZE (1432+4)
-
- struct bf {
- int counter;
- char buf[PKTSIZE];
- } bfs[2];
-
-
- #define BF_ALLOC -3
- #define BF_FREE -2
-
-
- extern int segsize;
-
- static int nextone;
- static int current;
-
-
- int newline = 0;
- int prevchar = -1;
-
- struct tftphdr *rw_init();
-
- struct tftphdr *w_init() { return rw_init(0); }
- struct tftphdr *r_init() { return rw_init(1); }
-
- struct tftphdr *
- rw_init(x)
- int x;
- {
- newline = 0;
- prevchar = -1;
- bfs[0].counter = BF_ALLOC;
- current = 0;
- bfs[1].counter = BF_FREE;
- nextone = x;
- return (struct tftphdr *)bfs[0].buf;
- }
-
-
-
- readit(file, dpp, convert)
- FILE *file;
- struct tftphdr **dpp;
- int convert;
- {
- struct bf *b;
-
- bfs[current].counter = BF_FREE;
- current = !current;
-
- b = &bfs[current];
- if (b->counter == BF_FREE)
- read_ahead(file, convert);
-
- *dpp = (struct tftphdr *)b->buf;
- return b->counter;
- }
-
-
- read_ahead(file, convert)
- FILE *file;
- int convert;
- {
- register int i;
- register char *p;
- register int c;
- struct bf *b;
- struct tftphdr *dp;
-
- b = &bfs[nextone];
- if (b->counter != BF_FREE)
- return;
- nextone = !nextone;
-
- dp = (struct tftphdr *)b->buf;
-
- if (convert == 0) {
- int i;
- b->counter = 0;
- do {
- i = read(fileno(file), dp->th_data + b->counter,
- segsize - b->counter);
- if (i > 0)
- b->counter += i;
- } while (i != 0 && !(i < 0 && errno != EINTR) &&
- b->counter < segsize);
- return;
- }
-
- p = dp->th_data;
- for (i = 0 ; i < segsize; i++) {
- if (newline) {
- if (prevchar == '\n')
- c = '\n';
- else c = '\0';
- newline = 0;
- }
- else {
- c = getc(file);
- if (c == EOF) break;
- if (c == '\n' || c == '\r') {
- prevchar = c;
- c = '\r';
- newline = 1;
- }
- }
- *p++ = c;
- }
- b->counter = (int)(p - dp->th_data);
- }
-
-
- writeit(file, dpp, ct, convert)
- FILE *file;
- struct tftphdr **dpp;
- int convert;
- {
- bfs[current].counter = ct;
- current = !current;
- if (bfs[current].counter != BF_FREE)
- write_behind(file, convert);
- bfs[current].counter = BF_ALLOC;
- *dpp = (struct tftphdr *)bfs[current].buf;
- return ct;
- }
-
-
- write_behind(file, convert)
- FILE *file;
- int convert;
- {
- char *buf;
- int count;
- register int ct;
- register char *p;
- register int c;
- struct bf *b;
- struct tftphdr *dp;
-
- b = &bfs[nextone];
- if (b->counter < -1)
- return 0;
-
- count = b->counter;
- b->counter = BF_FREE;
- dp = (struct tftphdr *)b->buf;
- nextone = !nextone;
- buf = dp->th_data;
-
- if (count <= 0) return -1;
-
- if (convert == 0)
- return write(fileno(file), buf, count);
-
- p = buf;
- ct = count;
- while (ct--) {
- c = *p++;
- if (prevchar == '\r') {
- if (c == '\n')
- fseek(file, -1, 1);
- else
- if (c == '\0')
- goto skipit;
-
- }
- putc(c, file);
- skipit:
- prevchar = c;
- }
- return count;
- }
-
-
-
-
- int
- synchnet(f)
- int f;
- {
- int i, j = 0;
- char rbuf[PKTSIZE];
- struct sockaddr_in from;
- int fromlen;
-
- while (1) {
- (void) ioctl(f, FIONREAD, &i);
- if (i) {
- j++;
- fromlen = sizeof from;
- (void) recvfrom(f, rbuf, sizeof (rbuf), 0,
- (struct sockaddr *)&from, &fromlen);
- } else {
- return(j);
- }
- }
- }
|