home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / archivers / mpackppc / src / dosunpk.c < prev    next >
C/C++ Source or Header  |  1998-04-27  |  3KB  |  148 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. #include <stdio.h>
  26. #include <dos.h>
  27. #include <errno.h>
  28. #include "version.h"
  29. #include "part.h"
  30.  
  31. extern int optind;
  32. extern char *optarg;
  33.  
  34. extern int overwrite_files;
  35. extern int didchat;
  36. int quiet;
  37.  
  38. main(argc, argv)
  39. int argc;
  40. char **argv;
  41. {
  42.     int opt;
  43.     FILE *file;
  44.     char *p, *q, *path = 0;
  45.     int extractText = 0;
  46.     int done, gotone;
  47.     struct _find_t ffblk;
  48.     
  49.     while ((opt = getopt(argc, argv, "qftC:")) != EOF) {
  50.     switch (opt) {
  51.     case 'f':
  52.         overwrite_files = 1;
  53.         break;
  54.  
  55.     case 'q':
  56.         quiet = 1;
  57.         break;
  58.  
  59.     case 't':
  60.         extractText = 1;
  61.         break;
  62.  
  63.     case 'C':
  64.         if (chdir(optarg)) {
  65.         perror(optarg);
  66.         exit(1);
  67.         }
  68.         break;
  69.  
  70.     default:
  71.         usage();
  72.     }
  73.     }
  74.  
  75.     if (optind == argc) {
  76.     fprintf(stderr, "munpack: reading from standard input\n");
  77.     didchat = 0;
  78.     handleMessage(part_init(stdin), "text/plain", 0, extractText);
  79.     if (!didchat) {
  80.         fprintf(stdout,
  81.             "Did not find anything to unpack from standard input\n");
  82.     }
  83.     exit(0);
  84.     }
  85.  
  86.     while (argv[optind]) {
  87.     if (path) free(path);
  88.     path = 0;
  89.  
  90.     p = argv[optind];
  91.     if (p[1] == ':') p += 2;
  92.     if (q = strrchr(p, '\\')) p = q+1;
  93.     if (q = strrchr(p, '/')) p = q+1;
  94.  
  95.     if (p != argv[optind]) {
  96.         path = xmalloc(strlen(argv[optind])+20);
  97.         strcpy(path, argv[optind]);
  98.         p = path + (p - argv[optind]);
  99.     }
  100.  
  101.     gotone = 0;
  102.     done = _dos_findfirst(argv[optind], 0, &ffblk);
  103.     while (!done) {
  104.         if (path) strcpy(p, ffblk.name);
  105.         file = fopen(path ? path : ffblk.name, "r");
  106.         if (!file) {
  107.         perror(path ? path : ffblk.name);
  108.         }
  109.         else {
  110.         didchat = 0;
  111.         handleMessage(part_init(file), "text/plain", 0, extractText);
  112.         fclose(file);
  113.         if (!didchat) {
  114.             fprintf(stdout, 
  115.                 "Did not find anything to unpack from %s\n",
  116.                 argv[optind]);
  117.         }
  118.         }
  119.         gotone = 1;
  120.         done = _dos_findnext(&ffblk);
  121.     }
  122.     if (!gotone) {
  123.         perror(argv[optind]);
  124.     }
  125.     optind++;
  126.     }
  127.     exit(0);
  128. }
  129.  
  130. usage() {
  131.     fprintf(stderr, "munpack version %s\n", MPACK_VERSION);
  132.     fprintf(stderr, "usage: munpack [-f] [-q] [-C directory] [files...]\n");
  133.     exit(1);
  134. }
  135.  
  136. warn(s)
  137. char *s;
  138. {
  139.     fprintf(stderr, "munpack: warning: %s\n", s);
  140. }
  141.  
  142. chat(s)
  143. char *s;
  144. {
  145.     didchat = 1;
  146.     if (!quiet) fprintf(stdout, "%s\n", s);
  147. }
  148.