home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / sources / apple2 / 16 < prev    next >
Encoding:
Text File  |  1992-11-08  |  6.0 KB  |  254 lines

  1. Path: sparky!uunet!gatech!rutgers!igor.rutgers.edu!yoko.rutgers.edu!jac
  2. From: jac@yoko.rutgers.edu (Jonathan A. Chandross)
  3. Newsgroups: comp.sources.apple2
  4. Subject: v001ADM23:  Repost of AAF Unpacker
  5. Message-ID: <Nov.8.22.07.00.1992.17922@yoko.rutgers.edu>
  6. Date: 9 Nov 92 03:07:01 GMT
  7. Organization: Rutgers Univ., New Brunswick, N.J.
  8. Lines: 243
  9. Approved: jac@paul.rutgers.edu
  10.  
  11.  
  12. Posting-number: Volume 1, Administrivia: 23
  13. Subject:  v001ADM22:  Repost of AAF Unpacker
  14.  
  15.  
  16. This posting is a repost of V1 #1 intended for those of you lacking a
  17. mechanism to unpack AAF (Apple Archive Format) files.
  18.  
  19. THIS POSTING SHOULD *NOT* BE ARCHIVED.
  20.  
  21. IT CONTAINS NO NEW SOURCE.
  22.  
  23. Enjoy.
  24.  
  25. (As I pull this from my archives, I find that I am somewhat embarrassed
  26.  by the code; I don't code like this anymore. However, the code works
  27.  just fine and I'm not about to rewrite it today... JAC)
  28.  
  29. #################################### CUT HERE ##################################
  30.  
  31. /*
  32.  *******************************************************************************
  33.  *
  34.  * upaaf.c
  35.  *
  36.  * Unpack an archive in apple format.
  37.  *
  38.  * Another quality product handcrafted with loving care by:
  39.  *    Jonathan A. Chandross
  40.  *    jac@paul.rutgers.edu
  41.  *    November 1990
  42.  *
  43.  * COPYRIGHT 1990 by Jonathan A. Chandross
  44.  * All rights reserved.
  45.  *
  46.  * Use "cc -D SHELL" to compile for use with a shell.
  47.  *
  48.  *******************************************************************************
  49.  */
  50.  
  51. #include <stdio.h>
  52.  
  53. /*
  54.  *******************************************************************************
  55.  *
  56.  * Defines
  57.  *
  58.  * EXPORT        function is visible outside this module
  59.  * IMPORT        function is defined outside this module
  60.  * STATIC        function is invisible outside this module
  61.  *
  62.  * LENGTH        length of strings
  63.  *
  64.  *******************************************************************************
  65.  */
  66. #define EXPORT
  67. #define IMPORT    extern
  68. #define LOCAL    static
  69.  
  70. #define LENGTH    255
  71.  
  72. /*
  73.  *******************************************************************************
  74.  *
  75.  * main
  76.  *
  77.  * Unpacks a file in Apple Archive Format
  78.  * 
  79.  * Parameters:
  80.  *    argc            number of command line arguments if compiled
  81.  *                for a shell
  82.  *    argv            command line argument vector
  83.  * Locals:
  84.  *    archive_name        name of archive to unpack if not using a
  85.  *                shell
  86.  * Returns:
  87.  *
  88.  * If this program is compiled for use with a shell, the first command line
  89.  * argument is considered to contain the name of the archive to unpack.  If
  90.  * this program is compiled stand-alone, i.e. not for use with a shell, it
  91.  * prompts for the archive to unpack.
  92.  *
  93.  * The SHELL define is an unsightly way to do things, but there is little
  94.  * choice if this program is to be used on an Apple lacking some sort of
  95.  * shell.
  96.  *
  97.  *******************************************************************************
  98.  */
  99. #ifdef SHELL
  100. EXPORT int main(argc, argv)
  101. int        argc;
  102. char        *argv[];
  103. #else
  104. EXPORT int main()
  105. #endif
  106. {
  107. IMPORT    void    unpack();
  108. #ifdef SHELL
  109. IMPORT    void    usage();
  110. #else
  111. char        archive_name[LENGTH];
  112. #endif
  113.  
  114. #ifdef SHELL
  115.     /*
  116.      * If we are using a shell, then argv[1] will be the name of the
  117.      * archive to unpack.
  118.      */
  119.     if(argc == 2)
  120.         unpack(argv[1]);
  121.     else {
  122.         usage(argv[0]);
  123.         exit(1);
  124.         }
  125. #else
  126.     (void) fputs("Enter name of archive to unpack:", stdout);
  127.     (void) scanf("%s", archive_name);
  128.     unpack(archive_name);
  129. #endif
  130.  
  131.     return(0);
  132. }
  133.  
  134. /*
  135.  *******************************************************************************
  136.  *
  137.  * usage
  138.  *
  139.  * Print out a message on how to use this program
  140.  * 
  141.  * Parameters:
  142.  *    object_name        name of object file storing this program
  143.  * Locals:
  144.  * Returns:
  145.  *
  146.  *******************************************************************************
  147.  */
  148. #ifdef SHELL
  149. LOCAL void usage(object_name)
  150. char        *object_name;
  151. {
  152.     (void) fprintf(stderr, "Usage: %s archive-name\n", object_name);
  153. }
  154. #endif
  155.  
  156. /*
  157.  *******************************************************************************
  158.  *
  159.  * unpack
  160.  *
  161.  * Unpack an archive
  162.  * 
  163.  * Parameters:
  164.  *    archive_file_name    name of archive file to unpack
  165.  * Locals:
  166.  *    buffer            holds each line as it is read out of archive
  167.  *    archive_file        archive file
  168.  *    output_file        current file being unpacked from archive
  169.  * Returns:
  170.  *
  171.  *******************************************************************************
  172.  */
  173. LOCAL void unpack(archive_file_name)
  174. char        *archive_file_name;
  175. {
  176. IMPORT    char    *fgets();
  177. IMPORT    int    fput();
  178. IMPORT    int    strlen();
  179. IMPORT    int    fclose();
  180. IMPORT    FILE    *fopen();
  181. char        buffer[LENGTH];
  182. FILE        *archive_file;
  183. FILE        *output_file = (FILE *)NULL;
  184.  
  185.     /*
  186.      * Open the archive file and make sure it exists.
  187.      */
  188.     if((archive_file = fopen(archive_file_name, "r")) == (FILE *)NULL) {
  189.         (void) fprintf(stderr, "Error: could not open archive '%s'\n",
  190.             archive_file_name);
  191.         exit(1);
  192.         }
  193.     
  194.     /*
  195.      * As long as the file is not empty, process lines
  196.      */
  197.     while(fgets(buffer, LENGTH, archive_file) != (char *)NULL) {
  198.         /*
  199.          * What kind of line do we have? 
  200.          */
  201.         switch(buffer[0]) {
  202.              case '=':
  203.             /*
  204.              * Have a file name.  Remove the trailing newline
  205.              * from the file-name (left by fgets) by overwriting
  206.              * with a Null.  Then open the file and verify that
  207.              * we can write to it.  Skip over the first character
  208.              * in the file-name (it is an '=') 
  209.              */
  210.             buffer[strlen(buffer) - 1] = '\0';
  211.             (void) fprintf(stdout, "Unpacking '%s'\n", &buffer[1]);
  212.  
  213.             /*
  214.              * If we have an open output file, close it.
  215.              */
  216.             if(output_file != (FILE *)NULL)
  217.                 (void) fclose(output_file);
  218.  
  219.             if((output_file = fopen(&buffer[1], "w")) ==
  220.                (FILE *)EOF) {
  221.                 (void) fprintf(stderr,
  222.                     "Error: could not open '%s'\n", buffer);
  223.                 exit(1);
  224.                 }
  225.             break;
  226.              case '-':
  227.             /*
  228.              * Have a line of source.  Add the line to the output
  229.              * file without the leading "-".
  230.              */
  231.             if(output_file == (FILE *)NULL) {
  232.                 fputs("Error: no file specified.\n", stderr);
  233.                 exit(1);
  234.                 }
  235.             else
  236.                 (void) fputs(&buffer[1], output_file);
  237.             break;
  238.              case '+':
  239.             /*
  240.              * End of archive.  Exit.
  241.              */
  242.             exit(0);
  243.             break;
  244.              default:
  245.             /*
  246.              * Have some other type of line (mail header,
  247.              * signature, comment, etc.)  Output it.
  248.              */
  249.             (void) fputs(&buffer[0], stdout);
  250.             break;
  251.              }
  252.         }
  253. }
  254.