|
|
1.1 ! root 1: #include <stdio.h> ! 2: ! 3: /*----------------------------------------- ! 4: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ! 5: >>>>>>>>>>>> NOTE >>>>>>>>>>>>> Must be identical to those in "tftpd" ! 6: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ! 7: read from local file and transform lines ending with '\n' to lines ! 8: ending with '\r' and '\n'; also transform '\r' to '\r' and '\0' ! 9: ! 10: when we read a '\n' or '\r' from a file, we do not automatically ! 11: store '\r''\n' or '\r' '\0' into buf as we would overflow "buf" but ! 12: "delayed write" is used instead. Thus '\r' '\n' can span across two ! 13: data packets. ! 14: ----------------------------------------*/ ! 15: tftpread(fd, buf, bufsize, fsize) ! 16: FILE *fd; ! 17: register char *buf; ! 18: int bufsize; ! 19: int *fsize; /* count # bytes read from file */ ! 20: { ! 21: ! 22: register char *p; ! 23: int i; ! 24: ! 25: /* oldc is to indicate if the transformation is unfinished in the ! 26: last block */ ! 27: static char oldc; ! 28: ! 29: *fsize = 0; ! 30: for (p=buf; p < buf+bufsize;) { ! 31: switch(oldc) { ! 32: case '\n': ! 33: *p++ = '\n'; /* already output '\r' */ ! 34: oldc = '\0'; ! 35: break; ! 36: case '\r': ! 37: *p++ = '\0'; /* already output '\r' */ ! 38: oldc = '\0'; ! 39: break; ! 40: default: ! 41: i = fread(&oldc, sizeof(oldc), 1, fd); ! 42: if (i<0) ! 43: return(i); ! 44: if (i==0) ! 45: goto endit; ! 46: ! 47: ++(*fsize); ! 48: switch(oldc) { ! 49: case '\n': ! 50: case '\r': ! 51: *p++ = '\r'; ! 52: /* will complete the transformation in next ! 53: ... loop iteration */ ! 54: break; ! 55: default: ! 56: *p++ = oldc; ! 57: break; ! 58: } ! 59: } ! 60: ! 61: } ! 62: endit: ! 63: return(p-buf); ! 64: } ! 65: /*----------------------------------------- ! 66: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ! 67: >>>>>>>>>>>>>>>> NOTE >>>>>>>>> Must be identical to those "tftpd" ! 68: >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ! 69: write to local file and transform lines ending with '\r''\n' to lines ! 70: ending with and '\n'; also transform '\r' '\0' to '\r'. ! 71: ----------------------------------------*/ ! 72: tftpwrite(fd, buf, bufsize, fsize) ! 73: FILE *fd; ! 74: register char *buf; ! 75: int bufsize; ! 76: int *fsize; /* count # of bytes write to file */ ! 77: { ! 78: register char *p; ! 79: char c; ! 80: int i; ! 81: ! 82: /* oldc is to indicate if the transformation is unfinished in the ! 83: last block */ ! 84: static char oldc; ! 85: ! 86: *fsize = 0; ! 87: for (p=buf; p<buf + bufsize; p++) { ! 88: c = *p; ! 89: if (oldc == '\r') { ! 90: char writethisc; ! 91: ! 92: switch(c) { ! 93: case '\0': ! 94: writethisc = oldc; ! 95: break; ! 96: case '\n': ! 97: writethisc = '\n'; ! 98: break; ! 99: default: ! 100: writethisc = oldc; ! 101: break; ! 102: } ! 103: i = fwrite(&writethisc, sizeof(writethisc), 1, fd); ! 104: if (i!=1) ! 105: return(-1); ! 106: ++(*fsize); ! 107: if (c=='\0' || ! 108: c=='\n') { ! 109: oldc = '\0'; ! 110: continue; ! 111: } ! 112: } ! 113: if ((oldc = c) == '\r') ! 114: continue; ! 115: i = fwrite(&c, sizeof(c), 1, fd); ! 116: if (i!=1) ! 117: return(-1); ! 118: ++(*fsize); ! 119: } ! 120: return(*fsize); ! 121: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.