home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / mpack-1.2 / amigaunpk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  3.6 KB  |  123 lines

  1. /* (C) Copyright 1993 by Mike W. Meyer
  2.  *
  3.  * Permission to use, copy, modify, distribute, and sell this software
  4.  * and its documentation for any purpose is hereby granted without
  5.  * fee, provided that the above copyright notice appear in all copies
  6.  * and that both that copyright notice and this permission notice
  7.  * appear in supporting documentation, and that the name of Mike W.
  8.  * Meyer not be used in advertising or publicity pertaining to
  9.  * distribution of the software without specific, written prior
  10.  * permission.  Mike W. Meyer makes no representations about the
  11.  * suitability of this software for any purpose.  It is provided "as
  12.  * is" without express or implied warranty.
  13.  *
  14.  * MIKE W. MEYER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  15.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16.  * FITNESS, IN NO EVENT SHALL MIKE W. MEYER BE LIABLE FOR ANY SPECIAL,
  17.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  18.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  19.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  20.  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22. #include <exec/types.h>
  23. #include <exec/execbase.h>
  24. #include <dos/dos.h>
  25.  
  26. #ifdef __SASC
  27. #include <proto/exec.h>
  28. #include <proto/dos.h>
  29. #else
  30. #include <clib/exec_protos.h>
  31. #include <clib/dos_protos.h>
  32. #endif
  33.  
  34. #include <stdio.h>
  35. #include <errno.h>
  36. #include "version.h"
  37.  
  38. #ifdef AMIGA
  39. static const char DOSId[] = "\0$VER: MUnpack " MPACK_VERSION " (" __DATE__ ")" ;
  40. #endif
  41.  
  42. extern int optind;
  43. extern char *optarg;
  44.  
  45. extern int overwrite_files ;
  46.  
  47. #define TEMPLATE    "Files/M,-f=Overwrite/S,-C=Directory/K"
  48. enum { FILES, OVERWRITE, DIRECTORY, OPT_COUNT } ;
  49. struct RDArgs *args = NULL, *my_args = NULL ;
  50. BPTR    OldDir = NULL ;
  51.  
  52. #define HELPSTRING "munpack vesion " MPACK_VERSION "\n\
  53. Unpack input files. If no files are present, reads from standard in. The\n\
  54. arguments other than file names are:\n\
  55. -f=Overwrite/S        Causes the unpacked file to overwrite any existing\n\
  56.             files of the same name. Otherwise, an extension\n\
  57.             with a period and a number is added to the file\n\
  58.             name.\n\
  59. -C=Directory/K        Change to the given directory before unpacking.\n\
  60.             Path names will be interpreted relative to the this\n\
  61.             directory, not the current one.\n"
  62.  
  63. void
  64. FinishArgs(void) {
  65.  
  66.     if (args) FreeArgs(args) ;
  67.     if (my_args) FreeDosObject(DOS_RDARGS, args) ;
  68.     if (OldDir) UnLock(CurrentDir(OldDir)) ;
  69.     }
  70.  
  71. main(int argc, char **argv) {
  72.     long opts[OPT_COUNT] ;
  73.     FILE *file ;
  74.     int goodenough ;
  75.     extern struct ExecBase *SysBase ;
  76.  
  77.     goodenough = SysBase->LibNode.lib_Version > 36 ;
  78.  
  79.     /* Do the 2.x argument parsing stuff */
  80.     if (!goodenough) opts[FILES] = argc ;
  81.     else {
  82.         onexit(FinishArgs) ;
  83.         memset((char *) opts, 0, sizeof(opts)) ;
  84.         if (!(my_args = AllocDosObject(DOS_RDARGS, NULL))) {
  85.             PrintFault(IoErr(), *argv) ;
  86.             exit(RETURN_FAIL) ;
  87.             }
  88.         my_args->RDA_ExtHelp = HELPSTRING ;
  89.         if (!(args = ReadArgs(TEMPLATE, opts, my_args))) {
  90.             PrintFault(IoErr(), *argv) ;
  91.             exit(RETURN_FAIL) ;
  92.             }
  93.         overwrite_files = opts[OVERWRITE] ;
  94.     
  95.         if (opts[DIRECTORY])
  96.             if (OldDir = Lock((char *) opts[DIRECTORY], SHARED_LOCK))
  97.                 OldDir = CurrentDir(OldDir) ;
  98.             else PrintFault(IoErr(), (char *) opts[DIRECTORY]) ;
  99.  
  100.         argv = ((char **) opts[FILES]) - 1 ;
  101.         }
  102.  
  103.     if (!opts[FILES]) {
  104.         fprintf(stderr, "munpack: reading from standard input\n");
  105.         handleMessage(stdin, "text/plain", 0) ;
  106.         }
  107.     else {
  108.         while (*++argv)
  109.             if (!(file = fopen(*argv, "r")))
  110.                 os_perror(*argv) ;
  111.             else {
  112.                 handleMessage(file, "text/plain", 0) ;
  113.                 fclose(file) ;
  114.                 }
  115.             }
  116.  
  117.     exit(0) ;
  118.     }
  119.  
  120. warn(char *s) {
  121.     fprintf(stderr, "munpack: warning: %s\n", s);
  122.     }
  123.