home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / apple2 / 60 < prev    next >
Encoding:
Internet Message Format  |  1991-05-02  |  5.6 KB

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