/[MITgcm]/MITgcm/tools/mpack-1.6/unixpk.c
ViewVC logotype

Contents of /MITgcm/tools/mpack-1.6/unixpk.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download)
Wed Mar 12 20:38:49 2008 UTC (16 years, 1 month ago) by jmc
Branch: MAIN
CVS Tags: checkpoint64y, checkpoint64x, checkpoint64z, checkpoint64q, checkpoint64p, checkpoint64s, checkpoint64r, checkpoint64u, checkpoint64t, checkpoint64w, checkpoint64v, checkpoint64i, checkpoint64h, checkpoint64k, checkpoint64j, checkpoint64m, checkpoint64l, checkpoint64o, checkpoint64n, checkpoint64a, checkpoint64c, checkpoint64b, checkpoint64e, checkpoint64d, checkpoint64g, checkpoint64f, checkpoint63p, checkpoint63q, checkpoint63r, checkpoint63s, checkpoint63l, checkpoint63m, checkpoint63n, checkpoint63o, checkpoint63h, checkpoint63i, checkpoint63j, checkpoint63k, checkpoint63d, checkpoint63e, checkpoint63f, checkpoint63g, checkpoint63a, checkpoint63b, checkpoint63c, checkpoint64, checkpoint65, checkpoint60, checkpoint61, checkpoint62, checkpoint63, checkpoint66g, checkpoint66f, checkpoint66e, checkpoint66d, checkpoint66c, checkpoint66b, checkpoint66a, checkpoint66o, checkpoint66n, checkpoint66m, checkpoint66l, checkpoint66k, checkpoint66j, checkpoint66i, checkpoint66h, checkpoint65z, checkpoint65x, checkpoint65y, checkpoint65r, checkpoint65s, checkpoint65p, checkpoint65q, checkpoint65v, checkpoint65w, checkpoint65t, checkpoint65u, checkpoint65j, checkpoint65k, checkpoint65h, checkpoint65i, checkpoint65n, checkpoint65o, checkpoint65l, checkpoint65m, checkpoint65b, checkpoint65c, checkpoint65a, checkpoint65f, checkpoint65g, checkpoint65d, checkpoint65e, checkpoint59q, checkpoint59p, checkpoint59r, checkpoint62c, checkpoint62b, checkpoint62a, checkpoint62g, checkpoint62f, checkpoint62e, checkpoint62d, checkpoint62k, checkpoint62j, checkpoint62i, checkpoint62h, checkpoint62o, checkpoint62n, checkpoint62m, checkpoint62l, checkpoint62s, checkpoint62r, checkpoint62q, checkpoint62p, checkpoint62w, checkpoint62v, checkpoint62u, checkpoint62t, checkpoint62z, checkpoint62y, checkpoint62x, checkpoint61f, checkpoint61g, checkpoint61d, checkpoint61e, checkpoint61b, checkpoint61c, checkpoint61a, checkpoint61n, checkpoint61o, checkpoint61l, checkpoint61m, checkpoint61j, checkpoint61k, checkpoint61h, checkpoint61i, checkpoint61v, checkpoint61w, checkpoint61t, checkpoint61u, checkpoint61r, checkpoint61s, checkpoint61p, checkpoint61q, checkpoint61z, checkpoint61x, checkpoint61y, HEAD
Changes since 1.4: +2 -0 lines
File MIME type: text/plain
test for header "getopt.h" and just do not include it if not found.

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

  ViewVC Help
Powered by ViewVC 1.1.22