?? fget.c
字號:
/*************************************************************************** * * * File: Download Unix File Utility * * Function: This program demostrating download file managment * * * * Author: Long Yun Liang * * Copyright (c) 1,23,1996 All rights reserved * * * ***************************************************************************/#include <stdio.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>#include <termio.h>#include "fget.h"/************************************************************************** * Revised XMODEM/YMODEM CRC protocol * * * * A data packet is defined as: * * typedef struct { * * BYTE soh; - Start of heading * * BYTE seq#; - sequence number * * BYTE not_seq#; - ~seq# * * BYTE data[128]; - data buffer * * BYTE bc; - block checksum * * } dataPacketRec; * **************************************************************************/unsigned short Checksum(BYTE *, int);int main(int argc,char *argv[]){ FILE *fp; char filename[128]; int ack_reply; int fd; int retry; PacketRec packet; BYTE *ptr; int n; long i; int buflen; unsigned short bc; struct termio savetty; struct termio tdes; if (argc<2) { printf ("Download File Utility Version 1.00\n"); printf ("Copyright (c) Software Engineering 1996-1997. All rights reserved.\n\n"); printf ("Writen by: Long Yunliang . Xi'An China .\n\n"); printf ("Usage: fget TransferFile\n"); return 1; } ioctl (1,TCGETA,&savetty); /* save original tty */ ioctl (1,TCGETA,&tdes); tdes.c_oflag &= ~OPOST; tdes.c_lflag &= ~ECHO; tdes.c_lflag &= ~ICANON; tdes.c_lflag &= ~XCASE; tdes.c_cc[VMIN] = 1; tdes.c_cc[VTIME] = 1; ioctl (1,TCSETA,&tdes); /* set new tty mode */ strcpy (filename,argv[1]); if ((fd = open(filename, O_RDONLY )) < 0) { printf ("File not found .\n"); ioctl (1,TCSETA,&savetty); return (OPENFAIL); /* Open file failed */ } printf ("\x1b[1Y"); /* Support a certain smart phone */ packet.soh = SOH; /* XMODEM transfer file header */ packet.seq = 0; /* initialize seqence# */ for(;;) { ack_reply = getchar(); if (ack_reply == CRC) break; } if ((ack_reply) != CRC) return(DISCONN); while ( buflen = read(fd, packet.buffer, DATALEN) ) { if (buflen < DATALEN) /* end of file */ for (i = buflen;i < DATALEN;i++) packet.buffer[i] = SUB; if (packet.seq == 255) /* next sequence # */ packet.seq = 0; else packet.seq++; packet.nseq = ~packet.seq; packet.bc[0] = 0x00; packet.bc[1] = 0x00; bc = Checksum((BYTE *)&packet.buffer, DATALEN); packet.bc[0] = (bc >> 8) & 0x00FF; packet.bc[1] = bc & 0x00FF; retry = MAXRETRY; do { } while ((ack_reply = getch()) != -1); do { ptr = (BYTE *)&packet; /* Sending packet */ for (n = 0; n < PACKETSIZE; n++, ptr++) { putchar (*ptr); } for (i = 0;i < 10000;i++) { ack_reply = getchar()&0xff; if (ack_reply == CRC) continue; if (ack_reply != 0xff) break; } /* wait for ACK from receiver */ switch (ack_reply) { case NAK: /* packet was received incorrectly */ case 0xff: /* packet/ACK might get lost */ if (--retry == 0) { /* DIS-CONNECT */ ioctl (1,TCSETA,&savetty); return(DISCONN); } break; case ACK: retry = 0; break; } } while (retry > 0); } close(fd); /* Sending EOT */ retry = MAXRETRY; do { if (putchar(EOT) == 0) { close(fd); ioctl (1,TCSETA,&savetty); return(DISCONN); } /* wait for ACK from receiver */ switch (ack_reply = getchar()&0xff) { case NAK: /* packet was received incorrectly */ case 0xff: /* packet/ACK might get lost */ if (--retry == 0) { /* DIS-CONNECT */ ioctl (1,TCSETA,&savetty); return(DISCONN); } break; case ACK: retry = 0; break; } } while (retry > 0); ioctl (1,TCSETA,&savetty); return(SUCCEED); /* Send successfully */}/*************************************************************************** * * * Function: Checksum() - compute crc's * * * * crc-16 is based on the polynomial x^16+x^15+x^2+1 * * The data is assumed to be fed in from least to most significant bit * * crc-ccitt is based on the polynomial x^16+x^12+x^5+1 * * The data is fed in from most to least significant bit * * The prescription for determining the mask to use for a given polynomial * * is as follows: * * 1. Represent the polynomial by a 17-bit number * * 2. Assume that the most and least significant bits are 1 * * 3. Place the right 16 bits into an integer * * 4. Bit reverse if serial LSB's are sent first * * * ***************************************************************************//* function declarations */unsigned short updcrc(unsigned int,int,unsigned int);unsigned short Checksum(BYTE *buf, int buflen){ /* variables */ unsigned int crctt; int ch; int num; crctt=0; for (num=0;num<buflen;num++) { ch=*(buf+num); crctt=updcrc(crctt,ch,CRC_CCITT); } return (crctt);}/* update crc */unsigned short updcrc(unsigned int crc,int c,unsigned int mask){ int i; c<<=8; for(i=0;i<8;i++) { if((crc ^ c) & 0x8000) crc=(crc<<1)^mask; else crc<<=1; c<<=1; } return crc;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -