/* SREC_MX: receiving file via terminal line compatible to bruce carbreys sreceive/ssend System: MINIX R. Henze 01.04.91 last edit: 27.06.91 */ #include #include #include /*tty*/ #include /*zeitumrechnungen*/ #include /*setzen file date and time*/ #define bufsz 0x0400 #define max_retry 5 #define nak 0x15 #define ack 0x06 #define cntrz 0x1a #define esc 0x1b #define true 1 #define false 0 #define cr 0x0d #define lf 0x0a #define f2 0xbc unsigned char bcheck; unsigned char checksum; unsigned char retry; unsigned int blksz; unsigned int bnum; unsigned int blknum; unsigned char dummy; unsigned int deflt; unsigned char chr; unsigned char buf[bufsz]; unsigned char ib; int file; unsigned int i; int input_fd = 0; unsigned char chr; unsigned char ib; struct sgttyb old_tty; struct sgttyb new_tty; time_t fdtime; /*long*/ int flag; /*---------------------------------*/ void quit() { close(file); exit(1); } /*---------------------------------*/ unsigned char get_com() { unsigned char chr; read(input_fd,&chr,1); return(chr); } /*---------------------------------*/ unsigned char get_com_chk() { unsigned char chr; chr = get_com(); checksum = checksum ^ chr; return(chr); } /*---------------------------------*/ int send_com(chr) unsigned char chr; { if (write(input_fd, &chr, 1)) return(true); else return(false); } /*---------------------------------*/ void put_com(chr) unsigned char chr; { while(!send_com(chr)) ; } /*---------------------------------------------------------------------*/ void input_blk() { unsigned int k; retry = 0; if (blknum>1) put_com(ack); do checksum = 0; bnum = get_com_chk(); i = get_com_chk(); bnum = bnum | (i<<8); if (blknum != bnum) quit(); blksz = get_com_chk(); i = get_com_chk(); blksz = blksz | (i<<8); k = 0; i = 0; while (i 0) ; if (blksz==0) put_com(ack); return; retry++; put_com(nak); while (retry < max_retry); quit(); } /* ------------------------------------------------------------------------ */ /* --- file date and time ------------------------------------------------- */ /* ------------------------------------------------------------------------ */ struct tm tmtime; /*integer-struktur fuer datum und zeit*/ struct utimbuf utim; /*fuer utime(): long acttime, long modtime*/ void getftimdat() { tmtime.tm_sec = get_com_chk(&dummy); tmtime.tm_min = get_com_chk(&dummy); tmtime.tm_hour = get_com_chk(&dummy); tmtime.tm_mday = get_com_chk(&dummy); tmtime.tm_mon = get_com_chk(&dummy); tmtime.tm_year = get_com_chk(&dummy); //fdtime = mktime(tmtime); } /* ------------------------------------------------------------------------ */ void main(argc,argv) int argc; char *argv[]; { ioctl(input_fd,TIOCGETP,&old_tty); ioctl(input_fd,TIOCGETP,&new_tty); new_tty.sg_flags = new_tty.sg_flags | RAW; /* RAW */ new_tty.sg_flags = new_tty.sg_flags | BITS8; /* 8 bit */ new_tty.sg_flags = new_tty.sg_flags & (ECHO ^ 0xffff); /* no ECHO */ new_tty.sg_flags = new_tty.sg_flags & (CRMOD ^ 0xffff); /*no lf to crlf*/ new_tty.sg_flags = new_tty.sg_flags & (XTABS ^ 0xffff); /*no tab expansion*/ ioctl(input_fd,TIOCSETP,&new_tty); printf("\nserial port file receiver"); flag = false; if (argc < 2) printf("\nusage: SSEN_MX filename [flag]"); exit(1); if (argc > 2) if (*(argv[2]) == 'a') flag = true; if (send_com(f2)) do chr = get_com_chk(&dummy); while(chr != ack); i = 0; do put_com(*(argv[1]+i)); i++; while (*(argv[1]+i-1) != 0); umask(0); file = creat(argv[1],0x0180); if (file == -1) quit(); put_com(ack); getftimdat(); do chr = get_com_chk(&dummy); while(chr != ack); //printf ("\nreceiving "); //printf ("\n%s ",argv[1]); //printf ("\n exits!"); put_com(ack); blknum=0; /* block sequence #*/ do blknum++; input_blk() ; if (blksz > 0) write(file,buf,blksz); while (blksz > 0); utim.actime = fdtime; utim.modtime = fdtime; utime(argv[1], utim); close(file); printf ("\nfile successfully received!"); ioctl(input_fd,TIOCSETP,&old_tty); } /*---------------------- end of file srec_mx.i -----------------------------*/