PERFORCE change 144082 for review

Gabor Kovesdan gabor at FreeBSD.org
Wed Jun 25 10:29:50 UTC 2008


http://perforce.freebsd.org/chv.cgi?CH=144082

Change 144082 by gabor at gabor_server on 2008/06/25 10:29:05

	- Remove the mmap code and apply some possible cleanups after the
	  removal. Using mmap is completely unnecessary as it doesn't bring us
	  any deliverables, just makes the code more complicated and
	  involves more cases.
	
	  Suggested by: ache

Affected files ...

.. //depot/projects/soc2008/gabor_textproc/grep/Makefile#7 edit
.. //depot/projects/soc2008/gabor_textproc/grep/binary.c#13 delete
.. //depot/projects/soc2008/gabor_textproc/grep/file.c#11 edit
.. //depot/projects/soc2008/gabor_textproc/grep/grep.c#45 edit
.. //depot/projects/soc2008/gabor_textproc/grep/grep.h#25 edit
.. //depot/projects/soc2008/gabor_textproc/grep/mmfile.c#5 delete
.. //depot/projects/soc2008/gabor_textproc/grep/util.c#39 edit

Differences ...

==== //depot/projects/soc2008/gabor_textproc/grep/Makefile#7 (text+ko) ====

@@ -2,7 +2,7 @@
 #	$OpenBSD: Makefile,v 1.6 2003/06/25 15:00:04 millert Exp $
 
 PROG=	grep
-SRCS=	binary.c file.c grep.c mmfile.c queue.c util.c
+SRCS=	file.c grep.c queue.c util.c
 LINKS=	${BINDIR}/grep ${BINDIR}/egrep \
 	${BINDIR}/grep ${BINDIR}/fgrep \
 	${BINDIR}/grep ${BINDIR}/zgrep \

==== //depot/projects/soc2008/gabor_textproc/grep/file.c#11 (text+ko) ====

@@ -42,21 +42,40 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <wchar.h>
+#include <wctype.h>
 #include <zlib.h>
 
 #include "grep.h"
 
 static char	 fname[MAXPATHLEN];
 
-#define FILE_STDIO	0
-#define FILE_MMAP	1
+#define iswbinary(ch)	(!iswspace((ch)) && iswcntrl((ch)))
+
+int
+bin_file(struct file *f)
+{
+	wint_t	 ch = L'\0';
+	size_t	 i;
+	int	 ret = 0;
+
+	if (f->noseek)
+		return (0);
+
+	if (fseek(f->f, 0L, SEEK_SET) == -1)
+		return (0);
+
+	for (i = 0; (i <= BUFSIZ) && (ch != WEOF); i++) {
+		ch = fgetwc(f->f);
+		if (iswbinary(ch)) {
+			ret = 1;
+			break;
+		}
+	}
 
-struct file {
-	int		 type;
-	int		 noseek;
-	FILE		*f;
-	struct mmfile	*mmf;
-};
+	rewind(f->f);
+	return (ret);
+}
 
 struct file *
 grep_stdin_open(void)
@@ -67,9 +86,8 @@
 
 	f = grep_malloc(sizeof *f);
 
-	f->type = FILE_STDIO;
-	f->noseek = isatty(FILE_STDIO);
-	if ((f->f = fdopen(FILE_STDIO, "r")) != NULL)
+	f->noseek = isatty(STDIN_FILENO);
+	if ((f->f = fdopen(STDIN_FILENO, "r")) != NULL)
 		return (f);
 
 	free(f);
@@ -106,7 +124,6 @@
 			}
 			gzclose(gzf);
 			lseek(tempfd, 0L, SEEK_SET);
-			f->type = FILE_STDIO;
 			if ((f->f = fdopen(tempfd, "r")) != NULL)
 				return (f);
 			else
@@ -128,7 +145,6 @@
 			BZ2_bzReadClose(&bzerror, bzf);
 			fclose(file);
 			lseek(tempfd, 0L, SEEK_SET);
-			f->type = FILE_STDIO;
 			if ((f->f = fdopen(tempfd, "r")) != NULL)
 				return (f);
 			else
@@ -136,63 +152,9 @@
 		}
 	}
 
