home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / MPack1_2_1.lha / MPack / src / unixpk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-05  |  6.4 KB  |  280 lines

  1. /* (C) Copyright 1993 by John G. Myers
  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 John G.
  9.  * Myers not be used in advertising or publicity pertaining to
  10.  * distribution of the software without specific, written prior
  11.  * permission.  John G. Myers makes no representations about the
  12.  * suitability of this software for any purpose.  It is provided "as
  13.  * is" without express or implied warranty.
  14.  *
  15.  * JOHN G. MYERS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17.  * FITNESS, IN NO EVENT SHALL JOHN G. MYERS BE LIABLE FOR ANY SPECIAL,
  18.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  19.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  21.  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  */
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include "common.h"
  27. #include "version.h"
  28. #include "xmalloc.h"
  29.  
  30. #define MAXADDRESS 100
  31.  
  32. extern char *getenv();
  33.  
  34. extern int errno;
  35. extern int optind;
  36. extern char *optarg;
  37.  
  38. main(argc, argv)
  39. int argc;
  40. char **argv;
  41. {
  42.     int opt;
  43.     char *fname = 0;
  44.     char *subject = 0;
  45.     char *descfname = 0;
  46.     long maxsize = 0;
  47.     char *outfname = 0;
  48.     char *newsgroups = 0;
  49.     char *ctype = 0;
  50.     char *headers = 0;
  51.     int i;
  52.     char *p;
  53.     char sbuf[1024];
  54.     char fnamebuf[4096];
  55.     int part;
  56.     FILE *infile;
  57.  
  58.     if ((p = getenv("SPLITSIZE")) && *p >= '0' && *p <= '9') {
  59.     maxsize = atoi(p);
  60.     }
  61.  
  62.     while ((opt = getopt(argc, argv, "s:d:m:c:o:n:")) != EOF) {
  63.     switch (opt) {
  64.     case 's':
  65.         subject = optarg;
  66.         break;
  67.  
  68.     case 'd':
  69.         descfname = optarg;
  70.         break;
  71.  
  72.     case 'm':
  73.         maxsize = atoi(optarg);
  74.         break;
  75.  
  76.     case 'c':
  77.         ctype = optarg;
  78.         break;
  79.  
  80.     case 'o':
  81.         outfname = optarg;
  82.         break;
  83.  
  84.     case 'n':
  85.         newsgroups = optarg;
  86.         break;
  87.  
  88.     default:
  89.         usage();
  90.  
  91.     }
  92.     }
  93.  
  94.     if (ctype) {
  95.     if (!cistrncmp(ctype, "text/", 5)) {
  96.         fprintf(stderr, "This program is not appropriate for encoding textual data\n");
  97.         exit(1);
  98.     }
  99.     if (cistrncmp(ctype, "application/", 12) && cistrncmp(ctype, "audio/", 6) &&
  100.         cistrncmp(ctype, "image/", 6) && cistrncmp(ctype, "video/", 6)) {
  101.         fprintf(stderr, "Content type must be subtype of application, audio, image, or video\n");
  102.         exit(1);
  103.     }
  104.     }
  105.  
  106.     if (optind == argc) {
  107.     fprintf(stderr, "An input file must be specified\n");
  108.     usage();
  109.     }
  110.     fname = argv[optind++];
  111.  
  112.     /* Must have exactly one of -o, -n, or destination addrs */
  113.     if (optind == argc) {
  114.     if (outfname && newsgroups) {
  115.         fprintf(stderr, "The -o and -n switches are mutually exclusive.\n");
  116.         usage();
  117.     }
  118.     if (!outfname && !newsgroups) {
  119.         fprintf(stderr, "Either an address or one of the -o or -n switches is required\n");
  120.         usage();
  121.     }
  122.     if (newsgroups) {
  123.         headers = xmalloc(strlen(newsgroups) + 25);
  124.         sprintf(headers, "Newsgroups: %s\n", newsgroups);
  125.     }
  126.     }
  127.     else {
  128.     if (outfname) {
  129.         fprintf(stderr, "The -o switch and addresses are mutually exclusive.\n");
  130.         usage();
  131.     }
  132.     if (newsgroups) {
  133.         fprintf(stderr, "The -n switch and addresses are mutually exclusive.\n");
  134.         usage();
  135.     }
  136.     headers = xmalloc(strlen(argv[optind]) + 25);
  137.     sprintf(headers, "To: %s", argv[optind]);
  138.     for (i = optind+1; i < argc; i++) {
  139.         headers = xrealloc(headers, strlen(headers)+strlen(argv[i]) + 25);
  140.         strcat(headers, ",\n\t");
  141.         strcat(headers, argv[i]);
  142.     }
  143.     strcat(headers, "\n");
  144.     }
  145.  
  146.     if (!subject) {
  147.     fputs("Subject: ", stdout);
  148.     fflush(stdout);
  149.     if (!fgets(sbuf, sizeof(sbuf), stdin)) {
  150.         fprintf(stderr, "A subject is required\n");
  151.         usage();
  152.     }
  153.     if (p = strchr(sbuf, '\n')) *p = '\0';
  154.     subject = sbuf;
  155.     }    
  156.  
  157.     if (!outfname) {
  158.     if (getenv("TMPDIR")) {
  159.         strcpy(fnamebuf, getenv("TMPDIR"));
  160.     }
  161.     else {
  162.         strcpy(fnamebuf, "/tmp");
  163.     }
  164.     strcat(fnamebuf, "/mpackXXXXXX");
  165.     mktemp(fnamebuf);
  166.     outfname = strsave(fnamebuf);
  167.     }
  168.  
  169.     if (encode(fname, descfname, subject, headers, maxsize, ctype, outfname)) exit(1);
  170.  
  171.     if (optind < argc || newsgroups) {
  172.     for (part = 0;;part++) {
  173.         sprintf(fnamebuf, "%s.%02d", outfname, part);
  174.         infile = fopen(part ? fnamebuf : outfname, "r");
  175.         if (!infile) {
  176.         if (part) break;
  177.         continue;
  178.         }
  179.         if (newsgroups) {
  180.         inews(infile, newsgroups);
  181.         }
  182.         else {
  183.         sendmail(infile, argv, optind);
  184.         }
  185.         fclose(infile);
  186.         remove(part ? fnamebuf : outfname);
  187.     }
  188.     }
  189.  
  190.     exit(0);
  191. }
  192.  
  193. usage()
  194. {
  195.     fprintf(stderr, "mpack version %s\n", MPACK_VERSION);
  196.     fprintf(stderr, 
  197. "usage: mpack [-s subj] [-d file] [-m maxsize] [-c content-type] file address...\n");
  198.     fprintf(stderr, 
  199. "       mpack [-s subj] [-d file] [-m maxsize] [-c content-type] -o file file\n");
  200.     fprintf(stderr, 
  201. "       mpack [-s subj] [-d file] [-m maxsize] [-c content-type] -n groups file\n");
  202.     exit(1);
  203. }
  204.  
  205. sendmail(infile, addr, start)
  206. FILE *infile;
  207. char **addr;
  208. int start;
  209. {
  210.     int status;
  211.     int pid, i = 0, j = 0;
  212.  
  213.     if (start < 2) abort();
  214.  
  215. #ifdef SCO
  216.     addr[--start] = "execmail";
  217. #else
  218.     addr[--start] = "-oi";
  219.     addr[--start] = "sendmail";
  220. #endif
  221.  
  222.     do {
  223.     pid = fork();
  224.     } while (pid == -1 && errno == EAGAIN);
  225.     
  226.     if (pid == -1) {
  227.     perror("fork");
  228.     return;
  229.     }
  230.     if (pid != 0) {
  231.     while (pid != wait(&status));
  232.     return;
  233.     }
  234.  
  235.     dup2(fileno(infile), 0);
  236.     fclose(infile);
  237. #ifdef SCO
  238.     execv("/usr/lib/mail/execmail", addr+start);
  239. #else
  240.     execv("/usr/lib/sendmail", addr+start);
  241. #endif
  242.     perror("execv");
  243.     _exit(1);
  244. }
  245.  
  246. inews(infile)
  247. FILE *infile;
  248. {
  249.     int status;
  250.     int pid;
  251.  
  252.     do {
  253.     pid = fork();
  254.     } while (pid == -1 && errno == EAGAIN);
  255.     
  256.     if (pid == -1) {
  257.     perror("fork");
  258.     return;
  259.     }
  260.     if (pid != 0) {
  261.     while (pid != wait(&status));
  262.     return;
  263.     }
  264.  
  265.     dup2(fileno(infile), 0);
  266.     fclose(infile);
  267.     execlp("inews", "inews", "-h", "-S", (char *)0);
  268.     execl("/usr/local/news/inews", "inews", "-h", "-S", (char *)0);
  269.     execl("/etc/inews", "inews", "-h", "-S", (char *)0);
  270.     execl("/usr/etc/inews", "inews", "-h", "-S", (char *)0);
  271.     execl("/usr/news/inews", "inews", "-h", "-S", (char *)0);
  272.     perror("execl");
  273.     _exit(1);
  274. }
  275.  
  276. warn()
  277. {
  278.     abort();
  279. }
  280.