home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / aufstols.zoo / aufstools.1 / binhex / binhex.c < prev    next >
C/C++ Source or Header  |  1991-02-26  |  4KB  |  169 lines

  1. /*
  2.  * binhex -- binhex aufs files
  3.  *
  4.  * if called binhex handles aufs files, if called unxbin handles *.{rsrc,data,info} files
  5.  * will process multiple files
  6.  *
  7.  * Nigel Perry, Aug 90, np@doc.ic.ac.uk
  8.  *
  9.  * This is a hacked version of...
  10.  *
  11.  * unxbin -- convert files generated by xbin or macget into BinHex 4.0 format.
  12.  *
  13.  * David Gentzel, Lexeme Corporation
  14.  *
  15.  * (c) 1985 David Gentzel
  16.  * may be used but not sold without permission
  17.  *
  18.  * This is based on a Unix(tm) program with the same name and function written
  19.  * by ????.  Original was a series of small programs (8to6, crc, etc.) piped
  20.  * together and run by a shell script.  I completely rewrote the system as a
  21.  * C program (speeding it up considerably, needless to say), added run-length
  22.  * compression, and bullet-proofed (at least partly) the thing.  Unfortunately,
  23.  * I have lost the name of the original poster (to net.sources.mac) without
  24.  * whom this would never have appeared.
  25.  *
  26.  * created dbg 09/10/85 -- Version 1.0
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include "aufs.h"
  31.  
  32. #ifdef VMS
  33. # define PROGRAMNAME    "unxbin"
  34. # define EXIT_ERROR    ((1 << 28) | 2)
  35. # ifndef MAXNAMLEN
  36. #  define MAXNAMLEN    127
  37. #  define MAXBASENAME    63
  38. # endif
  39. #else
  40. # include <sys/types.h>
  41. # include <sys/dir.h>
  42. # define PROGRAMNAME    (argv[0])
  43. # define AUFSNAME    "binhex"
  44. # define EXIT_ERROR    1
  45. # ifndef MAXNAMLEN
  46. #  ifdef DIRSIZ
  47. #   define MAXNAMLEN    DIRSIZ
  48. #  else
  49. #   define MAXNAMLEN    14
  50. #  endif
  51. # endif
  52. # define MAXBASENAME    (MAXNAMLEN - 2)
  53. #endif
  54.  
  55. extern char *sprintf(), *strrchr();
  56. extern void aufs_gethead(), gethead(), fakehead(), make_buffer_crc(), make_file_crc(),
  57.         putchar_run();
  58.  
  59. main(argc, argv)
  60. int argc;
  61. register char *argv[];
  62. {
  63.     register FILE *rsrc, *data, *info;
  64.     char fbuf[256], infobuf[128];
  65.     register char *file;
  66.     int aufs;
  67.     int i;
  68.     FinderInfo fndr_info;
  69.  
  70.     aufs = strcmp(PROGRAMNAME, AUFSNAME) == 0;
  71.  
  72.     if (argc < 2)
  73.     {
  74.     fprintf(stderr, "Usage: %s file(s)\n", PROGRAMNAME);
  75.     exit(EXIT_ERROR);
  76.     }
  77.  
  78.     for(i = 1; i < argc; i++)
  79.     {
  80. #ifdef VMS
  81.     if ((file = strrchr(argv[i], ']')) == NULL)
  82.         file = strrchr(argv[i], ':');
  83. #else
  84.     file = strrchr(argv[i], '/');
  85. #endif
  86.     if (file)
  87.         file++;
  88.     else
  89.         file = argv[i];
  90.     if (strlen(file) > MAXBASENAME)
  91.         file[MAXBASENAME] = '\0';
  92.     file = argv[i];
  93.     (void) sprintf(fbuf, aufs ? ".resource/%s" : "%s.rsrc", file);
  94.     fbuf[MAXNAMLEN] = '\0';
  95.     rsrc = fopen(fbuf, "r");
  96.     (void) sprintf(fbuf, aufs ? "%s" : "%s.data", file);
  97.     fbuf[MAXNAMLEN] = '\0';
  98.     data = fopen(fbuf, "r");
  99.     if (rsrc == NULL && data == NULL)
  100.     {
  101.         fprintf(stderr, "No resource or data forks for %s\n", argv[i]);
  102.         exit(EXIT_ERROR);
  103.     }
  104.     if (rsrc == NULL)
  105.         fprintf(stderr, "Warning: no resource file %s\n", fbuf);
  106.     if (data == NULL)
  107.         fprintf(stderr, "Warning: no data file %s\n", fbuf);
  108.     (void) sprintf(fbuf, aufs ? ".finderinfo/%s" : "%s.info", file);
  109.     fbuf[MAXNAMLEN] = '\0';
  110.     info = fopen(fbuf, "r");
  111.     if (info == NULL)
  112.         fprintf(stderr, "Warning: no info file %s\n", fbuf);
  113.  
  114.     if(aufs)
  115.     {    /* make the .finderinfo file */
  116.         FILE *oinfo;
  117.  
  118.         sprintf(fbuf, ".finderinfo/%s.Hqx", file);
  119.         if((oinfo = fopen(fbuf, "w")) == NULL)
  120.         {   perror(fbuf);
  121.         exit(1);
  122.         }
  123.         bzero(&fndr_info, sizeof(FinderInfo));
  124.         bcopy("TEXT", &fndr_info.fndr_type, 4);
  125.         bcopy("BnHq", &fndr_info.fndr_creator, 4);
  126.         fndr_info.fi_magic1 = FI_MAGIC1;
  127.         fndr_info.fi_version = FI_VERSION;
  128.         fndr_info.fi_magic = FI_MAGIC;
  129.         fndr_info.fi_bitmap = FI_BM_MACINTOSHFILENAME;
  130.         strcpy(fndr_info.fi_macfilename, file);
  131.         fwrite(&fndr_info, sizeof(FinderInfo), 1, oinfo);
  132.         fclose(oinfo);
  133.     }
  134.  
  135.     (void) sprintf(fbuf, "%s.Hqx", file);
  136.     fbuf[MAXNAMLEN] = '\0';
  137.     if (freopen(fbuf, "w", stdout) == NULL)
  138.     {
  139.         fputs("Couldn't open output file.\n", stderr);
  140.         exit(EXIT_ERROR);
  141.     }
  142.     fputs("(This file must be converted with BinHex 4.0)\n:", stdout);
  143.     if (info != NULL)
  144.     {   if(aufs)
  145.         {   (void) fread(&fndr_info, sizeof(FinderInfo), 1, info);
  146.         (void) fclose(info);
  147.         aufs_gethead(&fndr_info, data, rsrc, infobuf);
  148.         }
  149.         else
  150.         {   (void) fread(fbuf, 128, 1, info);
  151.         (void) fclose(info);
  152.         gethead(fbuf, infobuf);
  153.         }
  154.     }
  155.     else
  156.         fakehead(file, rsrc, data, infobuf);
  157.     make_buffer_crc(infobuf, 20 + infobuf[0]);
  158.     make_file_crc(data);
  159.     if (data != NULL)
  160.         (void) fclose(data);
  161.     make_file_crc(rsrc);
  162.     if (rsrc != NULL)
  163.         (void) fclose(rsrc);
  164.     putchar_run(EOF);
  165.     puts(":");
  166.     }
  167.     (void) fclose(stdout);
  168. }
  169.