?? ecusea.c
字號:
if(sf_nakquan > 10) goto CANCEL_TRANSFER; } if(endblk) /* if sending file, not EOT */ fclose(fp); return(1); /* exit with good status */CANCEL_TRANSFER: if(endblk) /* if sending file, not EOT */ fclose(fp); xmit_cancel(); return(0); /* exit with bad status */} /* end of send_file *//*+------------------------------------------------------------------------- set_utime_1980(filename,secs_since_1980)--------------------------------------------------------------------------*/voidset_utime_1980(filename,secs_since_1980)char *filename; /* file to set stamp on */long secs_since_1980;{time_t times[2];time_t time(); times[0] = time((long *) 0); /* accessed */ times[1] = secs_since_1980 + OFFSET_1980; /* modified (convert time) */ utime(filename,times);} /* end of set_utime_1980 *//*+------------------------------------------------------------------------- receive_block(buf) - get block from linereturn 0 if good chk/CRC, 1 if bad--------------------------------------------------------------------------*/intreceive_block(buf)char *buf; /* data buffer */{register unsigned int rdchar; register unsigned short rUINT16 = 0; /* calculated CRC or check value */int itmp;int timeout = no_ack_mode ? 200 : 5; /* short block timeout */unsigned short rcvd_crc; /* received CRC or check value */ itmp = 128; while(itmp--) { if((rdchar = lgetc_timeout(timeout)) == TIMEOUT) return(1); if(crc_in_use) rUINT16 = crc_update(rdchar,rUINT16); else rUINT16 += rdchar; *buf++ = rdchar; } if(crc_in_use) { rUINT16 = crc_update(0,rUINT16); rUINT16 = crc_update(0,rUINT16); rdchar = lgetc_timeout(timeout); rcvd_crc = (rdchar << 8) | lgetc_timeout(timeout); } else { rUINT16 &= 0xFF; rcvd_crc = lgetc_timeout(timeout) & 0xFF; } if(rUINT16 != rcvd_crc) { sprintf(s128,"bad %s calc=%04x rcvd=%04x", crc_in_use ? "CRC" : "checksum",rcvd_crc,rUINT16); report_str(s128,-1); } return(rUINT16 != rcvd_crc);} /* end of receive_block *//*+------------------------------------------------------------------------- receive_file()--------------------------------------------------------------------------*/char *receive_file(){int rdchar; /* received character */int tries; /* retry counter */int blknum; /* desired block number */int inblk; /* this block number */FILE *fp;char buf[128]; /* data buffer */char tmpname[100]; /* name of temporary file */static char outname[100]; /* name of final file */BLK0 blk0; /* file header data storage */int endblk; /* block number of EOT, if known */long left = 0; /* bytes left to output */int itmp; /* index */int cnvrt; /* flag -- convert filename? */char *onp; /* use to convert filename to l / rdchar */long ftell(); *outname = '\0'; /* get name from transmitter */ cnvrt = 1; /* convert to local is necessary */ sprintf(tmpname,"./SEA%05d.tmp",getpid()); /* use a unique temp filename */ if(!(fp = fopen(tmpname,"w"))) { /* open temporary file */ sprintf(s128,"Cannot create temp file %s\n",tmpname); report_str(s128,0); xmit_cancel(); return(NULL); } blknum = 0; tries = -10; /* kludge for first time around */ crc_in_use = 1; /* try for CRC error checking */ error_count = 0; /* no errors yet */ endblk = 0; /* we don't know the size yet */ no_ack_mode = 0; /* we don't know about this yet */ memset((char *)&blk0,0,sizeof(blk0)); /* or much of anything else */ report_protocol_crc_type("/CRC16");SEND_NAK: /* we got a bad block */ if(blknum > 1) { error_count++; report_str("bad block",1); } if(++tries > 10) goto CANCEL_TRANSFER; if(tries == 0) /* if CRC isn't going */ { crc_in_use = 0; /* then give checksum a try */ report_protocol_crc_type("/CHK"); } xmit_nak(blknum); /* send the NAK */ if(no_ack_mode && error_count > 20) { /* if no_ack_mode mode isn't working */ no_ack_mode = 0; /* then shut it off */ report_str("Overdrive disengaged",0); }RECEIVE_NEXT_BLOCK: /* start of "get a block" */ report_rxpos(ftell(fp)); while((rdchar = lgetc_timeout(30)) != TIMEOUT) { if(rdchar == CAN) { if((rdchar = lgetc_timeout(30)) == CAN) { xmit_cancel(); return(NULL); } break; } if(rdchar == EOT) { if(!endblk || endblk == blknum) goto RECEIVE_EOT_SEEN; } else if(rdchar == SOH) { if((inblk = lgetc_timeout(5)) == TIMEOUT) goto SEND_NAK; if(lgetc_timeout(5) == (inblk ^ 0xFF)) { sprintf(s128,"receiving %d",inblk); report_last_rxhdr(s128,0); goto GOT_START_OF_BLOCK; /* we found a start */ } } } goto SEND_NAK;GOT_START_OF_BLOCK: /* start of block detected */ rdchar = blknum & 0xFF; if(inblk == 0 && blknum <= 1) { /* if this is the header */ if(receive_block((char *)&blk0)) goto SEND_NAK; /* bad header block */ else { xmit_ack(inblk); /* ack the header */#if defined(M_UNIX) if(fname_too_long(blk0.filename)) { strcpy(s128,"truncated: "); strncat(s128,blk0.filename,sizeof(s128) - 12); report_str(s128,-1); strcpy(outname,fname_truncated()); } else#endif strcpy(outname,blk0.filename); report_file_rcv_started(outname,blk0.length, blk0.secs_since_1980 + OFFSET_1980); if(left = blk0.length) /* length to transfer */ endblk=(int)((left + 127L)/128L)+1; if(no_ack_mode != blk0.send_no_acks) { sprintf(s128,"Overdrive %sengaged", (blk0.send_no_acks) ? "" : "dis"); report_str(s128,0); } no_ack_mode = blk0.send_no_acks; blknum = 1; /* now we want first data block */ goto RECEIVE_NEXT_BLOCK; } } if(inblk == rdchar) { /* if this is the one we want */ if(!receive_block(buf)) { /* else if we get it okay */ if(!no_ack_mode) /* if we're sending ACKs */ xmit_ack(inblk); /* then ACK the data */ for(itmp = 0; itmp < 128; itmp++) { if(endblk) { /* limit file size if known */ if(!left) break; left--; } if(fputc(buf[itmp],fp) == EOF) { report_str("FILE WRITE ERROR",0); goto CANCEL_TRANSFER; } } tries = 0; /* reset try count */ blknum++; /* we want the next block */ goto RECEIVE_NEXT_BLOCK; } goto SEND_NAK; /* ask for a resend */ } if(inblk < rdchar || inblk > rdchar + 100) { /* if resending what we have */ receive_block(buf); /* ignore it */ xmit_ack(inblk); /* but ack it */ } goto RECEIVE_NEXT_BLOCK; /* else if running ahead */RECEIVE_EOT_SEEN:#ifdef NAKEOT xmit_nak(blknum); /* NAK the EOT, make sure */ if(lgetc_timeout(20) != EOT) /* we're all done */ goto SEND_NAK;#endif /* NAKEOT */ xmit_ack(blknum); /* ACK it and clean up */ report_last_rxhdr("EOT",0); if(blknum > 1) { /* if we really got anything */ fclose(fp); unlink(outname); /* rename temp to proper name */ for(onp = outname;cnvrt && *onp;onp++) /* find out if there's lower- */ if(islower(*onp)) /* case letters filename */ cnvrt = 0; /* there are, don't convert */ if(cnvrt) /* if there aren't, make all */ for(onp = outname;*onp;onp++) /* into uppercase */ *onp = tolower(*onp); if(link(tmpname,outname) == 0) unlink(tmpname); if(blk0.secs_since_1980) /* set stamp, if known */ set_utime_1980(outname,blk0.secs_since_1980); return(outname); } else { /* else no real file */ fclose(fp); unlink(tmpname); /* discard empty file */ report_str("end of transfer",0); rf_done = 1; return(NULL); }CANCEL_TRANSFER: fclose(fp); xmit_cancel(); rf_done = 2; return(NULL);} /* end of receive_file *//*+------------------------------------------------------------------------- cancel_transaction(sig)--------------------------------------------------------------------------*/SIGTYPEcancel_transaction(sig)int sig;{ xmit_cancel(); sprintf(s128,"signal %d ... exiting",sig); report_str(s128,1);/* report_rx_ind(0); report_tx_ind(0);*/ report_uninit(); if(sig == SIGQUIT) abort(); exit(128+sig);} /* end of cancel_transaction *//*+------------------------------------------------------------------------- getspeed(code)--------------------------------------------------------------------------*/struct B_to_baud { unsigned baud; int B_code; };unsignedgetspeed(code)int code;{register itmp;static struct B_to_baud speeds[] = { 50, B50, 75, B75, 110, B110, 300, B300, 600, B600, 1200, B1200, 2400, B2400, 4800, B4800, 9600, B9600, 19200, EXTA, 38400, EXTB, 0}; code &= CBAUD; for(itmp = 0; speeds[itmp].baud; itmp++) if(speeds[itmp].B_code == code) return(speeds[itmp].baud); return(38400); /* Assume fifo if ioctl failed */} /* end of getspeed *//*+------------------------------------------------------------------------- main(argc,argv,envp)--------------------------------------------------------------------------*/main(argc,argv,envp)int argc;char **argv;char **envp;{int ipaths;int ok = 0;#define MAX_PATHS 512char *paths[MAX_PATHS];char **ppaths = paths;char *cptr;char **gargv = argv;int gargc = argc; exit_code = 254; while(--argc) { cptr = *++argv; if(*cptr == '-') { cptr++; switch(*cptr++) { case ',': log_packets = 1; break; case '/': if(--argc < 1) exit(255); strcpy(curr_dir,*++argv); break; case '.': if(--argc < 1) exit(255); iofd = atoi(*++argv); break; case 'r': sending_flag = 0; break; case 's': sending_flag = 1; } } else if(argc > 0) { if(npaths < MAX_PATHS) { *ppaths++ = cptr; npaths++; } else { printf("too many filenames to send\n"); exit(255); } } } if(sending_flag == -1) { printf("no -r or -s\n"); exit(255); } if((npaths < 1) && sending_flag) exit(253); if(npaths && !sending_flag) exit(255); if(log_packets) { char log_packets_name[64]; FILE *ftmp; int iargv; sprintf(log_packets_name,"/tmp/sea%05d.plog",getpid()); unlink(log_packets_name); ftmp = fopen(log_packets_name,"w"); fclose(ftmp); log_packets = open(log_packets_name,O_WRONLY,0644); if(log_packets < 0) log_packets = 0; else { write(log_packets,"exec: ",6); for(iargv = 0; iargv < gargc; iargv++) { write(log_packets,gargv[iargv],strlen(gargv[iargv])); write(log_packets," ",1); } write(log_packets,"\n",1); } } sprintf(s128,"ecusea %s",revision); report_init(s128); report_top_line("System Enhancement Associates"); signal(SIGHUP,cancel_transaction); signal(SIGQUIT,cancel_transaction); signal(SIGINT,cancel_transaction); signal(SIGTERM,cancel_transaction);#if defined(SIGSTOP) /* * call Roto-Rooter on POSIX plots */ signal(SIGSTOP,SIG_IGN); signal(SIGTSTP,SIG_IGN); signal(SIGCONT,SIG_IGN); signal(SIGTTIN,SIG_IGN); signal(SIGTTOU,SIG_IGN);#endif ioctl(iofd,TCGETA,&tio0); tio = tio0; tio.c_oflag = 0; tio.c_cflag &= ~PARENB; tio.c_cflag &= ~CSIZE; tio.c_cflag |= CS8; /* * learn tick rate for various timers */ init_Nap(); baud_rate = getspeed(tio.c_cflag); ioctl(iofd,TCSETA,&tio); report_line(baud_rate,"RAW"); switch(sending_flag) { case 0: /* receive files */ while(receive_file() != NULL) Nap(1000L); ok = (rf_done == 1); break; case 1: /* send files */ ipaths = 0; while(ipaths < npaths) { if(!(ok = send_file(paths[ipaths]))) break; Nap(1000L); ipaths++; } if(ok) /* no errors, send end marker */ send_file(""); report_str("end of transfer",0); break; } ioctl(iofd,TCSETA,&tio0); report_line(baud_rate,"NORMAL"); report_uninit(); exit(ok ? 0 : exit_code); /* and return error status */} /* end of main */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -