Back to home page

MITgcm

 
 

    


File indexing completed on 2018-03-02 18:45:00 UTC

view on githubraw file Latest commit 351d1cc0 on 2008-03-12 20:38:49 UTC
ec6cf3b09d Ed H*0001 /* (C) Copyright 1993,1994 by Carnegie Mellon University
                0002  * All Rights Reserved.
                0003  *
                0004  * Permission to use, copy, modify, distribute, and sell this software
                0005  * and its documentation for any purpose is hereby granted without
                0006  * fee, provided that the above copyright notice appear in all copies
                0007  * and that both that copyright notice and this permission notice
                0008  * appear in supporting documentation, and that the name of Carnegie
                0009  * Mellon University not be used in advertising or publicity
                0010  * pertaining to distribution of the software without specific,
                0011  * written prior permission.  Carnegie Mellon University makes no
                0012  * representations about the suitability of this software for any
                0013  * purpose.  It is provided "as is" without express or implied
                0014  * warranty.
                0015  *
                0016  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
                0017  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
                0018  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
                0019  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                0020  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
                0021  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                0022  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
                0023  * SOFTWARE.
                0024  */
                0025 #include <stdio.h>
e768bd1221 Jean*0026 #include <stdlib.h>
ec6cf3b09d Ed H*0027 #include <string.h>
                0028 #include <errno.h>
351d1cc027 Jean*0029 #ifdef HAVE_GETOPT_H
e768bd1221 Jean*0030 #include <getopt.h>
351d1cc027 Jean*0031 #endif
e768bd1221 Jean*0032 #include <sys/types.h>
                0033 #include <sys/wait.h>
                0034 #include <unistd.h>
ec6cf3b09d Ed H*0035 #include "common.h"
                0036 #include "version.h"
                0037 #include "xmalloc.h"
                0038 
                0039 #define MAXADDRESS 100
                0040 
e768bd1221 Jean*0041 extern int attachment;
ec6cf3b09d Ed H*0042 
                0043 void usage(void);
                0044 void sendmail(FILE *infile, char **addr, int start);
                0045 void inews(FILE *infile);
e768bd1221 Jean*0046 void os_perror(char *str);
                0047 int encode(FILE *infile, FILE *applefile, char *fname, FILE *descfile,
                0048        char *subject, char *headers, long int maxsize,
                0049        char *typeoverride, char *outfname);
