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

  1. /* (C) Copyright 1993,1994 by Carnegie Mellon University
  2.  * All Rights Reserved.
  3.  *
  4. ww * 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 <string.h>
  27. #include <errno.h>
  28. #include "common.h"
  29. #include "version.h"
  30. #include "xmalloc.h"
  31.  
  32. #define MAXADDRESS 100
  33.  
  34. extern char *getenv();
  35.  
  36. extern int errno;
  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 *subject = 0;
  47.     char *descfname = 0;
  48.     long maxsize = 0;
  49.     char *outfname = 0;
  50.     char *newsgroups = 0;
  51.     char *ctype = 0;
  52.     char *headers = 0;
  53.     int i;
  54.     char *p;
  55.     char sbuf[1024];
  56.     char fnamebuf[4096];
  57.     int part;
  58.     FILE *infile;
  59.     FILE *descfile = 0;
  60.  
  61.     if ((p = getenv("SPLITSIZE")) && *p >= '0' && *p <= '9') {
  62.     maxsize = atoi(p);
  63.     }
  64.  
  65.     while ((opt = getopt(argc, argv, "s:d:m:c:o:n:")) != EOF) {
  66.     switch (opt) {
  67.     case 's':
  68.         subject = optarg;
  69.         break;
  70.  
  71.     case 'd':
  72.         descfname = optarg;
  73.         break;
  74.  
  75.     case 'm':
  76.         maxsize = atoi(optarg);
  77.         break;
  78.  
  79.     case 'c':
  80.         ctype = optarg;
  81.         break;
  82.  
  83.     case 'o':
  84.         outfname = optarg;
  85.         break;
  86.  
  87.     case 'n':
  88.         newsgroups = optarg;
  89.         break;
  90.  
  91.     default:
  92.         usage();
  93.  
  94.     }
  95.     }
  96.  
  97.     if (ctype) {
  98.     if (!cistrncmp(ctype, "text/", 5)) {
  99.         fprintf(stderr, "This program is not appropriate for encoding textual data\n");
  100.         exit(1);
  101.     }
  102.     if (cistrncmp(ctype, "application/", 12) && cistrncmp(ctype, "audio/", 6) &&
  103.         cistrncmp(ctype, "image/", 6) && cistrncmp(ctype, "video/", 6)) {
  104.         fprintf(stderr, "Content type must be subtype of application, audio, image, or video\n");
  105.         exit(1);
  106.     }
  107.     }
  108.  
  109.     if (optind == argc) {
  110.     fprintf(stderr, "An input file must be specified\n");
  111.     usage();
  112.     }
  113.     fname = argv[optind++];
  114.  
  115.     /* Must have exactly one of -o, -n, or destination addrs */
  116.     if (optind == argc) {
  117.     if (outfname && newsgroups) {
  118.         fprintf(stderr, "The -o and -n switches are mutually exclusive.\n");
  119.         usage();
  120.     }
  121.     if (!outfname && !newsgroups) {
  122.         fprintf(stderr, "Either an address or one of the -o or -n switches is required\n");
  123.         usage();
  124.     }
  125.     if (newsgroups) {
  126.         headers = xmalloc(strlen(newsgroups) + 25);
  127.         sprintf(headers, "Newsgroups: %s\n", newsgroups);
  128.     }
  129.     }
  130.     else {
  131.     if (outfname) {
  132.         fprintf(stderr, "The -o switch and addresses are mutually exclusive.\n");
  133.         usage();
  134.     }
  135.     if (newsgroups) {
  136.         fprintf(stderr, "The -n switch and addresses are mutually exclusive.\n");
  137.         usage();
  138.     }
  139.     headers = xmalloc(strlen(argv[optind]) + 25);
  140.     sprintf(headers, "To: %s", argv[optind]);
  141.     for (i = optind+1; i < argc; i++) {
  142.         headers = xrealloc(headers, strlen(headers)+strlen(argv[i]) + 25);
  143.         strcat(headers, ",\n\t");
  144.         strcat(headers, argv[i]);
  145.     }
  146.     strcat(headers, "\n");
  147.     }
  148.  
  149.     if (!subject) {
  150.     fputs("Subject: ", stdout);
  151.     fflush(stdout);
  152.     if (!fgets(sbuf, sizeof(sbuf), stdin)) {
  153.         fprintf(stderr, "A subject is required\n");
  154.         usage();
  155.     }
  156.     if (p = strchr(sbuf, '\n')) *p = '\0';
  157.     subject = sbuf;
  158.     }    
  159.  
  160.     if (!outfname) {
  161.     if (getenv("TMPDIR")) {
  162.         strcpy(fnamebuf, getenv("TMPDIR"));
  163.     }
  164.     else {
  165.         strcpy(fnamebuf, "/tmp");
  166.     }
  167.     strcat(fnamebuf, "/mpackXXXXXX");
  168.     mktemp(fnamebuf);
  169.     outfname = strsave(fnamebuf);
  170.     }
  171.  
  172.     infile = fopen(fname, "r");
  173.     if (!infile) {
  174.     os_perror(fname);
  175.     exit(1);
  176.     }
  177.  
  178.     if (descfname) {
  179.     descfile = fopen(descfname, "r");
  180.     if (!descfile) {
  181.         os_perror(descfname);
  182.         exit(1);
  183.     }
  184.     }
  185.  
  186.     if (encode(infile, (FILE *)0, fname, descfile, subject, headers,
  187.            maxsize, ctype, outfname)) exit(1);
  188.  
  189.     if (optind < argc || newsgroups) {
  190.     for (part = 0;;part++) {
  191.         sprintf(fnamebuf, "%s.%02d", outfname, part);
  192.         infile = fopen(part ? fnamebuf : outfname, "r");
  193.         if (!infile) {
  194.         if (part) break;
  195.         continue;
  196.         }
  197.         if (newsgroups) {
  198.         inews(infile, newsgroups);
  199.         }
  200.         else {
  201.         sendmail(infile, argv, optind);
  202.         }
  203.         fclose(infile);
  204.         remove(part ? fnamebuf : outfname);
  205.     }
  206.     }
  207.  
  208.     exit(0);
  209. }
  210.  
  211. usage()
  212. {
  213.     fprintf(stderr, "mpack version %s\n", MPACK_VERSION);
  214.     fprintf(stderr, 
  215. "usage: mpack [-s subj] [-d file] [-m maxsize] [-c content-type] file address...\n");
  216.     fprintf(stderr, 
  217. "       mpack [-s subj] [-d file] [-m maxsize] [-c content-type] -o file file\n");
  218.     fprintf(stderr, 
  219. "       mpack [-s subj] [-d file] [-m maxsize] [-c content-type] -n groups file\n");
  220.     exit(1);
  221. }
  222.  
  223. sendmail(infile, addr, start)
  224. FILE *infile;
  225. char **addr;
  226. int start;
  227. {
  228.     int status;
  229.     int pid;
  230.  
  231.     if (start < 2) abort();
  232.  
  233. #ifdef SCO
  234.     addr[--start] = "execmail";
  235. #else
  236.     addr[--start] = "-oi";
  237.     addr[--start] = "sendmail";
  238. #endif
  239.  
  240.     do {
  241.     pid = fork();
  242.     } while (pid == -1 && errno == EAGAIN);
  243.     
  244.     if (pid == -1) {
  245.     perror("fork");
  246.     return;
  247.     }
  248.     if (pid != 0) {
  249.     while (pid != wait(&status));
  250.     return;
  251.     }
  252.  
  253.     dup2(fileno(infile), 0);
  254.     fclose(infile);
  255. #ifdef SCO
  256.     execv("/usr/lib/mail/execmail", addr+start);
  257. #else
  258.     execv("/usr/lib/sendmail", addr+start);
  259.     execv("/usr/sbin/sendmail", addr+start);
  260. #endif
  261.     perror("execv");
  262.     _exit(1);
  263. }
  264.  
  265. inews(infile)
  266. FILE *infile;
  267. {
  268.     int status;
  269.     int pid;
  270.  
  271.     do {
  272.     pid = fork();
  273.     } while (pid == -1 && errno == EAGAIN);
  274.     
  275.     if (pid == -1) {
  276.     perror("fork");
  277.     return;
  278.     }
  279.     if (pid != 0) {
  280.     while (pid != wait(&status));
  281.     return;
  282.     }
  283.  
  284.     dup2(fileno(infile), 0);
  285.     fclose(infile);
  286.     execlp("inews", "inews", "-h", "-S", (char *)0);
  287.     execl("/usr/local/news/inews", "inews", "-h", "-S", (char *)0);
  288.     execl("/usr/local/lib/news/inews", "inews", "-h", "-S", (char *)0);
  289.     execl("/etc/inews", "inews", "-h", "-S", (char *)0);
  290.     execl("/usr/etc/inews", "inews", "-h", "-S", (char *)0);
  291.     execl("/usr/news/inews", "inews", "-h", "-S", (char *)0);
  292.     execl("/usr/news/bin/inews", "inews", "-h", "-S", (char *)0);
  293.     perror("execl");
  294.     _exit(1);
  295. }
  296.  
  297. warn()
  298. {
  299.     abort();
  300. }
  301.