]> sjero.net Git - wget/blobdiff - src/netrc.c
[svn] Committed <sxsbsv854j9.fsf@florida.arsdigita.de>.
[wget] / src / netrc.c
index 82de2bc87747d28f71473d2eb10489d81620ddf3..432a8c17e215243d6c2412f7c51e0168f9635870 100644 (file)
@@ -79,7 +79,7 @@ search_netrc (const char *host, const char **acc, const char **passwd,
          char *path = (char *)alloca (strlen (home) + 1
                                       + strlen (NETRC_FILE_NAME) + 1);
          sprintf (path, "%s/%s", home, NETRC_FILE_NAME);
-         free (home);
+         xfree (home);
          err = stat (path, &buf);
          if (err == 0)
            netrc_list = parse_netrc (path);
@@ -173,7 +173,7 @@ read_whole_line (FILE *fp)
     }
   if (c == EOF && !i)
     {
-      free(line);
+      xfree(line);
       return NULL;
     }
 
@@ -200,9 +200,9 @@ maybe_add_to_list (acc_t **newentry, acc_t **list)
   if (a && ! a->acc)
     {
       /* Free any allocated space.  */
-      free (a->host);
-      free (a->acc);
-      free (a->passwd);
+      xfree (a->host);
+      xfree (a->acc);
+      xfree (a->passwd);
     }
   else
     {
@@ -226,6 +226,16 @@ maybe_add_to_list (acc_t **newentry, acc_t **list)
   return;
 }
 
+/* Helper function for the parser, shifts contents of
+   null-terminated string once character to the left.
+   Used in processing \ and " constructs in the netrc file */
+static void
+shift_left(char *string){
+  char *p;
+  
+  for (p=string; *p; ++p)
+    *p = *(p+1);
+}
 
 /* Parse a .netrc file (as described in the ftp(1) manual page).  */
 static acc_t *
@@ -234,7 +244,7 @@ parse_netrc (const char *path)
   FILE *fp;
   char *line, *p, *tok, *premature_token;
   acc_t *current, *retval;
-  int ln;
+  int ln, quote;
 
   /* The latest token we've seen in the file.  */
   enum
@@ -263,6 +273,7 @@ parse_netrc (const char *path)
 
       /* Parse the line.  */
       p = line;
+      quote = 0;
 
       /* If the line is empty, then end any macro definition.  */
       if (last_token == tok_macdef && !*p)
@@ -280,11 +291,25 @@ parse_netrc (const char *path)
          if (*p == '#')
            break;
 
+         /* If the token starts with quotation mark, note this fact,
+            and squash the quotation character */
+         if (*p == '"'){
+           quote = 1;
+           shift_left (p);
+         }
+
          tok = p;
 
-         /* Find the end of the token.  */
-         while (*p && !ISSPACE (*p))
+         /* Find the end of the token, handling quotes and escapes.  */
+         while (*p && (quote ? *p != '"' : !ISSPACE (*p))){
+           if (*p == '\\')
+             shift_left (p);
            p ++;
+         }
+
+         /* if field was quoted, squash the trailing quotation mark */
+         if (quote)
+           shift_left(p);
 
          /* Null-terminate the token, if it isn't already.  */
          if (*p)
@@ -367,14 +392,14 @@ parse_netrc (const char *path)
            }
        }
 
-      free (line);
+      xfree (line);
     }
 
   fclose (fp);
 
   /* Finalize the last machine entry we found.  */
   maybe_add_to_list (&current, &retval);
-  free (current);
+  xfree (current);
 
   /* Reverse the order of the list so that it appears in file order.  */
   current = retval;
@@ -408,7 +433,7 @@ free_netrc(acc_t *l)
       FREE_MAYBE (l->acc);
       FREE_MAYBE (l->passwd);
       FREE_MAYBE (l->host);
-      free(l);
+      xfree (l);
       l = t;
     }
 }