home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / archivers / mpackppc-wos / src / os2unpk.c < prev    next >
C/C++ Source or Header  |  1998-04-27  |  4KB  |  168 lines

  1. /* (C) Copyright 1993,1994 by Carnegie Mellon University
  2.  * All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software
  5.  * and its documentation for any purpose is hereby granted without
  6.  * fee, provided that the above copyright notice appear in all copies
  7.  * and that both that copyright notice and this permission notice
  8.  * appear in supporting documentation, and that the name of Carnegie
  9.  * Mellon University not be used in advertising or publicity
  10.  * pertaining to distribution of the software without specific,
  11.  * written prior permission.  Carnegie Mellon University makes no
  12.  * representations about the suitability of this software for any
  13.  * purpose.  It is provided "as is" without express or implied
  14.  * warranty.
  15.  *
  16.  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
  17.  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  18.  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
  19.  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  20.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  21.  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  22.  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23.  * SOFTWARE.
  24.  */
  25. #define INCL_DOSFILEMGR
  26. #define INCL_DOSMISC
  27. #include <os2.h>
  28. #include <stdio.h>
  29. #include <errno.h>
  30. #include <string.h>
  31. #include "version.h"
  32. #include "part.h"
  33. #include "xmalloc.h"
  34.  
  35. #ifndef __EMX__
  36. #include <direct.h>
  37. #endif
  38.  
  39. extern int optind;
  40. extern char *optarg;
  41.  
  42. extern int overwrite_files;
  43. extern int didchat;
  44. extern int mime_eas;
  45. int quiet;
  46.  
  47. main(argc, argv)
  48. int argc;
  49. char **argv;
  50. {
  51.     int opt;
  52.     FILE *file;
  53.     char *p, *q, *path = 0;
  54.     int extractText = 0;
  55.     int done, gotone;
  56.     HDIR Findhandle;
  57.     ULONG FindCount;
  58.     FILEFINDBUF3 ffblk;
  59.     
  60.     DosError(0); /* switch off these annoying OS/2 dialog boxes */
  61.  
  62.     while ((opt = getopt(argc, argv, "eqftC:")) != EOF) {
  63.     switch (opt) {
  64.     case 'f':
  65.         overwrite_files = 1;
  66.         break;
  67.  
  68.     case 'q':
  69.         quiet = 1;
  70.         break;
  71.  
  72.     case 'e':
  73.         mime_eas = 1;
  74.         break;
  75.  
  76.     case 't':
  77.         extractText = 1;
  78.         break;
  79.  
  80.     case 'C':
  81.         if (chdir(optarg)) {
  82.         perror(optarg);
  83.         exit(1);
  84.         }
  85.         break;
  86.  
  87.     default:
  88.         usage();
  89.     }
  90.     }
  91.  
  92.     if (optind == argc) {
  93.     fprintf(stderr, "munpack: reading from standard input\n");
  94.     didchat = 0;
  95.     handleMessage(part_init(stdin), "text/plain", 0, extractText);
  96.     if (!didchat) {
  97.         fprintf(stdout,
  98.             "Did not find anything to unpack from standard input\n");
  99.     }
  100.     exit(0);
  101.     }
  102.  
  103.     while (argv[optind]) {
  104.     if (path) free(path);
  105.     path = 0;
  106.  
  107.     p = argv[optind];
  108.     if (p[1] == ':') p += 2;
  109.     if (q = strrchr(p, '\\')) p = q+1;
  110.     if (q = strrchr(p, '/')) p = q+1;
  111.  
  112.     if (p != argv[optind]) {
  113.         path = xmalloc(strlen(argv[optind])+20);
  114.         strcpy(path, argv[optind]);
  115.         p = path + (p - argv[optind]);
  116.     }
  117.  
  118.         FindCount=1;
  119.         Findhandle=1;
  120.     gotone = 0;
  121.     done = DosFindFirst(argv[optind], &Findhandle, 0, &ffblk,sizeof(ffblk),
  122.                             &FindCount,FIL_STANDARD);
  123.     while (!done) {
  124.         if (path) strcpy(p, ffblk.achName);
  125.         file = fopen(path ? path : ffblk.achName, "r");
  126.         if (!file) {
  127.         perror(path ? path : ffblk.achName);
  128.         }
  129.         else {
  130.             didchat = 0;
  131.         handleMessage(part_init(file), "text/plain", 0, extractText);
  132.         if (!didchat) {
  133.             fprintf(stdout, 
  134.                 "Did not find anything to unpack from %s\n",
  135.                 argv[optind]);
  136.         }
  137.         fclose(file);
  138.         }
  139.         gotone = 1;
  140.         done = DosFindNext(Findhandle,&ffblk,sizeof(ffblk),&FindCount);
  141.     }
  142.     if (!gotone) {
  143.         perror(argv[optind]);
  144.     }
  145.     optind++;
  146.     }
  147.     exit(0);
  148. }
  149.  
  150. usage() {
  151.     fprintf(stderr, "munpack version %s\n", MPACK_VERSION);
  152.     fprintf(stderr, "usage: munpack [-f] [-q] [-t] [-e] [-C directory] [files...]\n");
  153.     exit(1);
  154. }
  155.  
  156. warn(s)
  157. char *s;
  158. {
  159.     fprintf(stderr, "munpack: warning: %s\n", s);
  160. }
  161.  
  162. chat(s)
  163. char *s;
  164. {
  165.     didchat = 1;
  166.     if (!quiet) fprintf(stdout, "%s\n", s);
  167. }
  168.