home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ZOO21E.EXE / BILF.C < prev    next >
C/C++ Source or Header  |  1991-07-11  |  6KB  |  230 lines

  1. /* $Source: /usr/home/dhesi/zoo/RCS/bilf.c,v $ */
  2. /* $Id: bilf.c,v 1.2 91/07/07 14:57:06 dhesi Exp $ */
  3.  
  4. /*
  5. This program performs conversion of files between stream-LF format
  6. (as used by zoo) and fixed-length record binary format (used for Kermit
  7. transfers of zoo archives).
  8.  
  9. This program is:
  10.    (C) Copyright 1987 Rahul Dhesi.
  11.    All Rights Reserved.
  12.  
  13. Permission is hereby granted to copy and modify this for any purpose,
  14. whether commercial or noncommercial, provided only that the above
  15. copyright notice and this paragraph be preserved and included
  16. in all copies.
  17.  
  18.                                  -- Rahul Dhesi 1987/07/25
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <ssdef.h>
  23. #define STAT_NORM SS$_NORMAL
  24. #define STAT_ABORT SS$_ABORT
  25.  
  26. char *strrchr();
  27. char *strdup ();
  28.  
  29. main (argc, argv)
  30. int argc;
  31. char *argv[];
  32. {
  33.    char *inname;
  34.    char *outname;
  35.    char *option;
  36.    int status;
  37.  
  38.    if (argc < 3 || argc > 4) {
  39.       printf ("BILF version 1.00 for VAX/VMS by Rahul Dhesi (1987/07/25)\n\n");
  40.       printf ("(C) Copyright 1987 Rahul Dhesi,  All Rights Reserved\n");
  41.       printf ("Permission to use and distribute is granted provided this copyright\n");
  42.       printf ("notice is preserved and included in all copies.\n\n");
  43.       printf ("Usage:  BILF {lb} infile [ outfile ]\n\n");
  44.       printf ("Choose one character from within braces.  If outfile is not supplied\n");
  45.       printf ("it has the same name as infile but a higher version number.\n");
  46.       printf ("Options are:\n\n");
  47.       printf ("l:  Write output file in stream-LF format.  This is the format that\n");
  48.       printf ("    zoo expects all zoo archives to be in.  If a zoo archive was\n");
  49.       printf ("    uploaded to a VAX/VMS system, it will need to be converted to\n");
  50.       printf ("    stream-LF format before manipulating with zoo.\n\n");
  51.       printf ("b:  Write output file in fixed-length 512-byte binary record format.  Before\n");
  52.       printf ("    a zoo archive can be downloaded from a VAX/VMS system to a\n");
  53.       printf ("    microcomputer using VAX/VMS Kermit, it must be converted to\n");
  54.       printf ("    this binary format.  Failure to do so will result in a corrupted\n");
  55.       printf ("    download.\n");
  56.       exit (STAT_NORM);
  57.    }
  58.  
  59.    inname = argv[2];
  60.    option = argv[1];
  61.  
  62.    if (argc == 3) {                    /* use same filename for output */
  63.       char *p;
  64.       outname = strdup (inname);
  65.       p = strrchr (outname, ';');      /* strip trailing version field */
  66.       if (p != NULL)
  67.          *p = '\0';
  68.    } else
  69.       outname = argv[3];
  70.  
  71.    if (*option == 'l')
  72.       status = cvtstream (outname, inname);
  73.    else if (*option == 'b')
  74.       status = cvtbin (outname, inname);
  75.    else
  76.       prterror ('f', "Option %s is invalid\n", option);
  77.    if (status == -1)
  78.       prterror ('w', "An error occurred -- output file may be corrupted\n");
  79.    exit (STAT_NORM);
  80. }
  81.  
  82. #define  MYBUFSIZ    8192
  83.  
  84. /* writes input file to output file in stream format */
  85. int cvtstream (outname, inname)
  86. char *outname, *inname;
  87. {
  88.    FILE *infile, *outfile;
  89.    char buffer[MYBUFSIZ];
  90.    int count;
  91.  
  92.    infile = fopen (inname, "r");
  93.    if (infile == NULL)
  94.       prterror ('f', "Could not open input file %s\n", inname);
  95.    outfile = fopen (outname, "w");
  96.    if (outfile == NULL)
  97.       prterror ('f', "Could not open output file %s\n", outname);
  98.  
  99.    while ((count = fread (buffer, 1, sizeof (buffer), infile)) > 0)
  100.       count = fwrite (buffer, 1, count, outfile);
  101.  
  102.    close (infile); close (outfile);
  103.    if (count == -1)
  104.       return (-1);
  105.    else
  106.       return (0);
  107. }
  108.  
  109. /*
  110. VMS C doesn't have strdup().
  111. */
  112. char *strdup (str)
  113. char *str;
  114. {
  115.    char *malloc();
  116.    char *newstr = malloc (strlen (str) + 1);
  117.    if (newstr != NULL) {
  118.       strcpy (newstr, str);
  119.       return (newstr);
  120.    } else
  121.       return ((char *) NULL);
  122. }
  123.  
  124. /* BLKSIZ must correspond to block size specified below in creat() */
  125. #define BLKSIZ 512
  126.  
  127. /*
  128. Writes input file to output in fixed-length BLKSIZ-byte record format.
  129. */
  130.  
  131. #if 1
  132. #include <file.h>
  133. #else
  134. #include <fcntl.h>
  135. #endif
  136.  
  137. int convert ();
  138.  
  139. int cvtbin (outname, inname)
  140. char *outname, *inname;
  141. {
  142.    int status, inhan, outhan;
  143.    inhan = open (inname, O_RDONLY);
  144.    if (inhan == -1)
  145.       prterror ('f', "Could not open input file %s\n", inname);
  146.    outhan = creat (outname, 0, "rfm=fix", "mrs=512");
  147.    if (outhan == -1)
  148.       prterror ('f', "Could not open output file %s\n", outname);
  149.    status = convert (outhan, inhan);
  150.    close (inhan);
  151.    close (outhan);
  152.    return (status);
  153. }
  154.  
  155. /*
  156. Function convert() reads from inhan and writes to outhan, always
  157. writing in BLKSIZ-byte blocks, padding with nulls if necessary
  158. */
  159.  
  160. int convert (outhan, inhan)
  161. int inhan, outhan;
  162. {
  163.    char junk[BLKSIZ];
  164.    int count;
  165.    int done = 0;
  166.    do {
  167.       count = vmsread (inhan, junk, BLKSIZ);
  168.       if (count <= 0)
  169.          break;
  170.       if (count < BLKSIZ) {
  171.          int i;
  172.          for (i = count; i < BLKSIZ; i++)
  173.             junk[i] = 0;
  174.          done++;
  175.       }
  176.       count = write (outhan, junk, BLKSIZ);
  177.       if (count == -1)
  178.          break;
  179.    } while (!done);
  180.    if (count == -1)
  181.       return (-1);
  182.    else
  183.       return (0);
  184. }
  185.  
  186. /**** Function vmsread() does a standard read() but gets around bugs
  187. in the read() function of VAX/VMS C which make it unable to always
  188. read the entire amount requested in a single read() call.
  189. */
  190. int vmsread (han, buf, amount)
  191. int han;
  192. char *buf;
  193. int amount;
  194. {
  195.    int count;
  196.    int thiscount;
  197.    count = 0;
  198.    while (count != -1 && count < amount) {
  199.       thiscount = read (han, &buf[count], amount - count);
  200.       if (thiscount == 0)
  201.          thiscount = read (han, &buf[count], amount - count);
  202.       if (thiscount == 0)
  203.           break;
  204.       if (thiscount == -1)
  205.          count = -1;
  206.       else
  207.          count += thiscount;
  208.    }
  209.    return (count);
  210. }
  211.  
  212. prterror (level, msg1, msg2)
  213. char level;
  214. char *msg1, *msg2;
  215. {
  216.    if (level == 'e' || level == 'w' || level == 'f')
  217.       printf ("BILF: ");
  218.  
  219.    switch (level) {
  220.       case 'e': printf ("ERROR: ");    break;
  221.       case 'w': printf ("WARNING: ");  break;
  222.       case 'f': printf ("FATAL: ");    break;
  223.       default:  prterror ('f', "Internal error in prterror()\n");
  224.    }
  225.  
  226.    printf (msg1, msg2);
  227.    if (level == 'f')
  228.       exit (STAT_ABORT);
  229. }
  230.