(strcasecmp): Conform to ANSI specs for toupper.
(strncasecmp): Ditto.  Also do not use side-effects in macro args.
(wine_openpty): Plug descriptor leak.  Always set errno on failure.
(wine_anon_mmap): Fix comment location.

diff --git a/library/port.c b/library/port.c
index f44f37e..cc41a2e 100644
--- a/library/port.c
+++ b/library/port.c
@@ -164,8 +164,14 @@
 #ifndef HAVE_STRCASECMP
 int strcasecmp( const char *str1, const char *str2 )
 {
-    while (*str1 && toupper(*str1) == toupper(*str2)) { str1++; str2++; }
-    return toupper(*str1) - toupper(*str2);
+    const unsigned char *ustr1 = (const unsigned char *)str1;
+    const unsigned char *ustr2 = (const unsigned char *)str2;
+
+    while (*ustr1 && toupper(*ustr1) == toupper(*ustr2)) {
+        ustr1++;
+	ustr2++;
+    }
+    return toupper(*ustr1) - toupper(*ustr2);
 }
 #endif /* HAVE_STRCASECMP */
 
@@ -175,11 +181,17 @@
 #ifndef HAVE_STRNCASECMP
 int strncasecmp( const char *str1, const char *str2, size_t n )
 {
+    const unsigned char *ustr1 = (const unsigned char *)str1;
+    const unsigned char *ustr2 = (const unsigned char *)str2;
     int res;
+
     if (!n) return 0;
-    while ((--n > 0) && *str1)
-      if ((res = toupper(*str1++) - toupper(*str2++))) return res;
-    return toupper(*str1) - toupper(*str2);
+    while ((--n > 0) && *ustr1) {
+	if ((res = toupper(*ustr1) - toupper(*ustr2))) return res;
+	ustr1++;
+	ustr2++;
+    }
+    return toupper(*ustr1) - toupper(*ustr2);
 }
 #endif /* HAVE_STRNCASECMP */
 
@@ -197,9 +209,9 @@
                  struct termios *term, struct winsize *winsize)
 {
 #ifdef HAVE_OPENPTY
-    return openpty(master,slave,name,term,winsize);
+    return openpty(master, slave, name, term, winsize);
 #else
-    char *ptr1, *ptr2;
+    const char *ptr1, *ptr2;
     char pts_name[512];
 
     strcpy (pts_name, "/dev/ptyXY");
@@ -218,6 +230,7 @@
             pts_name[5] = 't';
             if ((*slave = open(pts_name, O_RDWR)) < 0) {
                 pts_name[5] = 'p';
+		close (*master);
                 continue;
             }
 
@@ -230,6 +243,7 @@
             return *slave;
         }
     }
+    errno = EMFILE;
     return -1;
 #endif
 }
@@ -405,13 +419,16 @@
         }
     }
 #endif  /* MAP_ANON */
-    /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
+
 #ifdef MAP_SHARED
     flags &= ~MAP_SHARED;
 #endif
+
+    /* Linux EINVAL's on us if we don't pass MAP_PRIVATE to an anon mmap */
 #ifdef MAP_PRIVATE
     flags |= MAP_PRIVATE;
 #endif
+
     return mmap( start, size, prot, flags, fdzero, 0 );
 }