/* UNIX support routines for file transfer Support routines for microcomputer file transfer between UNIX and UCSD (TTY program) Ann Macintosh August 1980 Give file protocol * is the escape character *B indicates beginning of file *E indicates end of file *C indicates a '*' char within the file ** indicates abnormal termination */ #include #include #include #define GTTY TIOCGETA #define STTY TIOCSETA #define TRUE 1 #define FALSE 0 #define MAXNAME 40 #define ULINE '_' #define GT '>' #define QUOTE '\'' #define SLASH '/' FILE *tf; char ch, file[MAXNAME]; struct sgttyb oldmodes, newmodes; int TERMS; int terms_reset(); main() { int i, chcount; TERMS = FALSE; #ifdef DEBUG testroutine(0); #endif signal(SIGINT, terms_reset); i=0; printf("Give file:"); getchar(); #ifdef DEBUG testroutine(4); #endif while(( ch = getchar()) != QUOTE ) { if ( ch == GT ) ch = ULINE; file[i] = ch; #ifdef DEBUG testroutine(4); #endif if (i == MAXNAME) error(1); i++; } file[i] = NULL; #ifdef DEBUG testroutine(1); #endif while ( getchar() != '\n'); terms_set(); openfile(); #ifdef DEBUG testroutine(5); #endif printf("*B"); #ifdef DEBUG testroutine(6); #endif chcount = 0; while(( ch =getc(tf)) != EOF) { if ( ch == '*' ) printf("*C"); else putchar(ch); chcount++; #ifdef DEBUG testroutine(7); #endif } #ifdef DEBUG testroutine(8); #endif printf("*E\n"); #ifdef DEBUG testroutine(9); #endif fclose(tf); #ifdef DEBUG testroutine(10); #endif printf("Give file:"); #ifdef DEBUG testroutine(13); #endif getchar(); /*wait for micro*/ #ifdef DEBUG testroutine(11); #endif printf("** Host:"); printf("%d characters sent.\r\n"); terms_reset(); #ifdef DEBUG testroutine(12); #endif } openfile() { int i, length; int sbuf[60]; #ifdef DEBUG testroutine(2); #endif i=0; length = 0; while(file[i] != NULL) { if (file[i] != SLASH) { length++; if (length > 14) error(2); } else length = 0; i++; } if ( stat(file,sbuf) < 0) error(3); if (( tf = fopen(file, "r")) == NULL) error(4); } error(flg) int flg; { #ifdef DEBUG testroutine(3); #endif switch (flg) { case 1: printf("** Host: pathname greater than %d chars.\r\n", MAXNAME); break; case 2: printf("** Host: name greater than 14 chars.\r\n"); break; case 3: printf("** Host: file %s does not exist.\r\n", file); break; case 4: printf("** Host: unable to open %s .\r\n", file); break; } #ifdef DEBUG testroutine(14); #endif printf("Give file:"); getchar(); #ifdef DEBUG testroutine(15); #endif terms_reset(); exit(); } terms_set() { ioctl(0,GTTY,&oldmodes); ioctl(0,GTTY,&newmodes); newmodes.sg_flags = (CBREAK|ODDP|EVENP|TANDEM); newmodes.sg_length = newmodes.sg_width = 0; TERMS = TRUE; ioctl(0,STTY,&newmodes); } terms_reset() { if ( TERMS ) { TERMS = FALSE; ioctl(0,STTY,&oldmodes); } } testroutine(flag) int flag; { static FILE *er; switch(flag) { case 0: er = fopen("/tmp/take.er","w"); break; case 1: fprintf(er, "filename received\n"); break; case 2: fprintf(er, "start of open file\n"); break; case 3: fprintf(er, "start of error routine\n"); break; case 4: putc(ch ,er); break; case 5: fprintf(er,"end of openfile\n"); break; case 6: fprintf(er,"after *B \n"); break; case 7: putc(ch, er); break; case 8: fprintf(er, "before E \n"); break; case 9: fprintf(er, "after E \n"); break; case 10: fprintf(er, "after closing take.test on unix\n"); break; case 11: fprintf(er, "after receiving last ch from micro\n"); break; case 12: fprintf(er, "at end of givefile program\n"); break; case 13: fprintf(er,"prompt givefile sent \n"); break; case 14: fprintf(er, "in error routine before getchar\n"); break; case 15: fprintf(er, "after getchar in error routine\n"); break; } fflush(er); }