I'd be content with the following logic:

Don't process a `system' wgetrc. If $HOME is not defined, use the directory the Wget executable is in as $HOME (what home_dir() returns).
If $HOME/.wgetrc exists, use that; otherwise look for wget.ini in the directory the executable is in, regardless of $HOME.


We would retain wget.ini support for backward compatibility, and support .wgetrc for consistency with other platforms and with the handling of .netrc. This would only break things if people had $HOME defined and it contained a .wgetrc and they expected the Windows port to ignore it.

As a side-effect, this would also resolve the above issue.


I went ahead and implemented this. I figure at least it will work as an interim solution.


2004-02-16 David Fritz <[EMAIL PROTECTED]>

        * init.c (home_dir): Use aprintf() instead of xmalloc()/sprintf().
        Under Windows, if $HOME is not defined, use the directory that
        contains the Wget binary instead of hard-coded `C:\'.
        (wgetrc_file_name): Under Windows, look for $HOME/.wgetrc then, if
        not found, look for wget.ini in the directory of the Wget binary.

        * mswindows.c (ws_mypath): Employ slightly more robust methodology.
        Strip trailing path separator.


Index: src/init.c
===================================================================
RCS file: /pack/anoncvs/wget/src/init.c,v
retrieving revision 1.91
diff -u -r1.91 init.c
--- src/init.c  2003/12/14 13:35:27     1.91
+++ src/init.c  2004/02/16 15:58:36
@@ -1,5 +1,5 @@
 /* Reading/parsing the initialization file.
-   Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001, 2003
+   Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001, 2003, 2004
    Free Software Foundation, Inc.
 
 This file is part of GNU Wget.
@@ -314,9 +314,9 @@
        return NULL;
       home = pwd->pw_dir;
 #else  /* WINDOWS */
-      home = "C:\\";
-      /* #### Maybe I should grab home_dir from registry, but the best
-        that I could get from there is user's Start menu.  It sucks!  */
+      /* Under Windows, if $HOME isn't defined, use the directory where
+         `wget.exe' resides.  */
+      home = ws_mypath ();
 #endif /* WINDOWS */
     }
 
@@ -347,27 +347,24 @@
       return xstrdup (env);
     }
 
-#ifndef WINDOWS
   /* If that failed, try $HOME/.wgetrc.  */
   home = home_dir ();
   if (home)
-    {
-      file = (char *)xmalloc (strlen (home) + 1 + strlen (".wgetrc") + 1);
-      sprintf (file, "%s/.wgetrc", home);
-    }
+    file = aprintf ("%s/.wgetrc", home);
   xfree_null (home);
-#else  /* WINDOWS */
-  /* Under Windows, "home" is (for the purposes of this function) the
-     directory where `wget.exe' resides, and `wget.ini' will be used
-     as file name.  SYSTEM_WGETRC should not be defined under WINDOWS.
-
-     It is not as trivial as I assumed, because on 95 argv[0] is full
-     path, but on NT you get what you typed in command line.  --dbudor */
-  home = ws_mypath ();
-  if (home)
+
+#ifdef WINDOWS
+  /* Under Windows, if we still haven't found .wgetrc, look for the file
+     `wget.ini' in the directory where `wget.exe' resides; we do this for
+     backward compatibility with previous versions of Wget.
+     SYSTEM_WGETRC should not be defined under WINDOWS.  */
+  if (!file || !file_exists_p (file))
     {
-      file = (char *)xmalloc (strlen (home) + strlen ("wget.ini") + 1);
-      sprintf (file, "%swget.ini", home);
+      xfree_null (file);
+      file = NULL;
+      home = ws_mypath ();
+      if (home)
+       file = aprintf ("%s/wget.ini", home);
     }
 #endif /* WINDOWS */
 
Index: src/mswindows.c
===================================================================
RCS file: /pack/anoncvs/wget/src/mswindows.c,v
retrieving revision 1.22
diff -u -r1.22 mswindows.c
--- src/mswindows.c     2003/11/03 21:57:03     1.22
+++ src/mswindows.c     2004/02/16 15:58:37
@@ -1,5 +1,5 @@
 /* mswindows.c -- Windows-specific support
-   Copyright (C) 1995, 1996, 1997, 1998  Free Software Foundation, Inc.
+   Copyright (C) 1995, 1996, 1997, 1998, 2004  Free Software Foundation, Inc.
 
 This file is part of GNU Wget.
 
@@ -199,22 +199,25 @@
 ws_mypath (void)
 {
   static char *wspathsave = NULL;
-  char buffer[MAX_PATH];
-  char *ptr;
 
-  if (wspathsave)
+  if (!wspathsave)
     {
-      return wspathsave;
-    }
+      char buf[MAX_PATH + 1];
+      char *p;
+      DWORD len;
+
+      len = GetModuleFileName (GetModuleHandle (NULL), buf, sizeof (buf));
+      if (!len || (len >= sizeof (buf)))
+        return NULL;
+
+      p = strrchr (buf, PATH_SEPARATOR);
+      if (!p)
+        return NULL;
 
-  if (GetModuleFileName (NULL, buffer, MAX_PATH) &&
-      (ptr = strrchr (buffer, PATH_SEPARATOR)) != NULL)
-    {
-      *(ptr + 1) = '\0';
-      wspathsave = xstrdup (buffer);
+      *p = '\0';
+      wspathsave = xstrdup (buf);
     }
-  else
-    wspathsave = NULL;
+
   return wspathsave;
 }
 

Reply via email to