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

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