ec6cf3b09d Ed H*0050 
                0051 int main(int argc, char **argv)
                0052 {
                0053     int opt;
                0054     char *fname = 0;
                0055     char *subject = 0;
                0056     char *descfname = 0;
                0057     long maxsize = 0;
                0058     char *outfname = 0;
                0059     char *newsgroups = 0;
                0060     char *ctype = 0;
                0061     char *headers = 0;
                0062     int i;
                0063     char *p;
                0064     char sbuf[1024];
                0065     char fnamebuf[4096];
                0066     int part;
                0067     FILE *infile;
                0068     FILE *descfile = 0;
                0069 
                0070     if ((p = getenv("SPLITSIZE")) && *p >= '0' && *p <= '9') {
                0071     maxsize = atoi(p);
                0072     }
                0073 
e768bd1221 Jean*0074     while ((opt = getopt(argc, argv, "as:d:m:c:o:n:")) != EOF) {
ec6cf3b09d Ed H*0075     switch (opt) {
                0076     case 's':
                0077         subject = optarg;
                0078         break;
                0079 
                0080     case 'd':
                0081         descfname = optarg;
                0082         break;
                0083 
                0084     case 'm':
                0085         maxsize = atoi(optarg);
                0086         break;
                0087 
                0088     case 'c':
                0089         ctype = optarg;
                0090         break;
                0091 
                0092     case 'o':
                0093         outfname = optarg;
                0094         break;
                0095 
                0096     case 'n':
                0097         newsgroups = optarg;
                0098         break;
                0099 
e768bd1221 Jean*0100     case 'a':
                0101         attachment = 1;
                0102         break;
                0103 
ec6cf3b09d Ed H*0104     default:
                0105         usage();
                0106 
                0107     }
                0108     }
                0109 
                0110     if (ctype) {
                0111     if (!strncasecmp(ctype, "text/", 5)) {
                0112         fprintf(stderr, "This program is not appropriate for encoding textual data\n");
                0113         exit(1);
                0114     }
                0115     if (strncasecmp(ctype, "application/", 12) && strncasecmp(ctype, "audio/", 6) &&
                0116         strncasecmp(ctype, "image/", 6) && strncasecmp(ctype, "video/", 6)) {
                0117         fprintf(stderr, "Content type must be subtype of application, audio, image, or video\n");
                0118         exit(1);
                0119     }
                0120     }
                0121 
                0122     if (optind == argc) {
                0123     fprintf(stderr, "An input file must be specified\n");
                0124     usage();
                0125     }
                0126     fname = argv[optind++];
                0127 
                0128     /* Must have exactly one of -o, -n, or destination addrs */
                0129     if (optind == argc) {
                0130     if (outfname && newsgroups) {
                0131         fprintf(stderr, "The -o and -n switches are mutually exclusive.\n");
                0132         usage();
                0133     }
                0134     if (!outfname && !newsgroups) {
                0135         fprintf(stderr, "Either an address or one of the -o or -n switches is required\n");
                0136         usage();
                0137     }
                0138     if (newsgroups) {
                0139         headers = xmalloc(strlen(newsgroups) + 25);
                0140         sprintf(headers, "Newsgroups: %s\n", newsgroups);
                0141     }
                0142     }
                0143     else {
                0144     if (outfname) {
                0145         fprintf(stderr, "The -o switch and addresses are mutually exclusive.\n");
                0146         usage();
                0147     }
                0148     if (newsgroups) {
                0149         fprintf(stderr, "The -n switch and addresses are mutually exclusive.\n");
                0150         usage();
                0151     }
                0152     headers = xmalloc(strlen(argv[optind]) + 25);
                0153     sprintf(headers, "To: %s", argv[optind]);
                0154     for (i = optind+1; i < argc; i++) {
                0155         headers = xrealloc(headers, strlen(headers)+strlen(argv[i]) + 25);
                0156         strcat(headers, ",\n\t");
                0157         strcat(headers, argv[i]);
                0158     }
                0159     strcat(headers, "\n");
                0160     }
                0161 
                0162     if (!subject) {
                0163     fputs("Subject: ", stdout);
                0164     fflush(stdout);
                0165     if (!fgets(sbuf, sizeof(sbuf), stdin)) {
                0166         fprintf(stderr, "A subject is required\n");
                0167         usage();
                0168     }
e768bd1221 Jean*0169     if ((p = strchr(sbuf, '\n'))) *p = '\0';
ec6cf3b09d Ed H*0170     subject = sbuf;
                0171     }   
                0172 
                0173     if (!outfname) {
                0174     if (getenv("TMPDIR")) {
                0175         strcpy(fnamebuf, getenv("TMPDIR"));
                0176     }
                0177     else {
e768bd1221 Jean*0178         strcpy(fnamebuf, "/var/tmp");
ec6cf3b09d Ed H*0179     }
                0180     strcat(fnamebuf, "/mpackXXXXXX");
                0181     mktemp(fnamebuf);
                0182     outfname = strsave(fnamebuf);
                0183     }
                0184 
                0185     infile = fopen(fname, "r");
                0186     if (!infile) {
                0187     os_perror(fname);
                0188     exit(1);
                0189     }
                0190 
                0191     if (descfname) {
                0192     descfile = fopen(descfname, "r");
                0193     if (!descfile) {
                0194         os_perror(descfname);
                0195         exit(1);
                0196     }
                0197     }
                0198 
                0199     if (encode(infile, (FILE *)0, fname, descfile, subject, headers,
                0200            maxsize, ctype, outfname)) exit(1);
                0201 
                0202     if (optind < argc || newsgroups) {
                0203     for (part = 0;;part++) {
                0204         sprintf(fnamebuf, "%s.%02d", outfname, part);
                0205         infile = fopen(part ? fnamebuf : outfname, "r");
                0206         if (!infile) {
                0207         if (part) break;
                0208         continue;
                0209         }
                0210         if (newsgroups) {
                0211         inews(infile);
                0212         }
                0213         else {
                0214         sendmail(infile, argv, optind);
                0215         }
                0216         fclose(infile);
                0217         remove(part ? fnamebuf : outfname);
                0218     }
                0219     }
                0220 
                0221     exit(0);
                0222 }
                0223 
                0224 void usage(void)
                0225 {
                0226     fprintf(stderr, "mpack version %s\n", MPACK_VERSION);
                0227     fprintf(stderr, 
                0228 "usage: mpack [-s subj] [-d file] [-m maxsize] [-c content-type] file address...\n");
                0229     fprintf(stderr, 
                0230 "       mpack [-s subj] [-d file] [-m maxsize] [-c content-type] -o file file\n");
                0231     fprintf(stderr, 
                0232 "       mpack [-s subj] [-d file] [-m maxsize] [-c content-type] -n groups file\n");
                0233     exit(1);
                0234 }
                0235 
                0236 void sendmail(FILE *infile, char **addr, int start)
                0237 {
                0238     int status;
                0239     int pid;
                0240 
                0241     if (start < 2) abort();
                0242 
                0243 #ifdef SCO
                0244     addr[--start] = "execmail";
                0245 #else
                0246     addr[--start] = "-oi";
                0247     addr[--start] = "sendmail";
                0248 #endif
                0249 
                0250     do {
                0251     pid = fork();
                0252     } while (pid == -1 && errno == EAGAIN);
                0253     
                0254     if (pid == -1) {
                0255     perror("fork");
                0256     return;
                0257     }
                0258     if (pid != 0) {
                0259     while (pid != wait(&status));
                0260     return;
                0261     }
                0262 
                0263     dup2(fileno(infile), 0);
                0264     fclose(infile);
                0265 #ifdef SCO
                0266     execv("/usr/lib/mail/execmail", addr+start);
                0267 #else
                0268     execv("/usr/lib/sendmail", addr+start);
                0269     execv("/usr/sbin/sendmail", addr+start);
                0270 #endif
                0271     perror("execv");
                0272     _exit(1);
                0273 }
                0274 
                0275 void inews(FILE *infile)
                0276 {
                0277     int status;
                0278     int pid;
                0279 
                0280     do {
                0281     pid = fork();
                0282     } while (pid == -1 && errno == EAGAIN);
                0283     
                0284     if (pid == -1) {
                0285     perror("fork");
                0286     return;
                0287     }
                0288     if (pid != 0) {
                0289     while (pid != wait(&status));
                0290     return;
                0291     }
                0292 
                0293     dup2(fileno(infile), 0);
                0294     fclose(infile);
                0295     execlp("inews", "inews", "-h", "-S", (char *)0);
                0296     execl("/usr/local/news/inews", "inews", "-h", "-S", (char *)0);
                0297     execl("/usr/local/lib/news/inews", "inews", "-h", "-S", (char *)0);
                0298     execl("/etc/inews", "inews", "-h", "-S", (char *)0);
                0299     execl("/usr/etc/inews", "inews", "-h", "-S", (char *)0);
                0300     execl("/usr/news/inews", "inews", "-h", "-S", (char *)0);
                0301     execl("/usr/news/bin/inews", "inews", "-h", "-S", (char *)0);
                0302     perror("execl");
                0303     _exit(1);
                0304 }
                0305 
                0306 void warn(void)
                0307 {
                0308     abort();
                0309 }