]> sjero.net Git - wget/blobdiff - src/cmpt.c
[svn] Parse Content-Disposition better. Implement memrchr where missing.
[wget] / src / cmpt.c
index 08a92aefadab9b75f4c69809e98c46bad5d9f3fc..d1cff3b7bdfdab6e9ff27c3786dd821dfa6bff48 100644 (file)
@@ -111,10 +111,28 @@ strncasecmp (const char *s1, const char *s2, size_t n)
   return c1 - c2;
 }
 #endif /* not HAVE_STRNCASECMP */
+
+#ifndef HAVE_MEMRCHR
+/* memrchr is a GNU extension.  It is like the memchr function, except
+   that it searches backwards from the end of the n bytes pointed to
+   by s instead of forwards from the front.  */
+
+void *
+memrchr (const void *s, int c, size_t n)
+{
+  const char *b = s;
+  const char *e = b + n;
+  while (e > b)
+    if (*--e == c)
+      return (void *) e;
+  return NULL;
+}
+#endif
 \f
 /* strptime is required by POSIX, but it is missing from Windows,
    which means we must keep a fallback implementation.  It is
-   reportedly missing or broken on many older systems as well.  */
+   reportedly missing or broken on many older Unix systems as well, so
+   it's good to have around.  */
 
 #ifndef HAVE_STRPTIME
 /* From GNU libc 2.1.3.  */
@@ -1046,9 +1064,10 @@ const unsigned short int __mon_yday[2][13] =
    and given a prefix, but many systems out there are still (as of
    this writing in 2005) broken and we must cater to them.
 
-   Additionally, according to some conventional, many historical
-   implementations of fnmatch are buggy and unreliable.  If yours is
-   such, undefine SYSTEM_FNMATCH in sysdep.h and tell us about it.  */
+   Additionally, according to some conventional wisdom, many
+   historical implementations of fnmatch are buggy and unreliable.  If
+   yours is such, undefine SYSTEM_FNMATCH in sysdep.h and tell us
+   about it.  */
 
 #ifndef SYSTEM_FNMATCH
 
@@ -1296,9 +1315,9 @@ char_value (char c, int base)
   return value;
 }
 
-#define LL strtoll_return      /* long long or __int64 */
+#define LL strtoll_type                /* long long or __int64 */
 
-/* These constants assume 64-bit strtoll_return. */
+/* These constants assume 64-bit strtoll_type. */
 
 /* A roundabout way of writing 2**63-1 = 9223372036854775807 */
 #define STRTOLL_OVERFLOW (((LL) 1 << 62) - 1 + ((LL) 1 << 62))
@@ -1306,13 +1325,13 @@ char_value (char c, int base)
 #define STRTOLL_UNDERFLOW (-STRTOLL_OVERFLOW - 1)
 
 /* A strtoll replacement for systems that have LFS but don't supply
-   strtoll.  The headers typedef strtoll_return to long long or to
+   strtoll.  The headers typedef strtoll_type to long long or to
    __int64.  */
 
-strtoll_return
+strtoll_type
 strtoll (const char *nptr, char **endptr, int base)
 {
-  strtoll_return result = 0;
+  strtoll_type result = 0;
   bool negative;
 
   if (base != 0 && (base < 2 || base > 36))
@@ -1347,6 +1366,13 @@ strtoll (const char *nptr, char **endptr, int base)
        {
          base = 16;
          nptr += 2;
+         /* "0x" must be followed by at least one hex char.  If not,
+            return 0 and place ENDPTR on 'x'. */
+         if (!ISXDIGIT (*nptr))
+           {
+             --nptr;
+             goto out;
+           }
        }
       else if (base == 0)
        base = 8;
@@ -1360,7 +1386,7 @@ strtoll (const char *nptr, char **endptr, int base)
       int val;
       for (; (val = char_value (*nptr, base)) != -1; ++nptr)
        {
-         strtoll_return newresult = base * result + val;
+         strtoll_type newresult = base * result + val;
          if (newresult < result)
            {
              result = STRTOLL_OVERFLOW;
@@ -1376,7 +1402,7 @@ strtoll (const char *nptr, char **endptr, int base)
       int val;
       for (; (val = char_value (*nptr, base)) != -1; ++nptr)
        {
-         strtoll_return newresult = base * result - val;
+         strtoll_type newresult = base * result - val;
          if (newresult > result)
            {
              result = STRTOLL_UNDERFLOW;
@@ -1386,6 +1412,7 @@ strtoll (const char *nptr, char **endptr, int base)
          result = newresult;
        }
     }
+ out:
   if (endptr)
     *endptr = (char *) nptr;
   return result;