-	/* try mmap first; if it fails, try stdio */
-	if ((f->mmf = mmopen(fname, "r")) != NULL) {
-		f->type = FILE_MMAP;
-		return (f);
-	}
-	f->type = FILE_STDIO;
 	if ((f->f = fopen(path, "r")) != NULL)
 		return (f);
 
 	free(f);
 	return (NULL);
 }
-
-int
-grep_bin_file(struct file *f)
-{
-	if (f->noseek)
-		return (0);
-
-	switch (f->type) {
-	case FILE_STDIO:
-		return (bin_file(f->f));
-	case FILE_MMAP:
-		return (mmbin_file(f->mmf));
-	default:
-		/* NOTREACHED */
-		errx(2, getstr(3));
-	}
-}
-
-char *
-grep_fgetln(struct file *f, size_t *l)
-{
-	switch (f->type) {
-	case FILE_STDIO:
-		return (fgetln(f->f, l));
-	case FILE_MMAP:
-		return (mmfgetln(f->mmf, l));
-	default:
-		/* NOTREACHED */
-		errx(2, getstr(3));
-	}
-}
-
-void
-grep_close(struct file *f)
-{
-	switch (f->type) {
-	case FILE_STDIO:
-		fclose(f->f);
-		break;
-	case FILE_MMAP:
-		mmclose(f->mmf);
-		break;
-	default:
-		/* NOTREACHED */
-		errx(2, getstr(3));
-	}
-	free(f);
-}

==== //depot/projects/soc2008/gabor_textproc/grep/grep.c#45 (text+ko) ====

@@ -540,7 +540,7 @@
 			break;
 		case 'u':
 		case MMAP_OPT:
-			/* default, compatibility */
+			/* noop, compatibility */
 			break;
 		case 'V':
 			printf(getstr(10));

==== //depot/projects/soc2008/gabor_textproc/grep/grep.h#25 (text+ko) ====

@@ -60,6 +60,12 @@
 #define LINK_EXPLICIT	1
 #define LINK_SKIP	2
 
+struct file {
+	int		 noseek;
+	FILE		*f;
+	struct mmfile	*mmf;
+};
+
 struct str {
 	size_t		 len;
 	int		 line_no;
@@ -115,26 +121,7 @@
 void	 printqueue(void);
 void	 clearqueue(void);
 
-/* mmfile.c */
-struct mmfile {
-	int	 fd;
-	size_t	 len;
-	char	*base, *end, *ptr;
-};
-
-struct mmfile	*mmopen(char *fn, char *mode);
-void		 mmclose(struct mmfile *mmf);
-char		*mmfgetln(struct mmfile *mmf, size_t *l);
-
 /* file.c */
-struct file;
-
+int		 bin_file(struct file * f);
 struct file	*grep_stdin_open(void);
 struct file	*grep_open(char *path);
-int		 grep_bin_file(struct file *f);
-char		*grep_fgetln(struct file *f, size_t *l);
-void		 grep_close(struct file *f);
-
-/* binary.c */
-int	 bin_file(FILE * f);
-int	 mmbin_file(struct mmfile *f);

==== //depot/projects/soc2008/gabor_textproc/grep/util.c#39 (text+ko) ====

@@ -147,9 +147,10 @@
 		return (0);
 	}
 
-	nottext = grep_bin_file(f);
+	nottext = bin_file(f);
 	if (nottext && binbehave == BINFILE_SKIP) {
-		grep_close(f);
+		fclose(f->f);
+		free(f);
 		return (0);
 	}
 
@@ -164,7 +165,7 @@
 		initqueue();
 	for (c = 0;  c == 0 || !(lflag || qflag); ) {
 		ln.off += ln.len + 1;
-		if ((ln.dat = grep_fgetln(f, &ln.len)) == NULL)
+		if ((ln.dat = fgetln(f->f, &ln.len)) == NULL)
 			break;
 		if (ln.len > 0 && ln.dat[ln.len - 1] == '\n')
 			--ln.len;
@@ -184,7 +185,8 @@
 	}
 	if (Bflag > 0)
 		clearqueue();
-	grep_close(f);
+	fclose(f->f);
+	free(f);
 
 	if (cflag) {
 		if (!hflag)


More information about the p4-projects mailing list