2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static char copyright[] =
32 "@(#) Copyright (c) 1980, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 4/20/95";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
49 * Mail -- a mail program
51 * Startup -- interface with user.
54 static jmp_buf hdrjmp;
56 extern const char *version;
59 main(int argc, char *argv[])
62 struct name *to, *cc, *bcc, *smopts;
63 char *subject, *replyto;
69 * Set up a reasonable environment.
70 * Figure out whether we are being run interactively,
71 * start the SIGCHLD catcher, and so forth.
73 (void)signal(SIGCHLD, sigchild);
75 assign("interactive", "");
78 * Now, determine how we are being used.
79 * We successively pick off - flags.
80 * If there is anything left, it is the base of the list
81 * of users to mail to. Argp will be set to point to the
82 * first of these users.
90 while ((i = getopt(argc, argv, "FEHINT:b:c:edfins:u:v")) != -1) {
94 * Next argument is temp file to write which
95 * articles have been read/deleted for netnews.
98 if ((i = open(Tflag, O_CREAT | O_TRUNC | O_WRONLY,
105 * Next argument is person to pretend to be.
112 * User wants to ignore interrupts.
113 * Set the variable "ignore"
115 assign("ignore", "");
122 * User wants to check mail and exit.
124 assign("checkmode", "");
128 * User wants a header summary only.
130 assign("headersummary", "");
134 * User wants to record messages to files
135 * named after first recipient username.
137 assign("recordrecip", "");
141 * Give a subject field for sending from
148 * User is specifying file to "edit" with Mail,
149 * as opposed to reading system mailbox.
150 * If no argument is given after -f, we read his
153 * getopt() can't handle optional arguments, so here
154 * is an ugly hack to get around it.
156 if ((argv[optind] != NULL) && (argv[optind][0] != '-'))
163 * User doesn't want to source /usr/lib/Mail.rc
169 * Avoid initial header printing.
171 assign("noheader", "");
175 * Send mailer verbose flag
177 assign("verbose", "");
183 assign("interactive", "");
187 * Get Carbon Copy Recipient list
189 cc = cat(cc, nalloc(optarg, GCC));
193 * Get Blind Carbon Copy Recipient list
195 bcc = cat(bcc, nalloc(optarg, GBCC));
199 * Don't send empty files.
201 assign("dontsendempty", "");
205 Usage: %s [-dEiInv] [-s subject] [-c cc-addr] [-b bcc-addr] [-F] to-addr ...\n\
206 %*s [-sendmail-option ...]\n\
207 %s [-dEHiInNv] [-F] -f [name]\n\
208 %s [-dEHiInNv] [-F] [-u user]\n\
209 %s [-d] -e [-f name]\n", __progname, strlen(__progname), "",
210 __progname, __progname, __progname);
214 for (i = optind; (argv[i] != NULL) && (*argv[i] != '-'); i++)
215 to = cat(to, nalloc(argv[i], GTO));
216 for (; argv[i] != NULL; i++)
217 smopts = cat(smopts, nalloc(argv[i], 0));
219 * Check for inconsistent arguments.
221 if (to == NULL && (subject != NULL || cc != NULL || bcc != NULL))
222 errx(1, "You must specify direct recipients with -s, -c, or -b.");
223 if (ef != NULL && to != NULL)
224 errx(1, "Cannot give -f and people to send to.");
233 if ((path_rc = malloc(sizeof(_PATH_MASTER_RC))) == NULL)
234 err(1, "malloc(path_rc) failed");
236 strcpy(path_rc, _PATH_MASTER_RC);
237 while ((s = strsep(&path_rc, ":")) != NULL)
242 * Expand returns a savestr, but load only uses the file name
243 * for fopen, so it's safe to do this.
245 if ((rc = getenv("MAILRC")) == NULL)
249 replyto = value("REPLYTO");
251 mail(to, cc, bcc, smopts, subject, replyto);
258 if(value("checkmode") != NULL) {
261 if (setfile(ef) <= 0)
262 /* Either an error has occured, or no mail */
270 * Ok, we are reading mail.
271 * Decide whether we are editing a mailbox or reading
272 * the system mailbox, and open up the right stuff.
277 exit(1); /* error already reported */
278 if (setjmp(hdrjmp) == 0) {
279 if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
280 (void)signal(SIGINT, hdrstop);
281 if (value("quiet") == NULL)
282 printf("Mail version %s. Type ? for help.\n",
285 (void)fflush(stdout);
286 (void)signal(SIGINT, prevint);
289 /* If we were in header summary mode, it's time to exit. */
290 if (value("headersummary") != NULL)
294 (void)signal(SIGHUP, SIG_IGN);
295 (void)signal(SIGINT, SIG_IGN);
296 (void)signal(SIGQUIT, SIG_IGN);
302 * Interrupt printing of the headers.
306 hdrstop(int signo __unused)
309 (void)fflush(stdout);
310 fprintf(stderr, "\nInterrupt\n");
315 * Compute what the screen size for printing headers should be.
316 * We use the following algorithm for the height:
317 * If baud rate < 1200, use 9
318 * If baud rate = 1200, use 14
319 * If baud rate > 1200, use 24 or ws_row
320 * Width is either 80 or ws_col;
329 if (ioctl(1, TIOCGWINSZ, (char *)&ws) < 0)
330 ws.ws_col = ws.ws_row = 0;
331 if (tcgetattr(1, &tbuf) < 0)
334 speed = cfgetospeed(&tbuf);
337 else if (speed == B1200)
339 else if (ws.ws_row != 0)
340 screenheight = ws.ws_row;
343 if ((realscreenheight = ws.ws_row) == 0)
344 realscreenheight = 24;
345 if ((screenwidth = ws.ws_col) == 0)