home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Amiga / Workbench / Archivers / mpackPPC.lha / mpackPPC / src / amigaunpk.c < prev    next >
C/C++ Source or Header  |  1998-04-08  |  6KB  |  177 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. #ifndef __PPC__
  26. #include <libraries/netsupport.h>
  27. #endif
  28.  
  29. #ifdef __SASC
  30. #include <proto/exec.h>
  31. #include <proto/dos.h>
  32. #ifndef __PPC__
  33. #include <proto/netsupport.h>
  34. #endif
  35. #else
  36. #include <clib/exec_protos.h>
  37. #include <clib/dos_protos.h>
  38. #include <clib/netsupport_protos.h>
  39. #endif
  40.  
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <errno.h>
  45. #include "version.h"
  46.  
  47. extern int didchat;
  48. extern int overwrite_files;
  49.  
  50. #if defined(__SASC) && (__VERSION__ > 5) && (__REVISION__ > 50)
  51. static const char DOSId[] = "\0$VER: MPack " MPACK_VERSION " " __AMIGADATE__ ;
  52. #else
  53. static const char DOSId[] = "\0$VER: MPack " MPACK_VERSION " (" __DATE__ ")" ;
  54. #endif
  55.  
  56. int quiet ;
  57.  
  58. #define TEMPLATE        "Files/M,-f=Overwrite/S,-C=Directory/K,-q=Quiet/S,-t=Text/S"
  59. enum { FILES, OVERWRITE, DIRECTORY, QUIET, EXTRACTTEXT, OPT_COUNT } ;
  60. static struct RDArgs *args = NULL, *my_args = NULL ;
  61. BPTR    OldDir = NULL ;
  62.  
  63. #ifndef __PPC__
  64. struct NetSupportLibrary *NetSupportBase;
  65. #endif
  66.  
  67. #define HELPSTRING "munpack version " MPACK_VERSION "\n\
  68. Unpack input files. If no files are present, reads from standard in. The\n\
  69. arguments other than file names are:\n\
  70. -f=Overwrite/S          Causes the unpacked file to overwrite any existing\n\
  71.                         files of the same name. Otherwise, an extension\n\
  72.                         with a period and a number is added to the file\n\
  73.                         name.\n\
  74. -C=Directory/K          Change to the given directory before unpacking.\n\
  75.                         Path names will be interpreted relative to the this\n\
  76.                         directory, not the current one.\n\
  77. -q=Quiet/S              Supress progress statements.\n\
  78. -t=Text/S               Unpack the Text part fo multipart messages.\n"
  79.  
  80. void warn(char *) ;
  81. void chat(char *) ;
  82.  
  83. void
  84. FreeSystem(void) {
  85.  
  86. #ifndef __PPC__
  87.         if (NetSupportBase) {
  88.                 UnLockFiles() ;
  89.                 CloseLibrary((struct Library *) NetSupportBase) ;
  90.                 }
  91. #endif
  92.  
  93.         if (args) FreeArgs(args) ;
  94.         if (my_args) FreeDosObject(DOS_RDARGS, args) ;
  95.         if (OldDir) UnLock(CurrentDir(OldDir)) ;
  96.         }
  97.  
  98. main(int argc, char **argv) {
  99.         long opts[OPT_COUNT] ;
  100.         FILE *file ;
  101.         int goodenough, extracttext ;
  102.         extern struct ExecBase *SysBase ;
  103.  
  104. #ifndef __PPC__
  105.         NetSupportBase = (struct NetSupportLibrary *) OldOpenLibrary(NETSUPPORTNAME) ;
  106. #endif
  107.  
  108.         goodenough = SysBase->LibNode.lib_Version > 36 ;
  109.  
  110.         /* Do the 2.x argument parsing stuff */
  111.         if (!goodenough) opts[FILES] = argc ;
  112.         else {
  113.                 onexit(FreeSystem) ;
  114.                 memset((char *) opts, 0, sizeof(opts)) ;
  115.                 if (!(my_args = AllocDosObject(DOS_RDARGS, NULL))) {
  116.                         PrintFault(IoErr(), *argv) ;
  117.                         exit(RETURN_FAIL) ;
  118.                         }
  119.                 my_args->RDA_ExtHelp = HELPSTRING ;
  120.                 if (!(args = ReadArgs(TEMPLATE, opts, my_args))) {
  121.                         PrintFault(IoErr(), *argv) ;
  122.                         exit(RETURN_FAIL) ;
  123.                         }
  124.                 overwrite_files = opts[OVERWRITE] ;
  125.                 quiet = opts[QUIET] ;
  126.                 extracttext = opts[EXTRACTTEXT] ;
  127.                 if (opts[DIRECTORY])
  128.                         if (OldDir = Lock((char *) opts[DIRECTORY], SHARED_LOCK))
  129.                                 OldDir = CurrentDir(OldDir) ;
  130.                         else PrintFault(IoErr(), (char *) opts[DIRECTORY]) ;
  131.  
  132.                 argv = ((char **) opts[FILES]) - 1 ;
  133.                 }
  134.  
  135. #ifndef __PPC__
  136.         if (!NetSupportBase)
  137.                 fprintf(stdout, "Couldn't open NetSupport.Library: Can't parse configfiles.\n");
  138. #endif
  139.  
  140.         if (!opts[FILES]) {
  141.                 fprintf(stderr, "reading from standard input\n");
  142.                 didchat = 0;
  143.                 handleMessage(part_init(stdin), "text/plain", 0, extracttext,
  144.                         (struct boundary *) NULL) ;
  145.                 if (!didchat)
  146.                     fprintf(stdout,
  147.                             "Did not find anything to unpack from standard input\n");
  148.                 }
  149.         else {
  150.                 while (*++argv)
  151.                         if (!(file = fopen(*argv, "r")))
  152.                                 os_perror(*argv) ;
  153.                         else {
  154.                                 didchat = 0 ;
  155.                                 handleMessage(part_init(file), "text/plain", 0,
  156.                                 extracttext, (struct boundary *) NULL) ;
  157.                                 fclose(file) ;
  158.                                 if (!didchat)
  159.                                         fprintf(stdout,
  160.                                                 "Did not find anything to unpack from %s\n", *argv);
  161.                                 }
  162.                         }
  163.  
  164.         exit(0) ;
  165.         }
  166.  
  167. void
  168. warn(char *s) {
  169.     fprintf(stderr, "munpack: warning: %s\n", s);
  170. }
  171.  
  172. void
  173. chat(char *s) {
  174.         didchat = 1;
  175.         if (!quiet) fprintf(stderr, "munpack: %s\n", s);
  176.         }
  177.