home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / archivers / mpackppc-wos / src / os2pk.c < prev    next >
C/C++ Source or Header  |  1998-04-27  |  7KB  |  291 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 <io.h>
  27. #include <process.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include <errno.h>
  32. #include "version.h"
  33. #include "xmalloc.h"
  34.  
  35. #define MAXADDRESS 100
  36.  
  37. extern int optind;
  38. extern char *optarg;
  39.  
  40. main(argc, argv)
  41. int argc;
  42. char **argv;
  43. {
  44.     int opt;
  45.     char *fname = 0;
  46.     char *from = 0;
  47.     char *subject = 0;
  48.     char *descfname = 0;
  49.     long maxsize = 0;
  50.     char *outfname = 0;
  51.     char *newsgroups = 0;
  52.     char *ctype = 0;
  53.     char *headers = 0;
  54.     int i;
  55.     char *p;
  56.     char sbuf[1024];
  57.     char fnamebuf[4096];
  58.     int part;
  59.     FILE *infile;
  60.     FILE *descfile = 0;
  61.  
  62.     if ((p = getenv("SPLITSIZE")) && *p >= '0' && *p <= '9') {
  63.     maxsize = atoi(p);
  64.     }
  65.  
  66.     if (!(from = getenv("LOGNAME"))) from="postmaster";
  67.  
  68.     while ((opt = getopt(argc, argv, "s:d:m:c:o:n:f:")) != EOF) {
  69.     switch (opt) {
  70.     case 's':
  71.         subject = optarg;
  72.         break;
  73.  
  74.     case 'd':
  75.         descfname = optarg;
  76.         break;
  77.  
  78.     case 'm':
  79.         maxsize = atol(optarg);
  80.         break;
  81.  
  82.     case 'n':
  83.         newsgroups = optarg;
  84.         break;
  85.  
  86.     case 'c':
  87.         ctype = optarg;
  88.         break;
  89.  
  90.         case 'f':
  91.             from = optarg;
  92.             break;
  93.  
  94.     case 'o':
  95.         outfname = optarg;
  96.         break;
  97.  
  98.     default:
  99.         usage();
  100.  
  101.     }
  102.     }
  103.  
  104.     if (ctype) {
  105.     if (!cistrncmp(ctype, "text/", 5)) {
  106.         fprintf(stderr, "This program is not appropriate for encoding textual data\n");
  107.         exit(1);
  108.     }
  109.     if (cistrncmp(ctype, "application/", 12) && cistrncmp(ctype, "audio/", 6) &&
  110.         cistrncmp(ctype, "image/", 6) && cistrncmp(ctype, "video/", 6)) {
  111.         fprintf(stderr, "Content type must be subtype of application, audio, image, or video\n");
  112.         exit(1);
  113.     }
  114.     }
  115.  
  116.     if (optind == argc) {
  117.     fprintf(stderr, "An input file must be specified\n");
  118.     usage();
  119.     }
  120.     fname = argv[optind++];
  121.  
  122.     /* Must have exactly one of -o, -n, or destination addrs */
  123.     if (optind == argc) {
  124.     if (outfname && newsgroups) {
  125.         fprintf(stderr, "The -o and -n switches are mutually exclusive.\n");
  126.         usage();
  127.     }
  128.     if (!outfname && !newsgroups) {
  129.         fprintf(stderr, "Either an address or one of the -o or -n switches is required\n");
  130.         usage();
  131.     }
  132.     if (newsgroups) {
  133.         headers = xmalloc(strlen(newsgroups) + strlen(from) +50);
  134.         sprintf(headers, "Newsgroups: %s\nFrom: %s\n", newsgroups, from);
  135.     }
  136.     }
  137.     else {
  138.     if (outfname) {
  139.         fprintf(stderr, "The -o switch and addresses are mutually exclusive.\n");
  140.         usage();
  141.     }
  142.     if (newsgroups) {
  143.         fprintf(stderr, "The -n switch and addresses are mutually exclusive.\n");
  144.         usage();
  145.     }
  146.     headers = xmalloc(strlen(argv[optind]) + 50);
  147.     sprintf(headers, "To: %s", argv[optind]);
  148.     for (i = optind+1; i < argc; i++) {
  149.         headers = xrealloc(headers, strlen(headers)+strlen(argv[i]) + 25);
  150.         strcat(headers, ",\n\t");
  151.         strcat(headers, argv[i]);
  152.     }
  153.     strcat(headers, "\n");
  154.         strcat(headers, "From: ");
  155.         strcat(headers, from);
  156.         strcat(headers, "\n");
  157.     }
  158.  
  159.     if (!subject) {
  160.     fputs("Subject: ", stdout);
  161.     fflush(stdout);
  162.     if (!fgets(sbuf, sizeof(sbuf), stdin)) {
  163.         fprintf(stderr, "A subject is required\n");
  164.         usage();
  165.     }
  166.     if (p = strchr(sbuf, '\n')) *p = '\0';
  167.     subject = sbuf;
  168.     }    
  169.  
  170.     if (!outfname) {
  171.     if (getenv("TMP")) {
  172.         strcpy(fnamebuf, getenv("TMP"));
  173.     }
  174.     else {
  175.         strcpy(fnamebuf, "c:\\tmp");
  176.     }
  177.     strcat(fnamebuf, "\\mpack.000");
  178.     outfname = strsave(fnamebuf);
  179.     }
  180.  
  181.     infile = fopen(fname, "rb");
  182.     if (!infile) {
  183.     os_perror(fname);
  184.     exit(1);
  185.     }
  186.  
  187.     if (descfname) {
  188.     descfile = fopen(descfname, "r");
  189.     if (!descfile) {
  190.         os_perror(descfname);
  191.         exit(1);
  192.     }
  193.     }
  194.  
  195.     if (encode(infile, (FILE *)0, fname, descfile, subject, headers,
  196.            maxsize, ctype, outfname)) exit(1);
  197.  
  198.     if (optind < argc || newsgroups) {
  199.     for (part = 0;;part++) {
  200.         sprintf(fnamebuf, "%s.%02d", outfname, part);
  201.         infile = fopen(part ? fnamebuf : outfname, "r");
  202.         if (!infile) {
  203.         if (part) break;
  204.         continue;
  205.         }
  206.         if (newsgroups) {
  207.         inews(infile, newsgroups);
  208.         }
  209.         else {
  210.         sendmail(infile, argv, argc, optind, from);
  211.         }
  212.         fclose(infile);
  213.         remove(part ? fnamebuf : outfname);
  214.     }
  215.     }
  216.  
  217.     exit(0);
  218. }
  219.  
  220. usage()
  221. {
  222.     fprintf(stderr, "mpack version %s\n", MPACK_VERSION);
  223.     fprintf(stderr,
  224. "usage: mpack [-s subj] [-d file] [-m maxsize] [-c content-type] [-f address] file address...\n");
  225.     fprintf(stderr,
  226. "       mpack [-s subj] [-d file] [-m maxsize] [-c content-type] -o file file\n");
  227.     fprintf(stderr,
  228. "       mpack [-s subj] [-d file] [-m maxsize] [-c content-type] [-f address] -n groups file\n");
  229.     exit(1);
  230. }
  231.  
  232. sendmail(infile, addr, cnt, start, from)
  233. FILE *infile;
  234. char **addr;
  235. int cnt;
  236. int start;
  237. char *from;
  238. {
  239.     int status;
  240.     int pid;
  241.     int oldfh;
  242.     int i;
  243.     char *p;
  244.     char **argv;
  245.     
  246.     argv = (char **) xmalloc( (cnt-start+5)*sizeof(char *));
  247.     i = 0;
  248.  
  249.     if(!(p = getenv("SENDMAIL"))) p="sendmail.exe";
  250.  
  251.     argv[i++] = p;
  252.     argv[i++] = "-f";
  253.     argv[i++] = from;
  254. /*  argv[i++] = "-oi"; */
  255. /* Currently not supported by IBM sendmail, but ignore dot is default */ 
  256.  
  257.     while (cnt-start) argv[i++] = addr[start++];
  258.     argv[i] = NULL;
  259.     oldfh = dup(0);
  260.     dup2(fileno(infile), 0);
  261.     fclose(infile);
  262.     spawnvp(P_WAIT,p, argv);
  263.     dup2(oldfh,0);
  264.     close(oldfh);
  265.     free(argv);
  266. }
  267.  
  268. inews(infile)
  269. FILE *infile;
  270. {
  271.     int status;
  272.     int pid;
  273.     int oldfh;
  274.     char *p;
  275.  
  276.     if (!(p=getenv("NEWSPOST"))) p="inews.exe";
  277.  
  278.     oldfh = dup(0);
  279.     dup2(fileno(infile), 0);
  280.     fclose(infile);
  281.     spawnlp(P_WAIT,p , p, "-h", (char *)0);
  282.     dup2(oldfh,0);
  283.     close(oldfh);
  284. }
  285.  
  286. warn()
  287. {
  288.     abort();
  289. }
  290.  
  291.