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

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