home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / apple2 / 21 < prev    next >
Encoding:
Text File  |  1990-11-26  |  5.8 KB  |  247 lines

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