home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / macutils.lzh / MACUTILS / BINHEX / binhex.c < prev    next >
C/C++ Source or Header  |  1996-02-01  |  4KB  |  181 lines

  1. #include <stdio.h>
  2. #include "../fileio/machdr.h"
  3. #include "../fileio/rdfile.h"
  4. #include "../util/patchlevel.h"
  5.  
  6. #ifdef OSK
  7. #define  fputc(c,path) fprintf(path,"%c",c)
  8. #endif
  9.  
  10. extern char *malloc();
  11. extern char *realloc();
  12. extern char *strcat();
  13. extern void exit();
  14. extern void transname();
  15. extern void do_indent();
  16. extern void dofile();
  17.  
  18. #define LOCALOPT    "RilqVH"
  19.  
  20. static void usage();
  21.  
  22. static char options[128];
  23. static char *dir_stack;
  24. static int dir_ptr = -64;
  25. static int dir_max;
  26. int dorep = 1;
  27.  
  28. int main(argc, argv)
  29. int argc;
  30. char **argv;
  31. {
  32.     int c, i, j, n;
  33.     extern int optind;
  34.     extern char *optarg;
  35.     int errflg;
  36.     char text[32], ftype[5], fauth[5];
  37.     int dir_skip = 0, write_it, query = 0, list = 0, info_only = 0;
  38.     int indent = 0;
  39.  
  40.     (void)strcat(options, get_rdfileopt());
  41.     (void)strcat(options, LOCALOPT);
  42.     errflg = 0;
  43.  
  44.     while((c = getopt(argc, argv, options)) != EOF) {
  45.     if(!rdfileopt((char)c)) {
  46.         switch(c) {
  47.         case 'R':
  48.         dorep = 0;
  49.         break;
  50.         case 'l':
  51.         list++;
  52.         break;
  53.         case 'q':
  54.         query++;
  55.         break;
  56.         case 'i':
  57.         info_only++;
  58.         break;
  59.         case '?':
  60.         errflg++;
  61.         break;
  62.         case 'H':
  63.         give_rdfileopt();
  64.         (void)fprintf(stderr, "Binhex specific options:\n");
  65.         (void)fprintf(stderr, "-r:\tdo not use run length encoding\n");
  66.         (void)fprintf(stderr,
  67.             "-i:\tgive information only, do not write\n");
  68.         (void)fprintf(stderr, "-l:\tgive listing\n");
  69.         (void)fprintf(stderr,
  70.             "-q:\tquery for every file/folder before writing\n");
  71.         (void)fprintf(stderr,
  72.             "-V:\tgive information about this version\n");
  73.         (void)fprintf(stderr, "-H:\tthis message\n");
  74.         (void)fprintf(stderr, "Default is silent writing\n");
  75.         exit(0);
  76.         case 'V':
  77.         (void)fprintf(stderr, "Version %s, ", VERSION);
  78.         (void)fprintf(stderr, "patchlevel %d", PATCHLEVEL);
  79.         (void)fprintf(stderr, "%s.\n", get_minb());
  80. #ifdef OSK
  81.         (void)fprintf(stderr,"OSK Port by Dean Leiber\n");
  82. #endif
  83.         exit(0);
  84.         }
  85.     }
  86.     }
  87.     if(errflg) {
  88.     usage();
  89.     exit(1);
  90.     }
  91.  
  92.     if(info_only || query) {
  93.     list++;
  94.     }
  95.  
  96.     setup(argc - optind, argv + optind);
  97.     while((i = nextfile()) != ISATEND) {
  98.     if(dir_skip) {
  99.         if(i == ISDIR) {
  100.         dir_skip++;
  101.         } else if(i == ENDDIR) {
  102.         dir_skip--;
  103.         }
  104.         continue;
  105.     }
  106.  
  107.     write_it = 1;
  108.     n = file_info[I_NAMEOFF] & 0x7f;
  109.     transname(file_info + I_NAMEOFF + 1, text, n);
  110.     if(i == ISFILE) {
  111.         transname(file_info + I_TYPEOFF, ftype, 4);
  112.         transname(file_info + I_AUTHOFF, fauth, 4);
  113.     }
  114.     if(list) {
  115.         if(i == ISFILE) {
  116.         do_indent(indent);
  117.         (void)fprintf(stderr,
  118.             "name=\"%s\", type=%4.4s, author=%4.4s, data=%ld, rsrc=%ld",
  119.             text, ftype, fauth, (long)data_size, (long)rsrc_size);
  120.         } else if(i == ISDIR) {
  121.         do_indent(indent);
  122.         dir_ptr += 64;
  123.         if(dir_ptr == dir_max) {
  124.             if(dir_max == 0) {
  125.             dir_stack = malloc(64);
  126.             } else {
  127.             dir_stack = realloc(dir_stack, (unsigned)dir_max + 64);
  128.             }
  129.             dir_max += 64;
  130.             if(dir_stack == NULL) {
  131.             (void)fprintf(stderr, "Insufficient memory\n");
  132.             exit(1);
  133.             }
  134.         }
  135.         for(j = 0; j <= n; j++) {
  136.             dir_stack[dir_ptr + j] = text[j];
  137.         }
  138.         (void)fprintf(stderr, "folder=\"%s\"", text);
  139.         indent++;
  140.         } else {
  141.         indent--;
  142.         do_indent(indent);
  143.         (void)fprintf(stderr, "leaving folder \"%s\"",
  144.             dir_stack + dir_ptr);
  145.         dir_ptr -= 64;
  146.         }
  147.         if(info_only) {
  148.         write_it = 0;
  149.         }
  150.         if(query) {
  151.         if(i != ENDDIR) {
  152.             write_it = do_query();
  153.         } else {
  154.             (void)fputc('\n', stderr);
  155.         }
  156.         if(!write_it && i == ISDIR) {
  157.             dir_skip = 1;
  158.             indent--;
  159.             dir_ptr -= 64;
  160.         }
  161.         } else {
  162.         (void)fputc('\n', stderr);
  163.         }
  164.     }
  165.  
  166.     if(write_it) {
  167.         if(i == ISFILE) {
  168.         dofile();
  169.         }
  170.     }
  171.     }
  172.     exit(0);
  173.     /* NOTREACHED */
  174. }
  175.  
  176. static void usage()
  177. {
  178.     (void)fprintf(stderr, "Usage: binhex [-%s] [files]\n", options);
  179.     (void)fprintf(stderr, "Use \"binhex -H\" for help.\n");
  180. }
  181.