--- ext.c	2002-08-20 01:16:36.000000000 +0200
+++ /usr/src/mc-4.5.99a/src/ext.c	2002-08-21 05:16:14.000000000 +0200
@@ -300,7 +300,8 @@ regex_command (char *filename, char *act
     int view_at_line_number;
     char *include_target;
     int include_target_len;
-
+    int use_content = FALSE; /* 'content_string' contains good data */
+    
 #ifdef FILE_STDIN
     int file_supports_stdin = 1;
 #else
@@ -425,18 +426,15 @@ file as an example of how to write it.\n
 	                found = 1;
 	        }
 	    } else if (!strncmp (p, "type/", 5)) {
-		int islocal = vfs_file_is_local (filename);
 	        p += 5;
 
-	        if (islocal || file_supports_stdin) {
-	    	    char *pp;
-	    	    int hasread = use_file_to_check_type;
-
-		    if (asked_file || !use_file_to_check_type)
-			goto match_file_output;
+		if (asked_file)
+		    goto match_file_output;
 
-		    hasread = 0;
-	    	    if (islocal) {
+		if (use_file_to_check_type != 0) {
+		    int islocal = vfs_file_is_local (filename);
+		    
+		    if (islocal) {
 			char *tmp = name_quote (filename, 0);
 	    	        char *command =
 			    g_strconcat (FILE_CMD, tmp, NULL);
@@ -445,10 +443,8 @@ file as an example of how to write it.\n
 			g_free (tmp);
 	    	        g_free (command);
 	    	        if (f != NULL) {
-	    	            hasread = (fgets (content_string, 2047, f)
-	    	                != NULL);
-	    	    	    if (!hasread)
-	    	    	        content_string [0] = 0;
+	    	            if (fgets (content_string, sizeof (content_string),
+				    f) != NULL) use_content = TRUE;
 	    	    	    pclose (f);
 #ifdef SCO_FLAVOR
 	    	    	    /*
@@ -458,7 +454,7 @@ file as an example of how to write it.\n
 	    	    	    waitpid(-1,NULL,WNOHANG);
 #endif /* SCO_FLAVOR */
 	    	        }
-	    	    } else {
+	    	    } else if (file_supports_stdin != 0) {
 #ifdef _OS_NT
 			message (1, " Win32 ", " Unimplemented file prediction ");
 #else
@@ -475,42 +471,52 @@ file as an example of how to write it.\n
 			    (remotehandle, 8192, &p,"file", "file", "-", NULL);
 			    if (pipehandle != -1) {
 	    	                int i;
-	    	                while ((i = read (pipehandle, content_string
-	    	                     + hasread, 2047 - hasread)) > 0)
-	    	                    hasread += i;
+				/* fgets () reads N - 1 bytes */
+				int bytes_to_read = sizeof (content_string) - 1;
+				char *curp = content_string;
+				while ((i = read (pipehandle, curp,
+	    	                    		bytes_to_read)) > 0) {
+	    	                    curp += i;
+				    bytes_to_read -= i;
+				}
 	    	    	        mc_doublepclose (pipehandle, p);
-	    	    	        content_string [hasread] = 0;
+				if ((curp - 1) > (char *) &content_string) {
+				    *curp = '\0';
+				    use_content = TRUE;
+				}
 	    	            }
 	    	            mc_close (remotehandle);
 	    	        }
 #endif /* _OS_NT */
-	    	    }
-		    asked_file = 1;
-match_file_output:
-	    	    if (hasread) {
+		    }
+	    	    if (use_content != FALSE) {
+		        char *pp;
 	    	        if ((pp = strchr (content_string, '\n')) != 0)
 	    	    	    *pp = 0;
-	    	        if (islocal && !strncmp (content_string,
-	    	            filename, file_len)) {
-	    	    	    content_shift = file_len;
-	    	    	    if (content_string [content_shift] == ':')
-	    	    	        for (content_shift++;
-	    	    	            content_string [content_shift] == ' ';
-	    	    	            content_shift++);
-	    	        } else if (!islocal
-				   && !strncmp (content_string,
-						"standard input:", 15)) {
-	    	            for (content_shift = 15;
-	    	                content_string [content_shift] == ' ';
-	    	                content_shift++);
+	    	        if (islocal != 0) {
+			    if (!strncmp (content_string, filename, file_len))
+	    	    		content_shift = file_len;
+	    	        } else {
+			    if (!strncmp (content_string, "standard input",
+					sizeof ("standard input")))
+				content_shift = sizeof ("standard input");
 	    	        }
-	    		if (content_string &&
-	    		    regexp_match (p, content_string +
-	    		        content_shift, match_normal)){
-	    		    found = 1;
-	    		}
+			if (content_shift != 0 &&
+			    content_string [content_shift] == ':') {
+			    for (content_shift++;
+				content_string [content_shift] == ' ';
+				content_shift++);
+			}
+			
 	    	    }
-	        }
+		    asked_file = 1;
+		}
+match_file_output:
+		if ((use_content != FALSE) &&
+		    regexp_match (p, &content_string[content_shift],
+				match_normal)) {
+		    found = 1;
+		}
 	    } else if (!strncmp (p, "default/", 8)) {
 	        p += 8;
 	        found = 1;
