home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / util / unix / macutil2.sha / macutil / mixed / macstream.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-05  |  4.0 KB  |  184 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.         exit(0);
  72.         }
  73.     }
  74.     }
  75.     if(errflg || optind == argc) {
  76.     usage();
  77.     exit(1);
  78.     }
  79.  
  80.     if(info_only || query) {
  81.     list++;
  82.     }
  83.  
  84.     setup(argc - optind, argv + optind);
  85.     while((i = nextfile()) != ISATEND) {
  86.     if(dir_skip) {
  87.         if(i == ISDIR) {
  88.         dir_skip++;
  89.         } else if(i == ENDDIR) {
  90.         dir_skip--;
  91.         }
  92.         continue;
  93.     }
  94.  
  95.     write_it = 1;
  96.     n = file_info[I_NAMEOFF] & 0x7f;
  97.     transname(file_info + I_NAMEOFF + 1, text, n);
  98.     if(i == ISFILE) {
  99.         transname(file_info + I_TYPEOFF, ftype, 4);
  100.         transname(file_info + I_AUTHOFF, fauth, 4);
  101.     }
  102.     if(list) {
  103.         if(i == ISFILE) {
  104.         do_indent(indent);
  105.         (void)fprintf(stderr,
  106.             "name=\"%s\", type=%4.4s, author=%4.4s, data=%ld, rsrc=%ld",
  107.             text, ftype, fauth, (long)data_size, (long)rsrc_size);
  108.         } else if(i == ISDIR) {
  109.         do_indent(indent);
  110.         dir_ptr += 64;
  111.         if(dir_ptr == dir_max) {
  112.             if(dir_max == 0) {
  113.             dir_stack = malloc(64);
  114.             } else {
  115.             dir_stack = realloc(dir_stack, (unsigned)dir_max + 64);
  116.             }
  117.             dir_max += 64;
  118.             if(dir_stack == NULL) {
  119.             (void)fprintf(stderr, "Insufficient memory\n");
  120.             exit(1);
  121.             }
  122.         }
  123.         for(j = 0; j <= n; j++) {
  124.             dir_stack[dir_ptr + j] = text[j];
  125.         }
  126.         (void)fprintf(stderr, "folder=\"%s\"", text);
  127.         indent++;
  128.         } else {
  129.         indent--;
  130.         do_indent(indent);
  131.         (void)fprintf(stderr, "leaving folder \"%s\"",
  132.             dir_stack + dir_ptr);
  133.         dir_ptr -= 64;
  134.         }
  135.         if(info_only) {
  136.         write_it = 0;
  137.         }
  138.         if(query) {
  139.         if(i != ENDDIR) {
  140.             write_it = do_query();
  141.         } else {
  142.             (void)fputc('\n', stderr);
  143.         }
  144.         if(!write_it && i == ISDIR) {
  145.             dir_skip = 1;
  146.             indent--;
  147.             dir_ptr -= 64;
  148.         }
  149.         } else {
  150.         (void)fputc('\n', stderr);
  151.         }
  152.     }
  153.  
  154.     if(write_it) {
  155.         (void)fwrite(file_info, 1, 128, stdout);
  156.         if(i == ISFILE) {
  157.         if(data_size != 0) {
  158.             (void)fwrite(data_fork, 1, data_size, stdout);
  159.             i = (((data_size + 127) >> 7) << 7) - data_size;
  160.             while(i-- > 0) {
  161.             (void)fputc(0, stdout);
  162.             }
  163.         }
  164.         if(rsrc_size != 0) {
  165.             (void)fwrite(rsrc_fork, 1, rsrc_size, stdout);
  166.             i = (((rsrc_size + 127) >> 7) << 7) - rsrc_size;
  167.             while(i-- > 0) {
  168.             (void)fputc(0, stdout);
  169.             }
  170.         }
  171.         }
  172.     }
  173.     }
  174.     exit(0);
  175.     /* NOTREACHED */
  176. }
  177.  
  178. static void usage()
  179. {
  180.     (void)fprintf(stderr, "Usage: macstream [-%s] files\n", options);
  181.     (void)fprintf(stderr, "Use \"macstream -H\" for help.\n");
  182. }
  183.  
  184.