]> sjero.net Git - wget/blobdiff - src/mswindows.c
[svn] Large file support added. Published in <87psyr6jn7.fsf@xemacs.org>.
[wget] / src / mswindows.c
index df8d5161ec2bc040d7a9f8544025ec01c9e96b16..93bf0c6967192be79c3a9ea4ae2b83a1e17b5910 100644 (file)
@@ -86,6 +86,146 @@ xsleep (double seconds)
 #endif /* not HAVE_USLEEP */
 }
 
+#if defined(_MSC_VER) && _MSC_VER < 1300
+
+static inline int
+char_value (char c, int base)
+{
+  int value;
+  if (c < '0')
+    return -1;
+  if ('0' <= c && c <= '9')
+    value = c - '0';
+  else if ('a' <= c && c <= 'z')
+    value = c - 'a' + 10;
+  else if ('A' <= c && c <= 'Z')
+    value = c - 'A' + 10;
+  else
+    return -1;
+  if (value >= base)
+    return -1;
+  return value;
+}
+
+/* A fairly simple strtoll replacement for MS VC versions that don't
+   supply _strtoi64.  */
+
+__int64
+str_to_int64 (const char *nptr, char **endptr, int base)
+{
+#define OVERFLOW 9223372036854775807I64
+#define UNDERFLOW (-OVERFLOW - 1)
+
+  __int64 result = 0;
+  int negative;
+
+  if (base != 0 && (base < 2 || base > 36))
+    {
+      errno = EINVAL;
+      return 0;
+    }
+
+  while (*nptr == ' ' || *nptr == '\t')
+    ++nptr;
+  if (*nptr == '-')
+    {
+      negative = 1;
+      ++nptr;
+    }
+  else if (*nptr == '+')
+    {
+      negative = 0;
+      ++nptr;
+    }
+  else
+    negative = 0;
+
+  /* If base is 0, determine the real base based on the beginning on
+     the number; octal numbers begin with "0", hexadecimal with "0x",
+     and the others are considered octal.  */
+  if (*nptr == '0')
+    {
+      if ((base == 0 || base == 16)
+         &&
+         (*(nptr + 1) == 'x' || *(nptr + 1) == 'X'))
+       {
+         base = 16;
+         nptr += 2;
+       }
+      else if (base == 0)
+       base = 8;
+    }
+  else if (base == 0)
+    base = 10;
+
+  if (!negative)
+    {
+      /* Parse positive number, checking for overflow. */
+      int val;
+      for (; (val = char_value (*nptr, base)) != -1; ++nptr)
+       {
+         __int64 newresult = base * result + val;
+         if (newresult < result)
+           {
+             result = OVERFLOW;
+             errno = ERANGE;
+             break;
+           }
+         result = newresult;
+       }
+    }
+  else
+    {
+      /* Parse negative number, checking for underflow. */
+      int val;
+      for (; (val = char_value (*nptr, base)) != -1; ++nptr)
+       {
+         __int64 newresult = base * result - val;
+         if (newresult > result)
+           {
+             result = UNDERFLOW;
+             errno = ERANGE;
+             break;
+           }
+         result = newresult;
+       }
+    }
+  if (endptr)
+    *endptr = (char *) nptr;
+  return result;
+}
+
+#else  /* !defined(_MSC_VER) || _MSC_VER >= 1300 */
+
+__int64
+str_to_int64 (const char *nptr, char **endptr, int base)
+{
+#ifdef _MSC_VER
+  return _strtoi64 (nptr, endptr, base);
+#else
+  return strtoll (nptr, endptr, base);
+#endif
+}
+
+#endif /* !defined(_MSC_VER) || _MSC_VER >= 1300 */
+
+/* A simple clone of ftello.  The normal ftell doesn't work for large
+   files, so this is needed, and used by file_size(), which is itself
+   used for the --post-file option.
+
+   This function uses fgetpos incorrectly and should be considered a
+   hack until a better way to tell the stream position is found.  */
+
+__int64
+wget_ftello (FILE *fp)
+{
+  fpos_t pos;
+  if (fgetpos (fp, &pos) != 0)
+    return -1;
+  else
+    return pos;
+}
+
 void
 windows_main_junk (int *argc, char **argv, char **exec_name)
 {
@@ -209,7 +349,6 @@ fake_fork_child (void)
 static void
 fake_fork (void)
 {
-  char *cmdline, *args;
   char exe[MAX_PATH + 1];
   DWORD exe_len, le;
   SECURITY_ATTRIBUTES sa;
@@ -222,22 +361,6 @@ fake_fork (void)
 
   event = section = pi.hProcess = pi.hThread = NULL;
 
-  /* Get command line arguments to pass to the child process.
-     We need to skip the name of the command (what amounts to argv[0]).  */
-  cmdline = GetCommandLine ();
-  if (*cmdline == '"')
-    {
-      args = strchr (cmdline + 1, '"');
-      if (args)
-        ++args;
-    }
-  else
-    args = strchr (cmdline, ' ');
-
-  /* It's ok if args is NULL, that would mean there were no arguments
-     after the command name.  As it is now though, we would never get here
-     if that were true.  */
-
   /* Get the fully qualified name of our executable.  This is more reliable
      than using argv[0].  */
   exe_len = GetModuleFileName (GetModuleHandle (NULL), exe, sizeof (exe));
@@ -258,8 +381,9 @@ fake_fork (void)
      suspended state.  */
   memset (&si, 0, sizeof (si));
   si.cb = sizeof (si);
-  rv = CreateProcess (exe, args, NULL, NULL, TRUE, CREATE_SUSPENDED |
-                      DETACHED_PROCESS, NULL, NULL, &si, &pi);
+  rv = CreateProcess (exe, GetCommandLine (), NULL, NULL, TRUE,
+                      CREATE_SUSPENDED | DETACHED_PROCESS,
+                      NULL, NULL, &si, &pi);
   if (!rv)
     goto cleanup;