- (djm) Sync openbsd-compat/glob.c
diff --git a/ChangeLog b/ChangeLog
index 6f7e8ae..1e138e8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -6,6 +6,7 @@
    fix from Philippe Levan <levan@epix.net>
  - (djm) Rework krbIV tests to get us closer to building on Redhat. Still
    doesn't work because of conflicts between krbIV's and OpenSSL's des.h
+ - (djm) Sync openbsd-compat/glob.c
 
 20010327
  - Attempt sync with sshlogin.c w/ OpenBSD (mainly CVS ID)
@@ -4734,4 +4735,4 @@
  - Wrote replacements for strlcpy and mkdtemp
  - Released 1.0pre1
 
-$Id: ChangeLog,v 1.1024 2001/03/28 04:37:06 djm Exp $
+$Id: ChangeLog,v 1.1025 2001/03/28 11:05:26 djm Exp $
diff --git a/configure.in b/configure.in
index 256443a..792e629 100644
--- a/configure.in
+++ b/configure.in
@@ -1,4 +1,4 @@
-# $Id: configure.in,v 1.270 2001/03/28 04:37:06 djm Exp $
+# $Id: configure.in,v 1.271 2001/03/28 11:05:27 djm Exp $
 
 AC_INIT(ssh.c)
 
@@ -595,22 +595,22 @@
 			# Try to use $ssldir/lib if it exists, otherwise 
 			# $ssldir
 			if test -d "$ssldir/lib" ; then
-				LDFLAGS="$saved_LDFLAGS -L$ssldir/lib"
+				LDFLAGS="-L$ssldir/lib $saved_LDFLAGS"
 				if test ! -z "$need_dash_r" ; then
-					LDFLAGS="$LDFLAGS -R$ssldir/lib"
+					LDFLAGS="-R$ssldir/lib $LDFLAGS"
 				fi
 			else
-				LDFLAGS="$saved_LDFLAGS -L$ssldir"
+				LDFLAGS="-L$ssldir $saved_LDFLAGS"
 				if test ! -z "$need_dash_r" ; then
-					LDFLAGS="$LDFLAGS -R$ssldir"
+					LDFLAGS="-R$ssldir $LDFLAGS"
 				fi
 			fi
 			# Try to use $ssldir/include if it exists, otherwise 
 			# $ssldir
 			if test -d "$ssldir/include" ; then
-				CPPFLAGS="$saved_CPPFLAGS -I$ssldir/include"
+				CPPFLAGS="-I$ssldir/include $saved_CPPFLAGS"
 			else
-				CPPFLAGS="$saved_CPPFLAGS -I$ssldir"
+				CPPFLAGS="-I$ssldir $saved_CPPFLAGS"
 			fi
 		fi
 
@@ -657,22 +657,22 @@
 		# Try to use $ssldir/lib if it exists, otherwise 
 		# $ssldir
 		if test -d "$ssldir/lib" ; then
-			LDFLAGS="$saved_LDFLAGS -L$ssldir/lib"
+			LDFLAGS="-L$ssldir/lib $saved_LDFLAGS"
 			if test ! -z "$need_dash_r" ; then
-				LDFLAGS="$LDFLAGS -R$ssldir/lib"
+				LDFLAGS="-R$ssldir/lib $LDFLAGS"
 			fi
 		else
-			LDFLAGS="$saved_LDFLAGS -L$ssldir"
+			LDFLAGS="-L$ssldir $saved_LDFLAGS"
 			if test ! -z "$need_dash_r" ; then
-				LDFLAGS="$LDFLAGS -R$ssldir"
+				LDFLAGS="-R$ssldir $LDFLAGS"
 			fi
 		fi
 		# Try to use $ssldir/include if it exists, otherwise 
 		# $ssldir
 		if test -d "$ssldir/include" ; then
-			CPPFLAGS="$saved_CPPFLAGS -I$ssldir/include"
+			CPPFLAGS="-I$ssldir/include $saved_CPPFLAGS"
 		else
-			CPPFLAGS="$saved_CPPFLAGS -I$ssldir"
+			CPPFLAGS="-I$ssldir $saved_CPPFLAGS"
 		fi
 	fi
 fi
diff --git a/openbsd-compat/glob.c b/openbsd-compat/glob.c
index 5ed2862..cca819d 100644
--- a/openbsd-compat/glob.c
+++ b/openbsd-compat/glob.c
@@ -56,7 +56,7 @@
 #if 0
 static char sccsid[] = "@(#)glob.c	8.3 (Berkeley) 10/13/93";
 #else
-static char rcsid[] = "$OpenBSD: glob.c,v 1.10 2001/03/19 13:45:30 millert Exp $";
+static char rcsid[] = "$OpenBSD: glob.c,v 1.13 2001/03/28 08:00:00 deraadt Exp $";
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -137,13 +137,10 @@
 
 
 static int	 compare __P((const void *, const void *));
-static void	 g_Ctoc __P((const Char *, char *));
+static int	 g_Ctoc __P((const Char *, char *, char *));
 static int	 g_lstat __P((Char *, struct stat *, glob_t *));
 static DIR	*g_opendir __P((Char *, glob_t *));
 static Char	*g_strchr __P((Char *, int));
-#ifdef notdef
-static Char	*g_strcat __P((Char *, const Char *));
-#endif
 static int	 g_stat __P((Char *, struct stat *, glob_t *));
 static int	 glob0 __P((const Char *, glob_t *));
 static int	 glob1 __P((Char *, glob_t *, size_t *));
@@ -184,8 +181,8 @@
 	bufnext = patbuf;
 	bufend = bufnext + MAXPATHLEN;
 	if (flags & GLOB_NOESCAPE)
