home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / macbin.c < prev    next >
C/C++ Source or Header  |  1991-01-29  |  6KB  |  217 lines

  1. /* macbin.c
  2.  
  3.    Purpose:  Convert .data, .info, and .rsrc files into one .bin
  4.       file for easy downloading (etc).
  5.       
  6.             MyMacFile.data  \
  7.             MyMacFile.info   >  MyMacFile.bin
  8.             MyMacFile.rsrc  /
  9.  
  10.    [this replaces the file macbinary.shar on sumex-aim.stanford.edu]
  11.  
  12.    Version History:
  13.       1.0      Jim Budler        First release.  Handled one file
  14.                jimb@amdcad.UUCP  only, and didn't have the ability
  15.                                  to delete the .d, .i, .r files.
  16.       
  17.       2.0      Mike Gleason      Added batch files, option to delete
  18.                                  the .d, .i, .r, files after
  19.                                  processing.
  20.                            
  21.       3.0      Mike Gleason      Improved batch file handling, so
  22.                                  the user wouldn't need to type
  23.                                  "macbin file1 file2 file3 .. filen"
  24.                                  but instead "macbin *". Obviously,
  25.                                  in previous versions using "macbin *"
  26.                                  would assume we had files like
  27.                                  file1.data.data, file1.data.info,
  28.                                  file1.data.rsrc, file1.info.data...
  29.                                  One would realize the great
  30.                                  convenience of this feature if you
  31.                                  have had to unsit a whole buttload
  32.                                  of files, and then type in mile-long
  33.                                  command lines.
  34.                         
  35. */
  36.  
  37.                         
  38. /* structure of the info file - from MacBin.C
  39.    char zero1;
  40.    char nlen;
  41.    char name[63];
  42.    char type[4];     65 0101
  43.    char creator[4];  69
  44.    char flags;    73
  45.    char zero2;    74 0112
  46.    char location[6]; 80
  47.    char protected;      81 0121
  48.    char zero3;    82 0122
  49.    char dflen[4];
  50.    char rflen[4];
  51.    char cdate[4];
  52.    char mdate[4];
  53. */
  54.  
  55. /* #define SYSV */
  56. /* 
  57.    Note:  to run this program on a System V Unix system, remove all calls to
  58.    the bzero() procedure.  It doesn't exist on System V, and isn't needed.
  59.    [To do this easily, just uncomment the above line, and the lines
  60.     containing bzero() will not be compiled. --MG] */
  61.  
  62. #include <stdio.h>
  63. #include <string.h>
  64.  
  65. void     main (int argc, char **argv);
  66. int      FilterArgumentList(int argc, char **argv);
  67. int      macbin (char *prefix, int delete);
  68.  
  69. void     main (int argc, char **argv)
  70. {
  71.    int i, deleteOn = 0;
  72.    
  73.    if (argc < 2) 
  74.    {
  75.       fprintf(stderr,
  76.          "\nUsage: %s [-d] <list of files>\n"
  77.          "\nPurpose: merges <prefix>.info, <prefix>.data, <prefix>.rsrc"
  78.          "\n  into a valid macbinary file, <prefix>.bin, and optionally"
  79.          "\n  deletes the .info, .data, .rsrc files (the -d).\n",argv[0]);
  80.       exit(1);
  81.    }
  82.    
  83.    if (argc > 3)
  84.       FilterArgumentList(argc, argv);
  85.    
  86.    for (i=1; i<argc; i++)
  87.       if (argv[i][0] == '-' && argv[i][1] == 'd')
  88.          deleteOn = !deleteOn;   /* you can toggle delete mode on/off */
  89.       else
  90.          if (*argv[i])  /* if the first character is not 0 */
  91.             macbin(argv[i], deleteOn);
  92. }  /* main */
  93.  
  94.  
  95.  
  96.  
  97.  
  98. int      FilterArgumentList(int argc, char **argv)
  99. {
  100.    register int i;
  101.    
  102.    for (i=1; i<argc-2; i++)
  103.    {
  104.       if    (
  105.          strcmp(argv[i], ".data") > 0 &&  
  106.          strcmp(argv[i+1], ".info") > 0 &&
  107.          strcmp(argv[i+2], ".rsrc")
  108.          )
  109.       {
  110.          /* if 3 successive arguments contain .data,
  111.             .info, and .rsrc, (i.e. we have MyFkey.data,
  112.             MyFkey.info, and MyFkey.rsrc) then let's really
  113.             only pass the prefix (i.e. "MyFkey") */
  114.             
  115.          argv[i][ strlen(argv[i]) - 5 ] = 
  116.             argv[i+1][0] = argv[i+2][0] = '\0';
  117.             
  118.          /* we'll use the first character of an argument as a
  119.             signal to ignore it. */
  120.       }
  121.    }
  122. }  /* FilterArgumentList */
  123.  
  124.  
  125.  
  126.  
  127. int      macbin (char *prefix, int delete)
  128. {
  129.    FILE *fd, *ofd;
  130.    char oname[128];
  131.    char iname[128];
  132.    char dname[128];
  133.    char rname[128];
  134.    char buf[128];
  135.    
  136. #ifndef SYSV
  137.    bzero(buf, 128);
  138. #endif
  139.  
  140.    strcpy(oname, prefix);
  141.    strcat(oname, ".bin");
  142.    
  143.    if ((ofd = fopen( oname, "w")) == NULL)
  144.    {
  145.       fprintf(stderr, "\n Cannot open %s for writing.\n", oname);
  146.       return(1);
  147.    }
  148.    
  149.    strcpy(iname, prefix);
  150.    strcat(iname, ".info");
  151.    if ((fd = fopen(iname, "r")) == NULL)
  152.    {
  153.       fprintf(stderr, "No %s file!\n", iname);
  154.       fclose(ofd);
  155.       unlink(oname);
  156.       return(1);
  157.    }
  158.    
  159.    if (fread(buf, sizeof(*buf), 128, fd) > 0)
  160.    {
  161.       if (buf[74] & 0x40) buf[81] = '\1'; /* protected */
  162.       buf[74] = '\0'; /* clear zero2 */
  163.       fwrite(buf, sizeof(*buf), 128, ofd);
  164. #ifndef SYSV
  165.       bzero(buf, 128);
  166. #endif
  167.    }
  168.    fclose(fd);
  169.    
  170.    strcpy(dname, prefix);
  171.    strcat(dname, ".data");
  172.    if ((fd = fopen(dname, "r")) == NULL)
  173.    {
  174.       fprintf(stderr, "No %s file!\n", dname);
  175.       fclose(ofd);
  176.       unlink(oname);
  177.       return(1);
  178.    }
  179.    
  180.    while (fread(buf, sizeof(*buf), 128, fd) > 0)
  181.    {
  182.       fwrite(buf, sizeof(*buf), 128, ofd);
  183. #ifndef SYSV
  184.       bzero(buf, 128);
  185. #endif
  186.    }
  187.    fclose(fd);
  188.    
  189.    strcpy(rname, prefix);
  190.    strcat(rname, ".rsrc");
  191.    if ((fd = fopen(rname, "r")) == NULL)
  192.    {
  193.       fprintf(stderr, "No %s file!\n", rname);
  194.       fclose(ofd);
  195.       unlink(oname);
  196.       return(1);
  197.    }
  198.    while (fread(buf, sizeof(*buf), 128, fd) > 0)
  199.    {
  200.       fwrite(buf, sizeof(*buf), 128, ofd);
  201. #ifndef SYSV
  202.       bzero(buf, 128);
  203. #endif
  204.    }
  205.    fclose(fd);
  206.    
  207.    if (delete)
  208.    {
  209.       unlink(iname);
  210.       unlink(rname);
  211.       unlink(dname);
  212.    }
  213. }  /* macbin */
  214.  
  215. /* EOF */
  216.  
  217.