2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 4/20/95";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
51 * Mail -- a mail program
53 * Startup -- interface with user.
56 static jmp_buf hdrjmp;
58 extern const char *version;
61 main(int argc, char *argv[])
64 struct name *to, *cc, *bcc, *smopts;
65 char *subject, *replyto;
71 * Set up a reasonable environment.
72 * Figure out whether we are being run interactively,
73 * start the SIGCHLD catcher, and so forth.
75 (void)signal(SIGCHLD, sigchild);
77 assign("interactive", "");
80 * Now, determine how we are being used.
81 * We successively pick off - flags.
82 * If there is anything left, it is the base of the list
83 * of users to mail to. Argp will be set to point to the
84 * first of these users.
92 while ((i = getopt(argc, argv, "FEHINT:b:c:edfins:u:v")) != -1) {
96 * Next argument is temp file to write which
97 * articles have been read/deleted for netnews.
100 if ((i = open(Tflag, O_CREAT | O_TRUNC | O_WRONLY,
107 * Next argument is person to pretend to be.
114 * User wants to ignore interrupts.
115 * Set the variable "ignore"
117 assign("ignore", "");
124 * User wants to check mail and exit.
126 assign("checkmode", "");
130 * User wants a header summary only.
132 assign("headersummary", "");
136 * User wants to record messages to files
137 * named after first recipient username.
139 assign("recordrecip", "");
143 * Give a subject field for sending from
150 * User is specifying file to "edit" with Mail,
151 * as opposed to reading system mailbox.
152 * If no argument is given after -f, we read his
155 * getopt() can't handle optional arguments, so here
156 * is an ugly hack to get around it.
158 if ((argv[optind] != NULL) && (argv[optind][0] != '-'))
165 * User doesn't want to source /usr/lib/Mail.rc
171 * Avoid initial header printing.
173 assign("noheader", "");
177 * Send mailer verbose flag
179 assign("verbose", "");
185 assign("interactive", "");
189 * Get Carbon Copy Recipient list
191 cc = cat(cc, nalloc(optarg, GCC));
195 * Get Blind Carbon Copy Recipient list
197 bcc = cat(bcc, nalloc(optarg, GBCC));
201 * Don't send empty files.
203 assign("dontsendempty", "");
207 Usage: %s [-dEiInv] [-s subject] [-c cc-addr] [-b bcc-addr] [-F] to-addr ...\n\
208 %*s [-sendmail-option ...]\n\
209 %s [-dEHiInNv] [-F] -f [name]\n\
210 %s [-dEHiInNv] [-F] [-u user]\n\
211 %s [-d] -e [-f name]\n", __progname, (int)strlen(__progname), "",
212 __progname, __progname, __progname);
216 for (i = optind; (argv[i] != NULL) && (*argv[i] != '-'); i++)
217 to = cat(to, nalloc(argv[i], GTO));
218 for (; argv[i] != NULL; i++)
219 smopts = cat(smopts, nalloc(argv[i], 0));
221 * Check for inconsistent arguments.
223 if (to == NULL && (subject != NULL || cc != NULL || bcc != NULL))
224 errx(1, "You must specify direct recipients with -s, -c, or -b.");
225 if (ef != NULL && to != NULL)
226 errx(1, "Cannot give -f and people to send to.");
235 if ((path_rc = malloc(sizeof(_PATH_MASTER_RC))) == NULL)
236 err(1, "malloc(path_rc) failed");
238 strcpy(path_rc, _PATH_MASTER_RC);
239 while ((s = strsep(&path_rc, ":")) != NULL)
244 * Expand returns a savestr, but load only uses the file name
245 * for fopen, so it's safe to do this.
247 if ((rc = getenv("MAILRC")) == NULL)
251 replyto = value("REPLYTO");
253 mail(to, cc, bcc, smopts, subject, replyto);
260 if(value("checkmode") != NULL) {
263 if (setfile(ef) <= 0)
264 /* Either an error has occurred, or no mail */
272 * Ok, we are reading mail.
273 * Decide whether we are editing a mailbox or reading
274 * the system mailbox, and open up the right stuff.
279 exit(1); /* error already reported */
280 if (setjmp(hdrjmp) == 0) {
281 if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
282 (void)signal(SIGINT, hdrstop);
283 if (value("quiet") == NULL)
284 printf("Mail version %s. Type ? for help.\n",
287 (void)fflush(stdout);
288 (void)signal(SIGINT, prevint);
291 /* If we were in header summary mode, it's time to exit. */
292 if (value("headersummary") != NULL)
296 (void)signal(SIGHUP, SIG_IGN);
297 (void)signal(SIGINT, SIG_IGN);
298 (void)signal(SIGQUIT, SIG_IGN);
304 * Interrupt printing of the headers.
308 hdrstop(int signo __unused)
311 (void)fflush(stdout);
312 fprintf(stderr, "\nInterrupt\n");
317 * Compute what the screen size for printing headers should be.
318 * We use the following algorithm for the height:
319 * If baud rate < 1200, use 9
320 * If baud rate = 1200, use 14
321 * If baud rate > 1200, use 24 or ws_row
322 * Width is either 80 or ws_col;
331 if (ioctl(1, TIOCGWINSZ, (char *)&ws) < 0)
332 ws.ws_col = ws.ws_row = 0;
333 if (tcgetattr(1, &tbuf) < 0)
336 speed = cfgetospeed(&tbuf);
339 else if (speed == B1200)
341 else if (ws.ws_row != 0)
342 screenheight = ws.ws_row;
345 if ((realscreenheight = ws.ws_row) == 0)
346 realscreenheight = 24;
347 if ((screenwidth = ws.ws_col) == 0)