/* macbin.c
 
   Purpose:  Convert .data, .info, and .rsrc files into one .bin
      file for easy downloading (etc).
      
            MyMacFile.data  \
            MyMacFile.info   >  MyMacFile.bin
            MyMacFile.rsrc  /

   [this replaces the file macbinary.shar on sumex-aim.stanford.edu]

   Version History:
      1.0      Jim Budler        First release.  Handled one file
               jimb@amdcad.UUCP  only, and didn't have the ability
                                 to delete the .d, .i, .r files.
      
      2.0      Mike Gleason      Added batch files, option to delete
                                 the .d, .i, .r, files after
                                 processing.
                           
      3.0      Mike Gleason      Improved batch file handling, so
                                 the user wouldn't need to type
                                 "macbin file1 file2 file3 .. filen"
                                 but instead "macbin *". Obviously,
                                 in previous versions using "macbin *"
                                 would assume we had files like
                                 file1.data.data, file1.data.info,
                                 file1.data.rsrc, file1.info.data...
                                 One would realize the great
                                 convenience of this feature if you
                                 have had to unsit a whole buttload
                                 of files, and then type in mile-long
                                 command lines.
                        
*/
 
                        
/* structure of the info file - from MacBin.C
   char zero1;
   char nlen;
   char name[63];
   char type[4];     65 0101
   char creator[4];  69
   char flags;    73
   char zero2;    74 0112
   char location[6]; 80
   char protected;      81 0121
   char zero3;    82 0122
   char dflen[4];
   char rflen[4];
   char cdate[4];
   char mdate[4];
*/
 
/* #define SYSV */
/* 
   Note:  to run this program on a System V Unix system, remove all calls to
   the bzero() procedure.  It doesn't exist on System V, and isn't needed.
   [To do this easily, just uncomment the above line, and the lines
    containing bzero() will not be compiled. --MG] */
 
#include <stdio.h>
#include <string.h>
 
void     main (int argc, char **argv);
int      FilterArgumentList(int argc, char **argv);
int      macbin (char *prefix, int delete);
 
void     main (int argc, char **argv)
{
   int i, deleteOn = 0;
   
   if (argc < 2) 
   {
      fprintf(stderr,
         "\nUsage: %s [-d] <list of files>\n"
         "\nPurpose: merges <prefix>.info, <prefix>.data, <prefix>.rsrc"
         "\n  into a valid macbinary file, <prefix>.bin, and optionally"
         "\n  deletes the .info, .data, .rsrc files (the -d).\n",argv[0]);
      exit(1);
   }
   
   if (argc > 3)
      FilterArgumentList(argc, argv);
   
   for (i=1; i<argc; i++)
      if (argv[i][0] == '-' && argv[i][1] == 'd')
         deleteOn = !deleteOn;   /* you can toggle delete mode on/off */
      else
         if (*argv[i])  /* if the first character is not 0 */
            macbin(argv[i], deleteOn);
}  /* main */
 
 
 
 
 
int      FilterArgumentList(int argc, char **argv)
{
   register int i;
   
   for (i=1; i<argc-2; i++)
   {
      if    (
         strcmp(argv[i], ".data") > 0 &&  
         strcmp(argv[i+1], ".info") > 0 &&
         strcmp(argv[i+2], ".rsrc")
         )
      {
         /* if 3 successive arguments contain .data,
            .info, and .rsrc, (i.e. we have MyFkey.data,
            MyFkey.info, and MyFkey.rsrc) then let's really
            only pass the prefix (i.e. "MyFkey") */
            
         argv[i][ strlen(argv[i]) - 5 ] = 
            argv[i+1][0] = argv[i+2][0] = '\0';
            
         /* we'll use the first character of an argument as a
            signal to ignore it. */
      }
   }
}  /* FilterArgumentList */
 
 
 
 
int      macbin (char *prefix, int delete)
{
   FILE *fd, *ofd;
   char oname[128];
   char iname[128];
   char dname[128];
   char rname[128];
   char buf[128];
   
#ifndef SYSV
   bzero(buf, 128);
#endif
 
   strcpy(oname, prefix);
   strcat(oname, ".bin");
   
   if ((ofd = fopen( oname, "w")) == NULL)
   {
      fprintf(stderr, "\n Cannot open %s for writing.\n", oname);
      return(1);
   }
   
   strcpy(iname, prefix);
   strcat(iname, ".info");
   if ((fd = fopen(iname, "r")) == NULL)
   {
      fprintf(stderr, "No %s file!\n", iname);
      fclose(ofd);
      unlink(oname);
      return(1);
   }
   
   if (fread(buf, sizeof(*buf), 128, fd) > 0)
   {
      if (buf[74] & 0x40) buf[81] = '\1'; /* protected */
      buf[74] = '\0'; /* clear zero2 */
      fwrite(buf, sizeof(*buf), 128, ofd);
#ifndef SYSV
      bzero(buf, 128);
#endif
   }
   fclose(fd);
   
   strcpy(dname, prefix);
   strcat(dname, ".data");
   if ((fd = fopen(dname, "r")) == NULL)
   {
      fprintf(stderr, "No %s file!\n", dname);
      fclose(ofd);
      unlink(oname);
      return(1);
   }
   
   while (fread(buf, sizeof(*buf), 128, fd) > 0)
   {
      fwrite(buf, sizeof(*buf), 128, ofd);
#ifndef SYSV
      bzero(buf, 128);
#endif
   }
   fclose(fd);
   
   strcpy(rname, prefix);
   strcat(rname, ".rsrc");
   if ((fd = fopen(rname, "r")) == NULL)
   {
      fprintf(stderr, "No %s file!\n", rname);
      fclose(ofd);
      unlink(oname);
      return(1);
   }
   while (fread(buf, sizeof(*buf), 128, fd) > 0)
   {
      fwrite(buf, sizeof(*buf), 128, ofd);
#ifndef SYSV
      bzero(buf, 128);
#endif
   }
   fclose(fd);
   
   if (delete)
   {
      unlink(iname);
      unlink(rname);
      unlink(dname);
   }
}  /* macbin */
 
/* EOF */

