home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / draco / draco-1.ark / UUENCODE.DRC < prev    next >
Encoding:
Internet Message Format  |  1986-11-13  |  4.6 KB

  1. Received: from seismo.css.gov by AMSAA.ARPA id a013548; 15 Oct 86 19:05 EDT
  2. Received: from ubc-vision.UUCP by seismo.CSS.GOV (5.54/1.14)
  3.     id AA09291; Wed, 15 Oct 86 18:29:23 EDT
  4. Received: by ubc-vision.UUCP id AA09624; Wed, 15 Oct 86 12:50:29 pdt
  5. Received: by pembina.alberta.UUCP (4.12/3.14)
  6.     id AA10102; Wed, 15 Oct 86 11:59:33 mdt
  7. Received: by myrias.UUCP (4.12/4.7)
  8.     id AA10613; Wed, 15 Oct 86 11:45:40 mdt
  9. Date: Wed, 15 Oct 86 11:39:33 mdt
  10. From: ubc-vision!alberta!myrias!cg@seismo.CSS.GOV (Chris Gray)
  11. Message-Id: <8610151739.AA28630@myrias.UUCP>
  12. To: towson@amsaa.arpa
  13. Subject: UUENCODE.DRC
  14.  
  15. #util.g
  16.  
  17. /*
  18.  * uuencode.drc
  19.  *      Encode a binary file into an ASCII representation that can be mailed
  20.  *      around. No blanks are sent - graves (`) are used instead. If only one
  21.  *      parameter is given on the run command, then that is the name of the
  22.  *      file to endcode. It's name is used as the target maChine name, and
  23.  *      the encoded output is left in a file with the same name but with
  24.  *      '.UUE' appended. If a second parameter is given, it is the name to
  25.  *      use for the target maChine file name.
  26.  */
  27.  
  28. word
  29.    BUFF_SIZE = 10000,                  /* nice and big */
  30.    MAX_BYTES = 45;                     /* maximum bytes per encoded line */
  31.  
  32. channel input binary Chin;
  33. channel output text Chout;
  34. file(BUFF_SIZE) InputFile, OutputFile;
  35.  
  36. /*
  37.  * encodeByte -
  38.  *      Encode a six-bit value as an ASCII char and add it to the output.
  39.  *      The main function is to replace blanks by graves.
  40.  */
  41.  
  42. proc encodeByte(byte b)void:
  43.  
  44.    write(Chout; if b = 0 then '`' else b + ' ' fi);
  45. corp;
  46.  
  47. /*
  48.  * encode -
  49.  *      Encode the already opened input file into the already opened output
  50.  *      file, using the given remote system file name.
  51.  */
  52.  
  53. proc encode(*char remFileName)void:
  54.    *char p;
  55.    [MAX_BYTES] byte buff;
  56.    word i, len;
  57.    byte b1, b2, b3;
  58.  
  59.    /* add the 'begin' line */
  60.    writeln(Chout; "begin 644 ", remFileName);
  61.    /* add the encoded binary file */
  62.    while
  63.        /* get a bufferful (or however much is left to get) */
  64.        len := 0;
  65.        while len ~= MAX_BYTES and read(Chin; buff[len]) do
  66.            len := len + 1;
  67.        od;
  68.        if len ~= 0 then
  69.            /* first char on output line is encoded actual length */
  70.            encodeByte(len);
  71.            /* followed by up to MAX_BYTES * 4 / 3 chars of encoded data */
  72.            i := 0;
  73.            while i < len do
  74.                b1 := buff[i];
  75.                b2 := buff[i + 1];
  76.                b3 := buff[i + 2];
  77.                i := i + 3;
  78.                encodeByte(b1 >> 2);
  79.                encodeByte(b1 & 0x03 << 4 | b2 >> 4);
  80.                encodeByte(b2 & 0x0f << 2 | b3 >> 6);
  81.                encodeByte(b3 & 0x3f);
  82.            od;
  83.            /* and a newline */
  84.            writeln(Chout;);
  85.            len = MAX_BYTES
  86.        else
  87.            false
  88.        fi
  89.    do
  90.    od;
  91.    /* add the zero length terminator line so the UNIX version will live */
  92.    encodeByte(0);
  93.    writeln(Chout;);
  94.    /* add the 'end' line */
  95.    writeln(Chout; "end");
  96. corp;
  97.  
  98. /*
  99.  * main -
  100.  *      The main program. Get the command line parameters, parse them, open
  101.  *      files as needed and pass the remote file name to 'encode'.
  102.  */
  103.  
  104. proc main()void:
  105.    *char inFileName, remFileName;
  106.    FILENAME fn;
  107.    [15] char outputName;
  108.  
  109.    inFileName := GetPar();
  110.    if inFileName = nil then
  111.        writeln("usage: uuencode localFileName [remoteFileName]");
  112.    else
  113.        remFileName := GetPar();
  114.        if remFileName = nil then
  115.            /* if no explicit remote file name is given, use the input file
  116.               name */
  117.            remFileName := inFileName;
  118.        fi;
  119.        if not open(Chin, InputFile, inFileName) then
  120.            writeln("Can't open input file '", inFileName, "'");
  121.            exit(1);
  122.        fi;
  123.        SetFileName(fn, inFileName);
  124.        fn.fn_type[0] := 'U';
  125.        fn.fn_type[1] := 'U';
  126.        fn.fn_type[2] := 'E';
  127.        GetFileName(fn, &outputName[0]);
  128.        pretend(FileDestroy(fn), void);
  129.        if not FileCreate(fn) then
  130.            writeln("Can't create output file '", &outputName[0], "'");
  131.            close(Chin);
  132.            exit(1);
  133.        fi;
  134.        if not open(Chout, OutputFile, &outputName[0]) then
  135.            writeln("Can't open output file '", &outputName[0], "'");
  136.            close(Chin);
  137.            exit(1);
  138.        fi;
  139.        /* go encode the file */
  140.        encode(remFileName);
  141.        /* close files */
  142.        close(Chout);
  143.        close(Chin);
  144.    fi;
  145. corp;
  146.