home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / unix_c / utils / bntnxvms.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  10.2 KB  |  391 lines

  1. /***********************************************************************
  2. Convert a TOPS-20 file transferred in FTP "binary" mode to "tenex" mode.
  3. In "binary" mode, we have 2 36-bit words in 9 8-bit bytes.  In "tenex"
  4. mode, we want the top 32 bits of each 36-bit group, giving 8 8-bit
  5. bytes.
  6.  
  7. Who knows what FTP did if the file had an odd number of 36-bit words.
  8.  
  9. [08-Oct-87]
  10.  
  11. This version is intended for VAX VMS only.  Modified by Jerry Leichter
  12. (LEICHTER@VENUS.YCC.YALE.ARPA).
  13.  
  14. Usage:
  15.     bintnxvms [-f[xx]] [filespec]
  16.  
  17. Filespec may be wild-carded; if it is missing, *.* is assumed.  Each
  18. matching file is read, and a new version the same specification is
  19. created.  (Note that you can only specify one filespec, however.)  If
  20. -fxx is specified, the last block of the file is filled with bytes of
  21. the given value (xx), which must be a hex number.  If -f is given with
  22. no value, 0 is assumed.  Appropriate values seem to be:
  23.  
  24.     * DVI    DF
  25.     * GF    DF
  26.       PK    F6 (File spec defines no filler, F6 may be an artifact)
  27.       PXL    ?? (Unknown, probably not significant)
  28.       TFM    00 (File spec defines no filler)
  29.  
  30. "Fill" is definitely significant, at least in some applications, for the
  31. file types marked, with a "*"; it probably doesn't matter for others.
  32. Note, how- ever, that there is no certainty that files filled this way
  33. will actually be readable to all programs, since the transfer process
  34. may insert other random junk at the end of the file.
  35.  
  36. The filler is used by programs that wish to read the files from end to
  37. beginning - for example, by DVI file translators that need to reverse
  38. page order.  The author has observed that "fixed up" files often follow
  39. the pattern:
  40.  
  41.     <data> <fill-bytes> <FFFFFFFF> <fill-bytes> end-of-block
  42.  
  43. The first set of fill bytes were probably inserted by the program that
  44. wrote the file to being with.  (There should be from 1 to 4 of them,
  45. bringing the file to an even number of 4-byte longwords.) The
  46. <FFFFFFFF> filler is four bytes of hex FF characters, and probably
  47. were inserted by VMS FTP to mark the end of the file on the TOPS-20
  48. system.  The rest of the <fill-bytes> were inserted by fixfile.  If
  49. you run into a program that is unable to read a file produced by
  50. fixfile, DUMP the last block of the file and see if this pattern
  51. occurs.  If so, it should be straightforward to over-write the FF's
  52. with more <fill-byte>'s.
  53. ***********************************************************************/
  54.  
  55. #include    <ctype.h>
  56. #include    <stdio.h>
  57. #include    <rms.h>
  58.  
  59. FILE *fwild();
  60. FILE *fnext();
  61.  
  62. main(argc, argv)
  63. int argc;
  64. char *argv[];
  65. {
  66.     FILE *infile;
  67.     FILE *outfile;
  68.     int fill = FALSE;
  69.     int fillchar = 0;
  70.     char *fspec;
  71.     char thefile[NAM$C_MAXRSS + 1];
  72.     char *c;
  73.     int d;
  74.  
  75.     if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'f')
  76.     {
  77.     fill = TRUE;
  78.     c = &argv[1][2];
  79.     while (isxdigit(*c))
  80.     {
  81.         if (isdigit(*c))
  82.         d = *c - '0';
  83.         else
  84.         d = 10 + toupper(*c) - 'A';
  85.         fillchar = (fillchar << 4) | d;
  86.         c++;
  87.     }
  88.     argc--;
  89.     argv++;
  90.     }
  91.     if (argc > 1)
  92.     fspec = argv[1];
  93.     else
  94.     fspec = "*.*";
  95.  
  96.     if ((infile = fwild(fspec, "r")) == NULL)
  97.     {
  98.     perror("Bad file spec");
  99.     exit(0);
  100.     }
  101.  
  102.     while (fnext(infile) != NULL)
  103.     {
  104.     fgetname(infile, thefile);
  105.     /* Remove version number */
  106.     d = strlen(thefile) - 1;
  107.     while (thefile[d] != ';')
  108.         d--;
  109.     thefile[d] = '\0';
  110.     (void)printf("[%s", thefile);
  111.     if ((outfile = fopen(thefile, "wb", "rfm=fix", "mrs=512"))
  112.         == NULL)
  113.     {
  114.         perror("] Can't open output file");
  115.         continue;
  116.     }
  117.     dofile(infile, outfile, fill, fillchar);
  118.     fclose(outfile);
  119.     (void)printf("]\n");
  120.     }
  121.  
  122. }
  123.  
  124. dofile(infile, outfile, fill, fillchar)
  125. FILE *infile;
  126. FILE *outfile;
  127. int fill;
  128. int fillchar;
  129. {
  130.     int c;
  131.     int d;
  132.     unsigned int bytecount = 0;
  133.  
  134. #define GET()    fgetc(infile)
  135. #define PUT(c)    (fputc((c),outfile),bytecount++)
  136. #define ECHO()    PUT(GET())
  137.  
  138.     for (;;)
  139.     {
  140.     c = GET();
  141.     if (c == EOF)
  142.         break;
  143.  
  144.     PUT(c);            /* 0..7 */
  145.     ECHO();            /* 8..15 */
  146.     ECHO();            /* 16..23 */
  147.     ECHO();            /* 24..31 */
  148.  
  149.     d = GET();
  150.     c = (d << 4);
  151.     d = GET();
  152.     c |= 0xFF & (d >> 4);
  153.     PUT(c);            /* 4..11 */
  154.  
  155.     c = (d << 4);
  156.     d = GET();
  157.     c |= 0xFF & (d >> 4);
  158.     PUT(c);            /* 12..19 */
  159.  
  160.     c = (d << 4);
  161.     d = GET();
  162.     c |= 0xFF & (d >> 4);
  163.     PUT(c);            /* 20..27 */
  164.  
  165.     c = (d << 4);
  166.     d = GET();
  167.     c |= 0xFF & (d >> 4);
  168.     PUT(c);            /* 28..36 */
  169.     }
  170.  
  171.     if (fill)
  172.     {
  173.     while (bytecount & 511)
  174.         PUT(fillchar);
  175.     }
  176. }
  177.  
  178. /*
  179.  * Fwild and fnext for vms.  Written by Martin Minow; taken from the
  180.  * DECUS C VMS compatibility collection.
  181.  */
  182.  
  183. /* #include    <stdio.h> */
  184. /* #include    <rms.h> */
  185. #include    <ssdef.h>
  186. #include    <descrip.h>
  187. #include    <ctype.h>
  188.  
  189. #define    TRUE        1
  190. #define    FALSE        0
  191. #define    EOS        0
  192.  
  193. typedef struct rmsstuff
  194. {
  195.     int flag;            /* Flag for nonwildcard calls     */
  196.     char *wildmode;
  197.     struct FAB fab;
  198.     struct NAM nam;
  199.     char starname[NAM$C_MAXRSS + 1];
  200.     char filename[NAM$C_MAXRSS + 1];
  201. }   RMSSTUFF;
  202.  
  203. static RMSSTUFF *wilddata[_NFILE];    /* RMS struct for each file */
  204.  
  205. /*
  206.  * rms->flag can take on the following values:
  207.  *    ISWILD        A file with wild-card bytes.
  208.  *    UNWILD        A file without wild-cards, unopened.
  209.  *    UNWILD_OPENED    A file without wild-cards, opened.
  210.  */
  211. #define    ISWILD        0
  212. #define    UNWILD        1
  213. #define    UNWILD_OPENED    2
  214.  
  215. extern FILE *cleanup();
  216. FILE *
  217. fwild(filename, mode)
  218. char *filename;
  219. char *mode;
  220. /*
  221.  * Do wildcard setup
  222.  */
  223. {
  224.     register FILE *fd;
  225.     register RMSSTUFF *r;
  226.     register int index;
  227.  
  228.     /* First get a file to work with.  In the process, make sure the
  229.        mode we got passed is valid.  Note that every file we will open
  230.        from here on already exists, so an open for write will fail
  231.        (since the open will include the fully-expanded file name,
  232.        including the version number).  Unfortunately, opening a file on
  233.        the null device always succeeds.  So, to simplify matters, we
  234.        just reject a "w" mode request right here.  (We could as well let
  235.        it go and then find "no matching files" when all later open
  236.        requests fail.  But this makes more sense.) */
  237.     if (*mode == 'w' || (fd = fopen("_nl:", mode)) == NULL)
  238.     {
  239.     return (NULL);
  240.     }
  241.     /* Warning: the package depends on fileno(fd) remaining
  242.        unchanged after calls to freopen(). */
  243.     index = fileno(fd);
  244.     /* If we've been here before, make sure buffers are released. */
  245.     cleanup(index);
  246.     if ((r = malloc(sizeof(RMSSTUFF))) == NULL
  247.     || (r->wildmode = malloc(strlen(mode) + 1)) == NULL)
  248.     return (cleanup(index));
  249.     strcpy(r->wildmode, mode);
  250.     wilddata[index] = r;
  251.     /* Setup the fab and nam blocks. */
  252.     r->fab = cc$rms_fab;    /* Initialize fab    */
  253.     r->nam = cc$rms_nam;    /* and nam blocks  */
  254.     r->fab.fab$l_nam = &r->nam;    /* fab -> nam         */
  255.     r->fab.fab$l_fna = filename;    /* Argument filename */
  256.     r->fab.fab$b_fns = strlen(filename);    /* filename size     */
  257.     r->nam.nam$l_esa = r->starname;    /* Expanded file name */
  258.     r->nam.nam$b_ess = NAM$C_MAXRSS;    /* ... size         */
  259.     r->nam.nam$l_rsa = r->filename;    /* Result filename   */
  260.     r->nam.nam$b_rss = NAM$C_MAXRSS;    /* ... size         */
  261.     /* Parse the file name */
  262.     if (sys$parse(&r->fab) != RMS$_NORMAL)
  263.     return (cleanup(index));
  264.     /* Success.  Null-terminate expanded file name and set flag to
  265.        distinguish between "wild" and "non-wild" filenames. */
  266.     ((char *) r->nam.nam$l_esa)[r->nam.nam$b_esl] = EOS;
  267.     r->flag = ((r->nam.nam$l_fnb & NAM$M_WILDCARD) == 0) ?
  268.     UNWILD : ISWILD;
  269.     return (fd);
  270. }
  271.  
  272.  
  273. FILE *
  274. fnext(fd)
  275. FILE *fd;
  276. /*
  277.  * Open the next valid file. return fd if successful, NULL if finished.
  278.  */
  279. {
  280.     register int index;
  281.     register RMSSTUFF *r;
  282.     register int errorcode;
  283.     FILE *test;
  284.  
  285.     index = fileno(fd);
  286.     if ((r = wilddata[index]) == NULL || r->flag == UNWILD_OPENED)
  287.     {
  288.     /* It wasn't ours, or wasn't a wildcard and has already been
  289.        processed. */
  290.     fclose(fd);
  291.     fd = NULL;
  292.     }
  293.     else
  294.     if (r->flag == UNWILD)
  295.     {
  296.     /* Not a wildcard file, first time through */
  297.     fd = freopen(r->starname, r->wildmode, fd);
  298.     r->flag = UNWILD_OPENED;
  299.     }
  300.     else
  301.     {
  302.     /* Look for the next match -- who says you can't write obscure
  303.        structured code? */
  304.     for (;;)
  305.     {
  306.         /* Look 'em up but skip any with protection violation
  307.         errors. */
  308.         r->fab.fab$w_ifi = 0;    /* Internal file index */
  309.         if ((errorcode = sys$search(&r->fab)) == RMS$_NORMAL)
  310.         {
  311.         /* We have a file.  Open it if we have access rights.
  312.            Determining this isn't as simple as it might be
  313.            because the access() function checks the wrong thing:
  314.            It ignores ACL's and any special privileges.  So we
  315.            just open the file twice.  (We have to check first
  316.            since if we fail, freopen will free up our file
  317.            pointer and we can't be sure of getting it back.)
  318.            Opening the file twice has one unfortunate side
  319.            effect - it bumps the file revision by 2.  That's
  320.            life. */
  321.         ((char *) r->nam.nam$l_rsa)[r->nam.nam$b_rsl] = EOS;
  322.         if ((test = fopen(r->filename, r->wildmode)) == NULL)
  323.         {
  324.             /* Couldn't open it; try for another. */
  325.             continue;
  326.         }
  327.         else
  328.         {
  329.             /* Close up the test channel first (to avoid
  330.                problems with sharing; then open the file.  We
  331.                should normally be able to open the file.  It
  332.                this fails, something is wrong inside the C
  333.                library.  (More likely, a timing problem occured
  334.                - between our test and our actual open, someone
  335.                deleted the file, or between our close and
  336.                re-open, someone else opened it in an
  337.                incompatible mode.)  In any case, we've lost our
  338.                file descriptor, and there is no good way to
  339.                recover; so we just return NULL and let the
  340.                caller think that's all there is. */
  341.             fclose(test);
  342.             fd = freopen(r->filename, r->wildmode, fd);
  343.             break;
  344.         }
  345.         }
  346.         else
  347.         if (errorcode == RMS$_PRV)
  348.         {
  349.         /* sys$search() found something, but we don't have
  350.            privileges to open it.  Look for another. */
  351.         continue;
  352.         }
  353.         else
  354.         {
  355.         /* Can't find a file.  This should be RMS$_NMF. */
  356.         fclose(fd);
  357.         fd = NULL;
  358.         break;
  359.         }
  360.     }
  361.     }
  362.     /* Cleanup if any errors */
  363.     if (fd == NULL)
  364.     {
  365.     cleanup(index);
  366.     }
  367.     return (fd);
  368. }
  369.  
  370. static FILE *
  371. cleanup(index)
  372. register int index;
  373. /*
  374.  * Empty out any stored information
  375.  */
  376. {
  377.     register RMSSTUFF *r;
  378.  
  379.     r = wilddata[index];
  380.     if (r != NULL)
  381.     {
  382.     if (r->wildmode != NULL)
  383.     {
  384.         free(r->wildmode);
  385.     }
  386.     free(r);
  387.     wilddata[index] = NULL;
  388.     }
  389.     return (NULL);
  390. }
  391.