]> Pileus Git - ~andy/fetchmail/blobdiff - netrc.c
Minor bug fixes for socket.c
[~andy/fetchmail] / netrc.c
diff --git a/netrc.c b/netrc.c
index 1eaf85df6d5125b81abc084ea26ae4028a96aefe..a585e1a0be37c236336713b949f15e7f9b3926e6 100644 (file)
--- a/netrc.c
+++ b/netrc.c
@@ -1,18 +1,22 @@
-/* netrc.c -- parse the .netrc file to get hosts, accounts, and passwords
-
+/*
+ * netrc.c -- parse the .netrc file to get hosts, accounts, and passwords
+ *
    Gordon Matzigkeit <gord@gnu.ai.mit.edu>, 1996
    Copyright assigned to Eric S. Raymond, October 2001.
 
    For license terms, see the file COPYING in this directory.
 
-   Compile with -DSTANDALONE to test this module. */
+   Compile with -DSTANDALONE to test this module.
+   (Makefile.am should have a rule so you can just type "make netrc")
+*/
+
+#include "config.h"
 
 #include <stdio.h>
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include "config.h"
 #include "fetchmail.h"
 #include "netrc.h"
 #include "i18n.h"
@@ -25,7 +29,7 @@
 # define xmalloc malloc
 # define xrealloc realloc
 
-char *program_name = "netrc";
+const char *program_name = "netrc";
 #endif
 
 /* Maybe add NEWENTRY to the account information list, LIST.  NEWENTRY is
@@ -73,8 +77,7 @@ maybe_add_to_list (netrc_entry **newentry, netrc_entry **list)
    list of entries.  NULL is returned if the file could not be
    parsed. */
 netrc_entry *
-parse_netrc (file)
-     char *file;
+parse_netrc (char *file)
 {
     FILE *fp;
     char buf[POPBUFSIZE+1], *p, *tok;
@@ -213,15 +216,9 @@ parse_netrc (file)
 
            if (premature_token)
            {
-#ifdef HAVE_ERROR
-               error_at_line (0, file, ln,
-                              GT_("warning: found \"%s\" before any host names"),
-                              premature_token);
-#else
                fprintf (stderr,
                         GT_("%s:%d: warning: found \"%s\" before any host names\n"),
                         file, ln, premature_token);
-#endif
                premature_token = NULL;
            }
 
@@ -294,9 +291,7 @@ parse_netrc (file)
 /* Return the netrc entry from LIST corresponding to HOST.  NULL is
    returned if no such entry exists. */
 netrc_entry *
-search_netrc (list, host, login)
-     netrc_entry *list;
-     char *host, *login;
+search_netrc (netrc_entry *list, char *host, char *login)
 {
     /* Look for the HOST in LIST. */
     while (list)
@@ -313,6 +308,20 @@ search_netrc (list, host, login)
     return list;
 }
 
+void
+free_netrc(netrc_entry *a) {
+    while(a) {
+       netrc_entry *n = a->next;
+       if (a->password != NULL) {
+               memset(a->password, 0x55, strlen(a->password));
+               free(a->password);
+       }
+       xfree(a->login);
+       xfree(a->host);
+       xfree(a);
+       a = n;
+    }
+}
 
 #ifdef STANDALONE
 #include <sys/types.h>
@@ -323,7 +332,7 @@ search_netrc (list, host, login)
 int main (int argc, char **argv)
 {
     struct stat sb;
-    char *program_name, *file, *host, *login;
+    char *file, *host, *login;
     netrc_entry *head, *a;
 
     program_name = argv[0];
@@ -331,6 +340,15 @@ int main (int argc, char **argv)
     host = argv[2];
     login = argv[3];
 
+    switch (argc) {
+       case 2:
+       case 4:
+           break;
+       default:
+           fprintf (stderr, "Usage: %s <file> [<host> <login>]\n", argv[0]);
+           exit(EXIT_FAILURE);
+    }
+
     if (stat (file, &sb))
     {
        fprintf (stderr, "%s: cannot stat %s: %s\n", argv[0], file,
@@ -348,7 +366,7 @@ int main (int argc, char **argv)
     if (host && login)
     {
        int status;
-       status = 0;
+       status = EXIT_SUCCESS;
 
        printf("Host: %s, Login: %s\n", host, login);
            
@@ -358,10 +376,10 @@ int main (int argc, char **argv)
            /* Print out the password (if any). */
            if (a->password)
            {
-               fputc (' ', stdout);
-               fputs (a->password, stdout);
+               printf("Password: %s\n", a->password);
            }
-       }
+       } else
+           status = EXIT_FAILURE;
        fputc ('\n', stdout);
 
        exit (status);
@@ -393,6 +411,8 @@ int main (int argc, char **argv)
        a = a->next;
     }
 
+    free_netrc(head);
+
     exit (0);
 }
 #endif /* STANDALONE */