3 * viamail.c -- send multiple files in a MIME message
10 #include <h/signals.h>
14 #include <zotnet/mts/mts.h>
17 #include <h/mhparse.h>
19 #ifdef HAVE_SYS_WAIT_H
20 # include <sys/wait.h>
23 static struct swit switches[] = {
27 { "from mailpath", 0 },
29 { "subject subject", 0 },
31 { "parameters arguments", 0 },
33 { "description text", 0 },
35 { "comment text", 0 },
37 { "delay seconds", 0 },
56 int ebcdicsw = 0; /* hack for linking purposes */
59 void set_endian (void);
62 int writeBase64aux (FILE *, FILE *);
67 static int via_mail (char *, char *, char *, char *, char *, int, char *);
71 main (int argc, char **argv)
74 char *f1 = NULL, *f2 = NULL, *f3 = NULL;
75 char *f4 = NULL, *f5 = NULL, *f7 = NULL;
76 char *cp, buf[BUFSIZ];
77 char **argp, **arguments;
80 setlocale(LC_ALL, "");
82 invo_name = r1bindex (argv[0], '/');
84 /* foil search of user profile/context */
85 if (context_foil (NULL) == -1)
88 arguments = getarguments (invo_name, argc, argv, 0);
91 while ((cp = *argp++)) {
93 switch (smatch (++cp, switches)) {
95 ambigsw (cp, switches);
98 adios (NULL, "-%s unknown", cp);
101 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
102 print_help (buf, switches, 1);
105 print_version(invo_name);
110 adios (NULL, "missing argument to %s", argp[-2]);
114 adios (NULL, "missing argument to %s", argp[-2]);
118 adios (NULL, "missing argument to %s", argp[-2]);
122 adios (NULL, "missing argument to %s", argp[-2]);
126 adios (NULL, "missing argument to %s", argp[-2]);
129 if (!(cp = *argp++) || *cp == '-')
130 adios (NULL, "missing argument to %s", argp[-2]);
133 * If there is an error, just reset the delay parameter
134 * to -1. We will set a default delay later.
136 if (sscanf (cp, "%d", &delay) != 1)
141 adios (NULL, "missing argument to %s", argp[-2]);
161 adios (NULL, "missing -viamail \"mailpath\" switch");
163 via_mail (f1, f2, f3, f4, f5, delay, f7);
164 return 0; /* dead code to satisfy the compiler */
173 via_mail (char *mailsw, char *subjsw, char *parmsw, char *descsw,
174 char *cmntsw, int delay, char *fromsw)
176 int status, vecp = 1;
182 umask (~m_gmprot ());
184 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
185 if ((fp = fopen (tmpfil, "w+")) == NULL)
186 adios (tmpfil, "unable to open for writing");
187 chmod (tmpfil, 0600);
189 if (!strchr(mailsw, '@'))
190 mailsw = concat (mailsw, "@", LocalName (), NULL);
191 fprintf (fp, "To: %s\n", mailsw);
194 fprintf (fp, "Subject: %s\n", subjsw);
197 if (!strchr(fromsw, '@'))
198 fromsw = concat (fromsw, "@", LocalName (), NULL);
199 fprintf (fp, "From: %s\n", fromsw);
202 fprintf (fp, "%s: %s\n", VRSN_FIELD, VRSN_VALUE);
203 fprintf (fp, "%s: application/octet-stream", TYPE_FIELD);
206 fprintf (fp, "; %s", parmsw);
209 fprintf (fp, "\n\t(%s)", cmntsw);
212 fprintf (fp, "\n%s: %s", DESCR_FIELD, descsw);
214 fprintf (fp, "\n%s: %s\n\n", ENCODING_FIELD, "base64");
217 adios (tmpfil, "error writing to");
219 writeBase64aux (stdin, fp);
221 adios (tmpfil, "error writing to");
223 if (fstat (fileno (fp), &st) == NOTOK)
224 adios ("failed", "fstat of %s", tmpfil);
232 vec[0] = r1bindex (postproc, '/');
234 vec[vecp++] = "-verbose";
236 switch (sendsbr (vec, vecp, tmpfil, &st, 0)) {
246 if (unlink (tmpfil) == -1)
247 advise (NULL, "unable to remove temp file %s", tmpfil);
248 return done (status);