home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / CTools ƒ / SampleUNIX.C < prev   
Encoding:
C/C++ Source or Header  |  1989-02-05  |  4.2 KB  |  176 lines  |  [TEXT/????]

  1. /* 
  2.  *    A sample of Unix(TM) style source file.
  3.  *    Open with C Tools, select "File Striper" from tools menu.
  4.  *        then check "Convert Unix File" option.
  5.  *    After stripping, run "source formatter" to clean up indentation.
  6.  */
  7.  
  8.  
  9. /*
  10.  * minirb.c By Chuck Forsberg Omen Technology INC
  11.  *        "The High Reliability Communications Software"
  12.  *
  13.  * A bootstrap program for Unix to receive files from computers running
  14.  *  YMODEM Batch (Professional-YAM, PowerCom, ZCOMM, etc.).
  15.  *
  16.  *    Minirb uses system(3) to call stty, avoiding system dependent code.
  17.  *     program strips CR and CPMEOF (^Z) characters (see putsec()).
  18.  *  Please refer to rz.c for comments, etc.
  19.  */
  20. char * Version = "minirb 2.00 05-25-87";
  21.  
  22. #include <stdio.h>
  23. #include <signal.h>
  24. #include <setjmp.h>
  25.  
  26. #define OK 0
  27. #define FALSE 0
  28. #define TRUE 1
  29. #define ERROR (-1)
  30. #define CAN ('X'&037)
  31. #define SOH 1
  32. #define STX 2
  33. #define EOT 4
  34. #define ACK 6
  35. #define NAK 025
  36. #define TIMEOUT (-2)
  37. #define RETRYMAX 9
  38. #define WCEOT (-10)
  39.  
  40. FILE *fout;
  41. long Bytesleft;
  42. int Blklen;
  43. char secbuf[1024];
  44. char linbuf[1024];
  45. int Lleft=0;
  46. jmp_buf tohere;
  47.  
  48. alrm() { longjmp(tohere, -1); }
  49.  
  50. bibi(n) {
  51.  canit(); mode(0);
  52.  fprintf(stderr, "minirb: caught signal %d; exiting", n);
  53.  exit(128+n);
  54. }
  55.  
  56. mode(n) {
  57.  if (n) system("stty raw -echo");
  58.  else system("stty echo -raw");
  59. }
  60.  
  61. main() {
  62.  mode(1);
  63.  if (signal(SIGINT, bibi) == SIG_IGN) {
  64.   signal(SIGINT, SIG_IGN); signal(SIGKILL, SIG_IGN);
  65.  } else {
  66.   signal(SIGINT, bibi); signal(SIGKILL, bibi);
  67.  }
  68.  printf("minirb: Now send file(s) with \042sb file ...\042 command\r\n");
  69.  
  70.  if (wcreceive()==ERROR)
  71.     canit();
  72.  mode(0); exit(0);
  73. }
  74.  
  75. wcreceive() {
  76.  for (;;) {
  77.   if (wcrxpn(secbuf) == ERROR) break;
  78.   if (secbuf[0]==0) return OK;
  79.     if (procheader(secbuf)==ERROR || wcrx()==ERROR) break;
  80.  }
  81.  canit(); return ERROR;
  82. }
  83.  
  84.  
  85. wcrxpn(rpn) char *rpn; {
  86.  register c;
  87.  
  88.  purgeline();
  89. et_tu:
  90.  sendline(NAK); Lleft=0;
  91.  while ((c = wcgetsec(rpn, 100)) != 0) {
  92.   if (c == WCEOT) { sendline(ACK); Lleft=0; readline(1); goto et_tu; }
  93.   return ERROR;
  94.  }
  95.  sendline(ACK); return OK;
  96. }
  97.  
  98. wcrx() {
  99.  register int sectnum, sectcurr, sendchar, cblklen;
  100.  
  101.  sectnum=0; sendchar=NAK;
  102.  for (;;) {
  103.   sendline(sendchar); Lleft=0;
  104.   sectcurr=wcgetsec(secbuf, 50);
  105.   if (sectcurr==(sectnum+1 & 0377)) {
  106.      sectnum++; cblklen = Bytesleft>Blklen ? Blklen:Bytesleft;
  107.    putsec(secbuf, cblklen);
  108.    if ((Bytesleft-=cblklen) < 0) Bytesleft = 0;
  109.    sendchar=ACK;
  110.   }
  111.   else if (sectcurr==(sectnum&0377)) sendchar=ACK;
  112.   else if (sectcurr==WCEOT) {
  113.     if (fclose(fout)==ERROR) return ERROR;
  114.    sendline(ACK); Lleft=0; return OK;
  115.   }
  116.   else if (sectcurr==ERROR) return ERROR;
  117.   else return ERROR;
  118.  }
  119. }
  120.  
  121. wcgetsec(rxbuf, maxtime) char *rxbuf; int maxtime; {
  122.  register checksum, wcj, firstch; register char *p; int sectcurr, errors;
  123.  for (errors=0; errors<RETRYMAX; errors++) {
  124.     if ((firstch=readline(maxtime))==STX) { Blklen=1024; goto get2; }
  125.     if (firstch==SOH) {
  126.    Blklen=128;
  127. get2:
  128.     sectcurr=readline(1); checksum=0;
  129.      if ((sectcurr+(readline(1)))==0377) {
  130.     for (p=rxbuf,wcj=Blklen; --wcj>=0; ) {
  131.        if ((firstch=readline(1)) < 0) goto bilge;
  132.        checksum += (*p++ = firstch);
  133.      }
  134.     if ((firstch=readline(1)) < 0) goto bilge;
  135.       if (((checksum-firstch)&0377)==0) return sectcurr;
  136.     }
  137.     }
  138.     else if (firstch==EOT) return WCEOT;
  139.   else if (firstch==CAN) return ERROR;
  140. bilge:
  141.   while(readline(1)!=TIMEOUT)
  142.     ;
  143.     maxtime=40; sendline(NAK); Lleft=0;
  144.  }
  145.  canit(); return ERROR;
  146. }
  147.  
  148. readline(timeout) int timeout; {
  149.  register n; static char *cdq;
  150.  
  151.  if (--Lleft >= 0) return (*cdq++ & 0377);
  152.  n = timeout/10;
  153.  if (n < 2) n = 3;
  154.  if (setjmp(tohere)) { Lleft = 0; return TIMEOUT; }
  155.  signal(SIGALRM, alrm); alarm(n);
  156.  Lleft=read(0, cdq=linbuf, 1024); alarm(0);
  157.  if (Lleft < 1) return TIMEOUT;
  158.  --Lleft; return (*cdq++ & 0377);
  159. }
  160.  
  161. purgeline() { Lleft = 0; lseek(0, 0L, 2); }
  162.  
  163.  
  164. procheader(name) char *name; {
  165.  register char *p;
  166.  
  167.  Bytesleft = 2000000000L; p = name + 1 + strlen(name);
  168.  if (*p) sscanf(p, "%ld", &Bytesleft);
  169.  if ((fout=fopen(name, "w")) == NULL) return ERROR;
  170.  return OK;
  171. }
  172.  
  173. putsec(p, n) char *p; int n;
  174. { for (; --n>=0; ++p) if (*p != 015 && *p != 032) putc(*p, fout); }
  175.  
  176. sendline(c) { char d; d = c; write(1, &d, 1); }
  177.  
  178. char canistr[] = { 24,24,24,24,24,24,24,24,0 };
  179.  
  180. canit() { printf(canistr); Lleft=0; }
  181.  
  182. /* END of minirb.c */
  183.  
  184.