"Fossies" - the Fresh Open Source Software Archive

Member "mpack-1.6/unixpk.c" (21 Jul 2003, 7123 Bytes) of package /linux/misc/old/mpack-1.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "unixpk.c" see the Fossies "Dox" file reference documentation.

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