home *** CD-ROM | disk | FTP | other *** search
/ Hall of Fame / HallofFameCDROM.cdr / proglc / x_transf.lzh / XR.C < prev   
Encoding:
C/C++ Source or Header  |  1986-12-17  |  2.7 KB  |  114 lines

  1. /* xr.c - remote xmodem functions for xenix/unix
  2.  * copyright 1986 Maple Lawn Farm, Inc.
  3.  * usage: xr|xt [-ct] [-d errfile] file
  4.  * -c    crc (instead of checksum), -t    text mode (CR-NL <-> NL)
  5.  * compile:    cc -i -O -s xr.c xmodem.c -o xr
  6.  *            ln xr xt
  7.  * To avoid overwriting existing files, received file with same name
  8.  * as an existing file is stored as fname~. */
  9.  
  10. #include <signal.h>
  11. #include <stdio.h>
  12. #include <fcntl.h>
  13. #include <sys/ioctl.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <termio.h>
  17.  
  18. #define    DEBUG       01
  19. #define    LF          02
  20. #define    CRC         04
  21. #define    BSIZE       128
  22. #define errx(m,f)   printf("%s: ", pname), \
  23.                   printf(m, f), \
  24.                   printf("\n"), \
  25.                   exit(1)
  26.  
  27. FILE    *errf;
  28.  
  29. struct    termio    old,
  30.                 new;
  31.  
  32. hangup()
  33. {
  34.     resetline();
  35.     exit(1);
  36. }
  37.  
  38. main(ac, av)
  39. int    ac;
  40. char    **av;
  41. {
  42.     char    *pname, 
  43.                 trans = (av[0][strlen(av[0]) -1] == 't'),
  44.                 *fname;
  45.     int    c, opts = 0; 
  46.     struct    stat    stbuf;
  47.     extern    int    optind;
  48.     extern    char    *optarg;
  49.     FILE    *fp;
  50.  
  51.     pname = *av;
  52.     while ((c = getopt(ac, av, "ctd:?")) != EOF)
  53.           switch (c)  {
  54.           case 't' :
  55.               opts |= LF;
  56.               break;
  57.           case 'c' :
  58.               opts |= CRC;
  59.               break;
  60.           case 'd' :
  61.               opts |= DEBUG;
  62.               if (!(errf = fopen(optarg, "w")))
  63.                     errx("can't open %s", optarg);
  64.               setbuf(errf, NULL);
  65.               break;
  66.           case '?' :
  67.               printf("usage: %s [-ct] [-d errfile] file\n", pname);
  68.               exit(1);
  69.           }
  70.     if (ac == 1 || ac == optind) 
  71.           errx("need file name", NULL);
  72.     fname = av[optind];
  73.     if (trans && !(fp = fopen(fname, "r"))) 
  74.           errx("can't open %s", fname);
  75.     if (!trans) {
  76.           if (!access(fname, 0))
  77.               strcat(fname, "~");
  78.             if (!(fp = fopen(fname, "w")))
  79.               errx("can't write %s", fname);
  80.     }
  81.  
  82.     printf("Ready to %s %s\n", (trans) ? "send":"receive", fname);
  83.     if (trans) {
  84.           stat(fname, &stbuf);
  85.           printf("%d blocks (128 bytes/block)\n",
  86.             stbuf.st_size/BSIZE+1);
  87.     }
  88.     printf("Ctrl-X to abort transfer\n");
  89.  
  90.     signal(SIGINT, SIG_IGN);
  91.     signal(SIGQUIT, SIG_IGN);
  92.     signal(SIGHUP, hangup);
  93.  
  94.         ioctl(1, TCGETA, &old);
  95.         ioctl(1, TCGETA, &new);
  96.     fflush(stdin);
  97.         new.c_iflag = IGNBRK|IGNPAR;
  98.         new.c_oflag = 0;
  99.         new.c_lflag = 0;
  100.         new.c_cc[4] = 1;
  101.         new.c_cflag &= ~PARENB;
  102.         new.c_cflag |= CS8;
  103.         ioctl(1, TCSETAW, &new);
  104.  
  105.     (trans) ? xput(fp, opts) : xget(fp, opts);
  106. }
  107.  
  108.  
  109. resetline()
  110. {
  111.     ioctl(1, TCSETA, &old);
  112. }
  113.  
  114.