home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume04 / tenex < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  6.4 KB

  1. From mipos3!intelca!oliveb!ames!necntc!ncoast!allbery Tue Sep 20 18:26:49 PDT 1988
  2. Article 538 of comp.sources.misc:
  3. Path: td2cad!mipos3!intelca!oliveb!ames!necntc!ncoast!allbery
  4. From: dan@rna.UUCP (Dan Ts'o)
  5. Newsgroups: comp.sources.misc
  6. Subject: v04i082: TENEX - pgm to convert DEC10 data to 8bit bytes
  7. Message-ID: <8809071745.AA08524@rna>
  8. Date: 20 Sep 88 01:11:18 GMT
  9. Sender: allbery@ncoast.UUCP
  10. Reply-To: dan@rna.UUCP (Dan Ts'o)
  11. Lines: 298
  12. Approved: allbery@ncoast.UUCP
  13.  
  14. Posting-number: Volume 4, Issue 82
  15. Submitted-by: "Dan Ts'o" <dan@rna.UUCP>
  16. Archive-name: tenex
  17.  
  18.     I had just spent a few hours uploading MSDOS stuff from SIMTEL20.
  19. Back on my PC, I realized that while I had remembered to transfer everything
  20. in binary mode (in FTP), that I had neglected to account for (is it called ?)
  21. TENEX mode (since SIMTEL20 is a 36-bit machine). I decided it would be faster
  22. to write a C program to convert the 36-bit data into good ol' 8-bit bytes,
  23. than re-upload every. So here is the program. I used MSC 5.0. The program will
  24. convert both binary (-b, default, from each 36-bit word, generate 4x8-bit bytes
  25. and discard the remaining 4bits) and text (-t, from each 36-bit word, generate
  26. 5x7bit (with zero parity bit) ASCII bytes and discard the last bit).
  27. The program has been tested on both MSDOS 3.1 and 4.3BSD UNIX. If anyone needs
  28. a binary copy for MSDOS, I'll mail them one. I hope this wheel hasn't been
  29. reinvented too many times...
  30.  
  31.                     Cheers,
  32.                     Dan Ts'o
  33.                     tso@rockefeller.edu
  34.                     dan@rna.rockefeller.edu
  35.                     ...cmcl2!rna!dan
  36.                     212-570-7671
  37.                     Dept Neurbiology
  38.                     The Rockefeller University
  39.                     1230 York Ave.
  40.                     NY, NY  10021
  41. wc tenex.c:
  42. 266     752    4787
  43. sum tenex.c: (VAX)
  44. 32982     5
  45. tenex.c:
  46. /*
  47.  * TENEX convert - convert tenex 36-bit files into standard 8-bit
  48.  *    byte stream by discarding every 9th nibble (hopefully its zero).
  49.  *    Also convert TENEX 7-bit ASCII to standard 8-bit byte ASCII by
  50.  *    discarding every 36th bit.
  51.  *
  52.  *    Written by Daniel Ts'o, dan@rna.rockefeller.edu, 9/7/88
  53.  *
  54.  *    Usage: tenex [-[t|b]] [-[v|q]] [files...]
  55.  *
  56.  *    -t    Convert 5x7bit ASCII (35+1bit packing) into 8-bit byte stream
  57.  *    -b    Convert 4*8bit binary (32+4bit packing) into 8-bit byte stream
  58.             (Default is -b)
  59.  *    -v    Print progress on stderr
  60.  *    -q    Quietly delete nonzero bits
  61.  *
  62.  *    If file arguments are given, they are converted "in place", using
  63.  *        temporary file "tenex.tmp".
  64.  *    If file arguments are missing, convert stdin to stdout (filter mode).
  65.  *
  66.  *    Compile with MSC 5.0 on MSDOS:
  67.  *
  68.  *        cl tenex.c \lib\setargv.obj /link /NOE
  69.  *
  70.  *    to permit wildcard expansion.
  71.  */
  72.  
  73. #include <stdio.h>
  74.  
  75. #define    TEMPFILE    "tenex.tmp"
  76.  
  77. /*#define    RENAME            /* Some UNIX's don't have rename() */
  78.  
  79. #ifdef    MSDOS
  80. #define    READMODE    "rb"
  81. #define    WRITEMODE    "wb"
  82. #include <fcntl.h>
  83. #include <io.h>
  84. #else
  85. #define    READMODE    "r"
  86. #define    WRITEMODE    "w"
  87. #endif
  88.  
  89. char *nodename;
  90. int vflag = 0;
  91. int qflag = 0;
  92. int tflag = 0;
  93.  
  94. main(c,v)
  95. char **v;
  96. {
  97.     register int i,f;
  98.     FILE *ifd, *ofd;
  99.     char ibuf[BUFSIZ];
  100.     char obuf[BUFSIZ];
  101.  
  102.     f = 0;
  103.     nodename = *v;
  104.     setbuf(stdin, ibuf);
  105.     setbuf(stdout, obuf);
  106.     while (c > 1 && v[1][0] == '-') {
  107.         c--;
  108.         v++;
  109.         switch (v[0][1]) {
  110.         case 'v':
  111.             vflag++;
  112.             break;
  113.         case 'q':
  114.             qflag++;
  115.             break;
  116.         case 't':
  117.             tflag++;
  118.             break;
  119.         case 'b':
  120.             tflag = 0;
  121.             break;
  122.         default:
  123.             fprintf(stderr, "%s:%s: Bad option\n", nodename, *v);
  124.             exit(-1);
  125.         }
  126.     }
  127.  
  128. #ifdef    MSDOS
  129.     if (setmode(fileno(stdin), O_BINARY) == -1
  130.         || setmode(fileno(stdout), O_BINARY) == -1) {
  131.         fprintf(stderr, "%s: Cannot setmode on stdio\n", nodename);
  132.         exit(1);
  133.     }
  134. #endif
  135.  
  136.     if (--c <= 0)
  137.         exit(xfer(stdin, stdout, "(stdin)"));
  138.     else {
  139.         while (c--) {
  140.             v++;
  141.             if (**v == '-' && v[0][1] == 0) {
  142.                 xfer(stdin, stdout, "(stdin)");
  143.                 clearerr(stdin);
  144.             }
  145.             else {
  146.                 unlink(TEMPFILE);
  147.                 if ((ifd = fopen(*v, READMODE)) == NULL) {
  148.                     fprintf(stderr, "%s: %s: Can't open\n",
  149.                         nodename, *v);
  150.                     f++;
  151.                     break;
  152.                 }
  153.                 else if ((ofd = fopen(TEMPFILE, WRITEMODE))
  154.                     == NULL) {
  155.                     fprintf(stderr, "%s: %s: Can't open\n",
  156.                         nodename, TEMPFILE);
  157.                     f++;
  158.                     break;
  159.                 }
  160.                 else if (xfer(ifd, ofd, *v) == 0) {
  161.                     fclose(ifd);
  162.                     fclose(ofd);
  163.                     if (unlink(*v)) {
  164.                         fprintf(stderr,
  165.                         "%s: %s: Can't unlink\n",
  166.                         nodename, *v);
  167.                         f++;
  168.                         break;
  169.                     }
  170.                     if (rename(TEMPFILE, *v)) {
  171.                         fprintf(stderr,
  172.                         "%s: %s: Can't rename\n",
  173.                         nodename, *v);
  174.                         f++;
  175.                         break;
  176.                     }
  177.                 }
  178.             }
  179.         }
  180.     }
  181.     exit(f ? -1 : 0);
  182. }
  183.  
  184. xfer(ifd, ofd, name)
  185. FILE *ifd;
  186. FILE *ofd;
  187. char *name;
  188. {
  189.     if (vflag) {
  190.         fprintf(stderr, "%s: %s", nodename, name);
  191.         if (tflag)
  192.             fprintf(stderr, " (text mode)\n");
  193.         else
  194.             fprintf(stderr, " (binary mode)\n");
  195.     }
  196.     if (tflag)
  197.         return txfer(ifd, ofd, name);
  198.     else
  199.         return bxfer(ifd, ofd, name);
  200. }
  201.  
  202. bxfer(ifd, ofd, name)
  203. FILE *ifd;
  204. FILE *ofd;
  205. char *name;
  206. {
  207.     register int icnt, ocnt, ncnt, ci, co, c;
  208.  
  209.     icnt = 0;
  210.     ocnt = 0;
  211.     ncnt = 0;
  212.     co = ci = c = 0;
  213.     for (;;) {
  214.         if (icnt <= 0) {
  215.             if ((ci = getc(ifd)) == EOF) {
  216.                 if (ocnt > 0) {
  217.                     fprintf(stderr, "%s: 0x%x: Warning, unpaired ending nibble in file %s\n", nodename, co, name);
  218.                     putc(co, ofd);
  219.                 }
  220.                 break;
  221.             }
  222.             else {
  223.                 icnt = 1;
  224.                 c = ci>>4;
  225.             }
  226.         }
  227.         else {
  228.             icnt = 0;
  229.             c = ci&017;
  230.         }
  231.         if (++ncnt >= 9) {
  232.             ncnt = 0;
  233.             if (c != 0 && !qflag)
  234.                 fprintf(stderr, "%s: 0x%x: Warning, nonzero nibble discarded from file %s\n", nodename, c, name);
  235.             continue;
  236.         }
  237.         if (++ocnt >= 2) {
  238.             co |= c;
  239.             putc(co, ofd);
  240.             if (ferror(ofd))
  241.                 break;
  242.             co = 0;
  243.             ocnt = 0;
  244.         }
  245.         else
  246.             co |= c<<4;
  247.     }
  248.     fflush(ofd);
  249. }
  250.  
  251. txfer(ifd, ofd, name)
  252. FILE *ifd;
  253. FILE *ofd;
  254. char *name;
  255. {
  256.     register int icnt, ocnt, ncnt, ci, co, c;
  257.  
  258.     icnt = 0;
  259.     ocnt = 0;
  260.     ncnt = 0;
  261.     co = ci = c = 0;
  262.     for (;;) {
  263.         if (icnt <= 0) {
  264.             if ((ci = getc(ifd)) == EOF) {
  265.                 if (ocnt > 0) {
  266.                     fprintf(stderr, "%s: 0x%x: Warning, incomplete ending byte in file %s\n", nodename, co, name);
  267.                     putc(co, ofd);
  268.                 }
  269.                 break;
  270.             }
  271.             else {
  272.                 ci &= 0377;
  273.                 icnt = 7;
  274.                 c = ci>>7;
  275.             }
  276.         }
  277.         else {
  278.             icnt--;
  279.             c = (ci>>icnt)&1;
  280.         }
  281.         if (++ncnt >= 36) {
  282.             ncnt = 0;
  283.             if (c != 0 && !qflag)
  284.                 fprintf(stderr, "%s: Warning, nonzero bit discarded from file %s\n", nodename, name);
  285.             continue;
  286.         }
  287.         if (++ocnt >= 7) {
  288.             co |= c;
  289.             putc(co, ofd);
  290.             if (ferror(ofd))
  291.                 break;
  292.             co = 0;
  293.             ocnt = 0;
  294.         }
  295.         else
  296.             co |= c<<(7-ocnt);
  297.     }
  298.     fflush(ofd);
  299. }
  300.  
  301. #ifdef    RENAME
  302. rename(a, b)
  303. char *a, *b;
  304. {
  305.     register int f;
  306.  
  307.     if (f = link(a, b))
  308.         return f;
  309.     return unlink(a);
  310. }
  311. #endif
  312.  
  313.  
  314.