BSD 4_3_Tahoe release
[unix-history] / usr / src / usr.lib / sendmail / aux / mconnect.c
index bc01e0f..7a408f1 100644 (file)
@@ -1,57 +1,62 @@
 /*
 /*
-**  Sendmail
-**  Copyright (c) 1983  Eric P. Allman
-**  Berkeley, California
-**
-**  Copyright (c) 1983 Regents of the University of California.
-**  All rights reserved.  The Berkeley software License Agreement
-**  specifies the terms and conditions for redistribution.
-*/
+ * Copyright (c) 1983 Eric P. Allman
+ * Copyright (c) 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
 
 #ifndef lint
 char copyright[] =
 
 #ifndef lint
 char copyright[] =
-"@(#) Copyright (c) 1980 Regents of the University of California.\n\
+"@(#) Copyright (c) 1988 Regents of the University of California.\n\
  All rights reserved.\n";
  All rights reserved.\n";
-#endif not lint
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char    SccsId[] = "@(#)mconnect.c      5.2 (Berkeley) 7/13/85";
-#endif not lint
+static char sccsid[] = "@(#)mconnect.c 5.4 (Berkeley) 6/29/88";
+#endif /* not lint */
 
 
-# include <stdio.h>
-# include <signal.h>
-# include <ctype.h>
-# include <sgtty.h>
-# include <sys/types.h>
-# include <sys/socket.h>
-# include <netinet/in.h>
-# include <netdb.h>
+#include <stdio.h>
+#include <signal.h>
+#include <ctype.h>
+#include <sgtty.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
 
 
-struct sockaddr_in     SendmailAddress;
-struct sgttyb          TtyBuf;
+static struct sgttyb TtyBuf;
 
 main(argc, argv)
        int argc;
        char **argv;
 {
 
 main(argc, argv)
        int argc;
        char **argv;
 {
+       extern char *optarg;
+       extern int optind;
+       register FILE *f;
        register int s;
        register int s;
-       char *host;
-       register int i;
-       int pid;
        struct servent *sp;
        struct servent *sp;
-       int raw = 0;
-       char buf[1000];
-       register char *p;
-       extern char *index();
-       register FILE *f;
-       extern u_long inet_addr();
-       extern finis();
+       struct sockaddr_in SendmailAddress;
+       int ch, raw;
+       char *host, buf[1000], *index();
+       u_long inet_addr();
+       int finis();
+
+       raw = 0;
+       (void)gtty(0, &TtyBuf);
+       (void)signal(SIGINT, finis);
 
 
-       (void) gtty(0, &TtyBuf);
-       (void) signal(SIGINT, finis);
-       s = socket(AF_INET, SOCK_STREAM, 0, 0);
-       if (s < 0)
-       {
+       if ((s = socket(AF_INET, SOCK_STREAM, 0, 0)) < 0) {
                perror("socket");
                exit(-1);
        }
                perror("socket");
                exit(-1);
        }
@@ -60,89 +65,71 @@ main(argc, argv)
        if (sp != NULL)
                SendmailAddress.sin_port = sp->s_port;
 
        if (sp != NULL)
                SendmailAddress.sin_port = sp->s_port;
 
-       while (--argc > 0)
-       {
-               register char *p = *++argv;
-
-               if (*p == '-')
-               {
-                       switch (*++p)
-                       {
-                         case 'h':             /* host */
-                               break;
-
-                         case 'p':             /* port */
-                               SendmailAddress.sin_port = htons(atoi(*++argv));
-                               argc--;
-                               break;
-
-                         case 'r':             /* raw connection */
-                               raw = 1;
-                               TtyBuf.sg_flags &= ~CRMOD;
-                               stty(0, &TtyBuf);
-                               TtyBuf.sg_flags |= CRMOD;
-                               break;
-                       }
+       while ((ch = getopt(argc, argv, "hp:r")) != EOF)
+               switch((char)ch) {
+               case 'h':       /* host */
+                       break;
+               case 'p':       /* port */
+                       SendmailAddress.sin_port = htons(atoi(optarg));
+                       break;
+               case 'r':       /* raw connection */
+                       raw = 1;
+                       TtyBuf.sg_flags &= ~CRMOD;
+                       stty(0, &TtyBuf);
+                       TtyBuf.sg_flags |= CRMOD;
+                       break;
+               case '?':
+               default:
+                       fputs("usage: mconnect [-hr] [-p port] [host]\n", stderr);
+                       exit(-1);
                }
                }
-               else if (host == NULL)
-                       host = p;
-       }
-       if (host == NULL)
-               host = "localhost";
+       argc -= optind;
+       argv += optind;
+
+       host = argc ? *argv : "localhost";
 
        if (isdigit(*host))
                SendmailAddress.sin_addr.s_addr = inet_addr(host);
 
        if (isdigit(*host))
                SendmailAddress.sin_addr.s_addr = inet_addr(host);
-       else
-       {
+       else {
                register struct hostent *hp = gethostbyname(host);
 
                register struct hostent *hp = gethostbyname(host);
 
-               if (hp == NULL)
-               {
+               if (hp == NULL) {
                        fprintf(stderr, "mconnect: unknown host %s\r\n", host);
                        finis();
                }
                bcopy(hp->h_addr, &SendmailAddress.sin_addr, hp->h_length);
        }
        SendmailAddress.sin_family = AF_INET;
                        fprintf(stderr, "mconnect: unknown host %s\r\n", host);
                        finis();
                }
                bcopy(hp->h_addr, &SendmailAddress.sin_addr, hp->h_length);
        }
        SendmailAddress.sin_family = AF_INET;
-       printf("connecting to host %s (0x%x), port 0x%x\r\n", host,
+       printf("connecting to host %s (0x%lx), port 0x%x\r\n", host,
               SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port);
               SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port);
-       if (connect(s, &SendmailAddress, sizeof SendmailAddress, 0) < 0)
-       {
+       if (connect(s, &SendmailAddress, sizeof(SendmailAddress), 0) < 0) {
                perror("connect");
                exit(-1);
        }
 
        /* good connection, fork both sides */
                perror("connect");
                exit(-1);
        }
 
        /* good connection, fork both sides */
-       printf("connection open\n");
-       pid = fork();
-       if (pid < 0)
-       {
+       puts("connection open");
+       switch(fork()) {
+       case -1:
                perror("fork");
                exit(-1);
                perror("fork");
                exit(-1);
-       }
-       if (pid == 0)
-       {
-               /* child -- standard input to sendmail */
+       case 0: {               /* child -- standard input to sendmail */
                int c;
 
                f = fdopen(s, "w");
                int c;
 
                f = fdopen(s, "w");
-               while ((c = fgetc(stdin)) >= 0)
-               {
+               while ((c = fgetc(stdin)) >= 0) {
                        if (!raw && c == '\n')
                                fputc('\r', f);
                        fputc(c, f);
                        if (c == '\n')
                        if (!raw && c == '\n')
                                fputc('\r', f);
                        fputc(c, f);
                        if (c == '\n')
-                               fflush(f);
+                               (void)fflush(f);
                }
        }
                }
        }
-       else
-       {
-               /* parent -- sendmail to standard output */
+       default:                /* parent -- sendmail to standard output */
                f = fdopen(s, "r");
                f = fdopen(s, "r");
-               while (fgets(buf, sizeof buf, f) != NULL)
-               {
+               while (fgets(buf, sizeof(buf), f) != NULL) {
                        fputs(buf, stdout);
                        fputs(buf, stdout);
-                       fflush(stdout);
+                       (void)fflush(stdout);
                }
        }
        finis();
                }
        }
        finis();