home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / c-kermit / ckdenco.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  6KB  |  256 lines

  1. #ifndef lint
  2. static char sccsid[] = "@(#)uuencode.c    5.3-1 (Berkeley) 1/22/85";
  3. #endif
  4. /* modified by ajr to include checksums */
  5.  
  6. /* Adapted by Phil Julian, SAS Institute, Inc., for use on the Data General
  7.  * minicomputers.  #ifdefs surround host-dependent code.
  8.  * 27 May 1987
  9.  */
  10.  
  11. /*
  12.  * uuencode [input] output
  13.  *
  14.  * Encode a file so it can be mailed to a remote system.
  15.  */
  16. #include <stdio.h>
  17.  
  18. #ifdef unix
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #endif
  22.  
  23. #ifdef datageneral
  24. #include <sys_calls.h>
  25. #include <packets/filestatus.h>         /* Used for ?GNFN */
  26. #include <packets:normal_io.h>
  27. #include <paru.h>
  28. struct p_nio_ex w_io_parms;             /* ?write system call structure */
  29. P_FSTAT buf; 
  30. int ac0,ac2;
  31. char name[256];
  32. #define R_ACC 4
  33. #ifdef putc
  34. #undef putc
  35. #endif
  36. #define putc(c,file) { char ch = (char) (c); dg_binw(fchannel(file),&ch,1); }
  37. #endif
  38.  
  39. #define SUMSIZE 64
  40.  
  41. /* ENC is the basic 1 character encoding function to make a char printing */
  42. #define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
  43.  
  44. main(argc, argv)
  45. char **argv;
  46. {
  47.     FILE *in;
  48. #ifdef unix
  49.     struct stat sbuf;
  50. #endif
  51.     int mode;
  52.  
  53.     /* optional 1st argument */
  54. #ifdef datageneral
  55.         name[0] = 0;            /* Signal that there is not filename */
  56.     if (argc > 2) {
  57.                 strcpy (name,argv[1]);
  58.         if ((in = fopen(argv[1], "j")) == NULL) {
  59. #else
  60.     if (argc > 2) {
  61.         if ((in = fopen(argv[1], "r")) == NULL) {
  62. #endif
  63.             perror(argv[1]);
  64.             exit(1);
  65.         }
  66.         argv++; argc--;
  67.     } else
  68.         in = stdin;
  69.  
  70.     if (argc != 2) {
  71. #ifdef datageneral
  72.         printf("Usage: x uuencode/l=localname [infile] remotefile\n");
  73. #else
  74.         printf("Usage: uuencode [infile] remotefile\n");
  75. #endif
  76.         exit(2);
  77.     }
  78.  
  79. #ifdef unix
  80.     /* figure out the input file mode */
  81.     fstat(fileno(in), &sbuf);
  82.     mode = sbuf.st_mode & 0777;
  83. #else
  84. #ifdef datageneral
  85.         /* Initialize the i/o block for putc() */
  86.         zero((char *) &w_io_parms, sizeof(w_io_parms));
  87.         w_io_parms.isti = $IBIN|$RTDY|$ICRF|$OFOT;
  88.         w_io_parms.isti &= ~$IPST;
  89.         w_io_parms.imrs = 2048;
  90.         w_io_parms.ibad = -1;
  91.         w_io_parms.ircl = -1;
  92.  
  93.         if (name[0]) {
  94.             /* For some reason, the DG croaks intermittently with a call to 
  95.              * stat(), because the directory /etc/passwd may not exist.  We 
  96.              * use ?fstat to circumvent this bug.
  97.              */
  98.  
  99.             ac0 = (int) name;  ac2 = (int) &buf;
  100.             if (sys_fstat(ac0,0,ac2)) { perror("sys_fstat:"); exit(-1); }
  101.  
  102.             /* buf.styp_type is the file format field.
  103.              * buf.styp_format is the record format field.
  104.              *
  105.              * buf.scps is the record length if the type is fixed -- if you
  106.              * need to use fixed records, then you would have to use a sys_open
  107.              * to have control over the ircl value, or you would have to use
  108.              * a sys_create call.  I have chosen only to use dg_open and 
  109.              * simplify the process, since fixed files are seldom used and are
  110.              * seldom binary.
  111.              */
  112.  
  113.             if ((buf.styp_type >= $LDIR) && (buf.styp_type <= $HDIR)) {
  114.                 fprintf(stderr,"Illegal file type for encode!");
  115.                 exit(-2);
  116.             }
  117.             if (access(name,R_ACC) < 0) {         /* Is the file accessible? */
  118.                 perror("Access failed:");
  119.                 exit(-3);            
  120.             } 
  121.         }
  122. #else
  123.     mode = 0777;
  124. #endif
  125. #endif
  126. #ifdef datageneral
  127.     {
  128.         char temp[512]; int i;
  129.         /* No format defaults to an undefined record format */
  130.         if (buf.styp_format == 0) buf.styp_format = $RTUN;
  131.     sprintf(temp, "begin %#o %s %#o\n", 
  132.                   buf.styp_format, argv[1], buf.styp_type);
  133.         dg_binw (fchannel(stdout), temp, strlen(temp));
  134.     }
  135. #else
  136.     printf("begin %o %s\n", mode, argv[1]);
  137. #endif
  138.  
  139.     encode(in, stdout);
  140.  
  141. #ifdef datageneral
  142.         dg_binw (fchannel(stdout), "end\n", 4);
  143. #else
  144.     printf("end\n");
  145. #endif
  146.     exit(0);
  147. }
  148.  
  149. /*
  150.  * copy from in to out, encoding as you go along.
  151.  */
  152. encode(in, out)
  153. FILE *in;
  154. FILE *out;
  155. {
  156.     char buf[80];
  157.     int i, n, checksum;
  158.  
  159.     for (;;) {
  160.         /* 1 (up to) 45 character line */
  161.         n = fr(in, buf, 45);
  162.         putc(ENC(n), out);
  163.  
  164.         checksum = 0;
  165.         for (i=0; i<n; i += 3)
  166.             checksum = (checksum+outdec(&buf[i], out)) % SUMSIZE;
  167.  
  168.         putc(ENC(checksum), out);
  169.         putc('\n', out);
  170.         if (n <= 0)
  171.             break;
  172.     }
  173. }
  174.  
  175. /*
  176.  * output one group of 3 bytes, pointed at by p, on file f.
  177.  * return the checksum increment.
  178.  */
  179. int outdec(p, f)
  180. char *p;
  181. FILE *f;
  182. {
  183.     int c1, c2, c3, c4;
  184.  
  185.     c1 = *p >> 2;
  186.     c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  187.     c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  188.     c4 = p[2] & 077;
  189.     putc(ENC(c1), f);
  190.     putc(ENC(c2), f);
  191.     putc(ENC(c3), f);
  192.     putc(ENC(c4), f);
  193.  
  194.     return((p[0]+p[1]+p[2]) % SUMSIZE);
  195. }
  196.  
  197. /* fr: like read but stdio */
  198. int
  199. fr(fd, buf, cnt)
  200. FILE *fd;
  201. char *buf;
  202. int cnt;
  203. {
  204.     int c, i;
  205.  
  206.     for (i=0; i<cnt; i++) {
  207.         c = getc(fd);
  208.         if (c == EOF)
  209.             return(i);
  210.         buf[i] = c;
  211.     }
  212.     return (cnt);
  213. }
  214.  
  215. #if (!unix && MANX)
  216.  
  217. perror (sp)
  218. char *sp;
  219. {
  220.     if (sp && *sp) {
  221.         fprintf (stderr, "%s: ");
  222.     }
  223.     fprintf (stderr, "<unknown error>\n");
  224. }
  225.  
  226. #endif    /* Unix */
  227.  
  228.  
  229. #ifdef datageneral
  230. /* D G _ B I N W -- Output len characters to the file number filenum 
  231.  * 
  232.  *  The syntax is like the Unix write command.
  233.  *  This code was borrowed from my Kermit source -- ckdtio.c
  234.  */
  235.  
  236. dg_binw(channel,chs,len) int channel, len; char *chs; 
  237. {
  238.      int ac2,err;
  239.  
  240.      if (len == 0) return(0);
  241.  
  242.      w_io_parms.ich = channel;
  243.      w_io_parms.ibad = chs;
  244.      w_io_parms.ircl = len;
  245.      ac2 = &w_io_parms;
  246.      
  247.      if ((err = sys_write(ac2)) == 0) return(0);
  248.  
  249.      if ( err != ERLTL && err != EREOF ) {
  250.           perror("dg_binw: sys_write ");
  251.           exit(err);
  252.      }
  253. }
  254. #endif
  255.  
  256.