]> git.notmuchmail.org Git - notmuch/commitdiff
smtp-dummy: Prefer return rather than exit() in main.
authorCarl Worth <cworth@cworth.org>
Tue, 21 Jun 2011 21:54:10 +0000 (14:54 -0700)
committerCarl Worth <cworth@cworth.org>
Wed, 22 Jun 2011 13:38:33 +0000 (06:38 -0700)
The main() function should be written as just another function with a
return value. This allows for more reliable code reuse. Imagine that
main() grows too large and needs to be factored into multiple
functions. At that point, exit() is probably the wrong thing, yet can
also be hard to notice as it's in less-frequently-tested exceptional
cases.

test/smtp-dummy.c

index 133d6c4eea3bfc43df4780d8d777e2dbbd2fdbbd..9da8202fd1ff8c0f7a9b91ff8f6490350831a5dc 100644 (file)
@@ -127,7 +127,7 @@ main (int argc, char *argv[])
 
        if (argc != 2) {
                fprintf (stderr, "Usage: %s <output-file>\n", argv[0]);
-               exit (1);
+               return 1;
        }
 
        output_filename = argv[1];
@@ -135,14 +135,14 @@ main (int argc, char *argv[])
        if (output == NULL) {
                fprintf (stderr, "Failed to open %s for writing: %s\n",
                         output_filename, strerror (errno));
-               exit (1);
+               return 1;
        }
 
        sock = socket (AF_INET, SOCK_STREAM, 0);
        if (sock == -1) {
                fprintf (stderr, "Error: socket() failed: %s\n",
                         strerror (errno));
-               exit (1);
+               return 1;
        }
 
        reuse = 1;
@@ -150,13 +150,13 @@ main (int argc, char *argv[])
        if (err) {
                fprintf (stderr, "Error: setsockopt() failed: %s\n",
                         strerror (errno));
-               exit (1);
+               return 1;
        }
 
        hostinfo = gethostbyname ("localhost");
        if (hostinfo == NULL) {
                fprintf (stderr, "Unknown host: localhost\n");
-               exit (1);
+               return 1;
        }
 
        addr.sin_family = AF_INET;
@@ -167,7 +167,7 @@ main (int argc, char *argv[])
                fprintf (stderr, "Error: bind() failed: %s\n",
                         strerror (errno));
                close (sock);
-               exit (1);
+               return 1;
        }
 
        err = listen (sock, 1);
@@ -175,7 +175,7 @@ main (int argc, char *argv[])
                fprintf (stderr, "Error: listen() failed: %s\n",
                         strerror (errno));
                close (sock);
-               exit (1);
+               return 1;
        }
 
        peer_addr_len = sizeof (peer_addr);
@@ -183,14 +183,14 @@ main (int argc, char *argv[])
        if (peer == -1) {
                fprintf (stderr, "Error: accept() failed: %s\n",
                         strerror (errno));
-               exit (1);
+               return 1;
        }
 
        peer_file = fdopen (peer, "w+");
        if (peer_file == NULL) {
                fprintf (stderr, "Error: fdopen() failed: %s\n",
                         strerror (errno));
-               exit (1);
+               return 1;
        }
 
        do_smtp_to_file (peer_file, output);