The handling of the argument to the -f option is a hack.

Currently, this works:

    $ mail -f mbox2

But this does not:

    $ mail -fmbox2

Instead of fooling around with argv behind getopt()'s back we can
just treat the remainder of argv[] after option processing as the
file name for -f.  It is not possible to use -f in sending mode so
there is no ambiguity.

This doesn't make the no-space usage work but we can make it clear
in the manual that [file] is not actually an argument to -f.

 - todd

Index: usr.bin/mail/main.c
===================================================================
--- usr.bin/mail/main.c.orig
+++ usr.bin/mail/main.c
@@ -111,16 +111,10 @@ main(int argc, char **argv)
                        /*
                         * User is specifying file to "edit" with Mail,
                         * as opposed to reading system mailbox.
-                        * If no argument is given after -f, we read his
-                        * mbox file.
-                        *
-                        * getopt() can't handle optional arguments, so here
-                        * is an ugly hack to get around it.
-                        */
-                       if ((argv[optind]) && (argv[optind][0] != '-'))
-                               ef = argv[optind++];
-                       else
-                               ef = "&";
+                        * We read his mbox file unless another file
+                        * is specified after the arguments.
+                        */
+                       ef = "&";
                        break;
                case 'n':
                        /*
@@ -169,17 +163,22 @@ main(int argc, char **argv)
                        /*NOTREACHED*/
                }
        }
-       for (i = optind; (argv[i]) && (*argv[i] != '-'); i++)
-               to = cat(to, nalloc(argv[i], GTO));
-       for (; argv[i]; i++)
-               smopts = cat(smopts, nalloc(argv[i], 0));
+       if (ef != NULL) {
+               /* Check for optional mailbox file name. */
+               if (optind < argc) {
+                       ef = argv[optind++];
+                       if (optind < argc)
+                           errx(1, "Cannot give -f and people to send to");
+               }
+       } else {
+               for (i = optind; argv[i]; i++)
+                       to = cat(to, nalloc(argv[i], GTO));
+       }
        /*
         * Check for inconsistent arguments.
         */
        if (to == NULL && (subject != NULL || cc != NULL || bcc != NULL))
                errx(1, "You must specify direct recipients with -s, -c, or 
-b");
-       if (ef != NULL && to != NULL)
-               errx(1, "Cannot give -f and people to send to");
        /*
         * Block SIGINT except where we install an explicit handler for it.
         */
Index: usr.bin/mail/mail.1
===================================================================
--- usr.bin/mail/mail.1.orig
+++ usr.bin/mail/mail.1
@@ -79,13 +79,16 @@ to output all sorts of information usefu
 .Nm mail .
 .It Fl E
 Don't send messages with an empty body.
-.It Fl f Op Ar file
-Read in the contents of your mailbox
-(or the specified
-.Ar file )
-for processing; when you quit,
+.It Fl f
+Use an alternate mailbox.
+Defaults to the user's
+.Ar mbox
+if no
+.Ar file
+is specified.
+When quit,
 .Nm mail
-writes undeleted messages back to this
+will write undeleted messages back to this
 .Ar file .
 .It Fl I
 Forces

Reply via email to