-	    while (bufnext < bufend && (c = *patnext++) != EOS)
-		    *bufnext++ = c;
+		while (bufnext < bufend && (c = *patnext++) != EOS)
+			*bufnext++ = c;
 	else {
 		/* Protect the quoted characters. */
 		while (bufnext < bufend && (c = *patnext++) != EOS)
@@ -279,7 +276,7 @@
 		return 0;
 	}
 
-	for (i = 0, pl = pm = ptr; pm <= pe; pm++)
+	for (i = 0, pl = pm = ptr; pm <= pe; pm++) {
 		switch (*pm) {
 		case LBRACKET:
 			/* Ignore everything between [] */
@@ -300,8 +297,8 @@
 
 		case RBRACE:
 			if (i) {
-			    i--;
-			    break;
+				i--;
+				break;
 			}
 			/* FALLTHROUGH */
 		case COMMA:
@@ -332,6 +329,7 @@
 		default:
 			break;
 		}
+	}
 	*rv = 0;
 	return 0;
 }
@@ -464,7 +462,7 @@
 			 * to avoid exponential behavior
 			 */
 			if (bufnext == patbuf || bufnext[-1] != M_ALL)
-			    *bufnext++ = M_ALL;
+				*bufnext++ = M_ALL;
 			break;
 		default:
 			*bufnext++ = CHAR(c);
@@ -546,8 +544,8 @@
 				return(0);
 
 			if (((pglob->gl_flags & GLOB_MARK) &&
-			    pathend[-1] != SEP) && (S_ISDIR(sb.st_mode)
-			    || (S_ISLNK(sb.st_mode) &&
+			    pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) ||
+			    (S_ISLNK(sb.st_mode) &&
 			    (g_stat(pathbuf, &sb, pglob) == 0) &&
 			    S_ISDIR(sb.st_mode)))) {
 				*pathend++ = SEP;
@@ -571,7 +569,8 @@
 			pattern = p;
 			while (*pattern == SEP)
 				*pathend++ = *pattern++;
-		} else			/* Need expansion, recurse. */
+		} else
+			/* Need expansion, recurse. */
 			return(glob3(pathbuf, pathend, pattern, p, pglob,
 			    limitp));
 	}
@@ -603,10 +602,11 @@
 	if ((dirp = g_opendir(pathbuf, pglob)) == NULL) {
 		/* TODO: don't call for ENOENT or ENOTDIR? */
 		if (pglob->gl_errfunc) {
-			g_Ctoc(pathbuf, buf);
+			if (g_Ctoc(pathbuf, buf, buf+sizeof(buf)))
+				return(GLOB_ABORTED);
 			if (pglob->gl_errfunc(buf, errno) ||
 			    pglob->gl_flags & GLOB_ERR)
-				return (GLOB_ABORTED);
+				return(GLOB_ABORTED);
 		}
 		return(0);
 	}
@@ -695,7 +695,10 @@
 	len = (size_t)(p - path);
 	*limitp += len;
 	if ((copy = malloc(len)) != NULL) {
-		g_Ctoc(path, copy);
+		if (g_Ctoc(path, copy, copy+len+1)) {
+			free(copy);
+			return(GLOB_NOSPACE);
+		}
 		pathv[pglob->gl_offs + pglob->gl_pathc++] = copy;
 	}
 	pathv[pglob->gl_offs + pglob->gl_pathc] = NULL;
@@ -730,7 +733,8 @@
 			do
 			    if (match(name, pat, patend))
 				    return(1);
-			while (*name++ != EOS);
+			while (*name++ != EOS)
+				;
 			return(0);
 		case M_ONE:
 			if (*name++ == EOS)
@@ -788,8 +792,10 @@
 
 	if (!*str)
 		strcpy(buf, ".");
-	else
-		g_Ctoc(str, buf);
+	else {
+		if (g_Ctoc(str, buf, buf+sizeof(buf)))
+			return(NULL);
+	}
 
 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
 		return((*pglob->gl_opendir)(buf));
@@ -805,7 +811,8 @@
 {
 	char buf[MAXPATHLEN];
 
-	g_Ctoc(fn, buf);
+	if (g_Ctoc(fn, buf, buf+sizeof(buf)))
+		return(-1);
 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
 		return((*pglob->gl_lstat)(buf, sb));
 	return(lstat(buf, sb));
@@ -819,7 +826,8 @@
 {
 	char buf[MAXPATHLEN];
 
-	g_Ctoc(fn, buf);
+	if (g_Ctoc(fn, buf, buf+sizeof(buf)))
+		return(-1);
 	if (pglob->gl_flags & GLOB_ALTDIRFUNC)
 		return((*pglob->gl_stat)(buf, sb));
 	return(stat(buf, sb));
@@ -837,33 +845,18 @@
 	return (NULL);
 }
 
-#ifdef notdef
-static Char *
-g_strcat(dst, src)
-	Char *dst;
-	const Char* src;
-{
-	Char *sdst = dst;
-
-	while (*dst++)
-		continue;
-	--dst;
-	while((*dst++ = *src++) != EOS)
-		continue;
-
-	return (sdst);
-}
-#endif
-
-static void
-g_Ctoc(str, buf)
+static int
+g_Ctoc(str, buf, ebuf)
 	register const Char *str;
-	char *buf;
+	char *buf, *ebuf;
 {
 	register char *dc;
 
-	for (dc = buf; (*dc++ = *str++) != EOS;)
+	for (dc = buf; dc < ebuf && (*dc++ = *str++) != EOS;)
 		continue;
+	if (dc >= ebuf)
+		return (1);
+	return (0);
 }
 
 #ifdef DEBUG