2011-09-05 00:29:12 +04:00
|
|
|
/* $NetBSD: look.c,v 1.15 2011/09/04 20:29:32 joerg Exp $ */
|
1994-12-23 04:10:55 +03:00
|
|
|
|
1993-04-09 16:58:42 +04:00
|
|
|
/*-
|
1994-12-23 04:10:55 +03:00
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-04-09 16:58:42 +04:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* David Hitz of Auspex Systems, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-04-09 16:58:42 +04:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-19 08:21:27 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-04-09 16:58:42 +04:00
|
|
|
#ifndef lint
|
2008-07-21 18:19:20 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1991, 1993\
|
|
|
|
The Regents of the University of California. All rights reserved.");
|
1993-04-09 16:58:42 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1994-12-23 04:10:55 +03:00
|
|
|
#if 0
|
1995-09-01 02:36:32 +04:00
|
|
|
static char sccsid[] = "@(#)look.c 8.2 (Berkeley) 5/4/95";
|
1994-12-23 04:10:55 +03:00
|
|
|
#endif
|
2011-09-05 00:29:12 +04:00
|
|
|
__RCSID("$NetBSD: look.c,v 1.15 2011/09/04 20:29:32 joerg Exp $");
|
1993-04-09 16:58:42 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* look -- find lines in a sorted list.
|
|
|
|
*
|
|
|
|
* The man page said that TABs and SPACEs participate in -d comparisons.
|
|
|
|
* In fact, they were ignored. This implements historic practice, not
|
|
|
|
* the manual page.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
1994-12-23 04:10:55 +03:00
|
|
|
|
1995-09-01 02:36:32 +04:00
|
|
|
#include <ctype.h>
|
1993-04-09 16:58:42 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
1995-09-01 02:36:32 +04:00
|
|
|
#include <limits.h>
|
1993-04-09 16:58:42 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1993-10-13 21:33:45 +03:00
|
|
|
#include <unistd.h>
|
1994-12-23 04:10:55 +03:00
|
|
|
#include <err.h>
|
1995-09-01 02:36:32 +04:00
|
|
|
|
1993-04-09 16:58:42 +04:00
|
|
|
#include "pathnames.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FOLD and DICT convert characters to a normal form for comparison,
|
|
|
|
* according to the user specified flags.
|
|
|
|
*
|
|
|
|
* DICT expects integers because it uses a non-character value to
|
|
|
|
* indicate a character which should not participate in comparisons.
|
|
|
|
*/
|
|
|
|
#define EQUAL 0
|
|
|
|
#define GREATER 1
|
|
|
|
#define LESS (-1)
|
|
|
|
#define NO_COMPARE (-2)
|
|
|
|
|
|
|
|
#define FOLD(c) (isascii(c) && isupper(c) ? tolower(c) : (c))
|
|
|
|
#define DICT(c) (isascii(c) && isalnum(c) ? (c) : NO_COMPARE)
|
|
|
|
|
2011-09-05 00:29:12 +04:00
|
|
|
static int dflag, fflag;
|
1993-04-09 16:58:42 +04:00
|
|
|
|
2011-09-05 00:29:12 +04:00
|
|
|
static char *binary_search(char *, char *, char *);
|
|
|
|
static int compare(char *, char *, char *);
|
|
|
|
static char *linear_search(char *, char *, char *);
|
|
|
|
static int look(char *, char *, char *);
|
|
|
|
static void print_from(char *, char *, char *);
|
|
|
|
__dead static void usage(void);
|
1993-04-09 16:58:42 +04:00
|
|
|
|
1994-12-23 04:10:55 +03:00
|
|
|
int
|
2011-09-05 00:29:12 +04:00
|
|
|
main(int argc, char *argv[])
|
1993-04-09 16:58:42 +04:00
|
|
|
{
|
|
|
|
struct stat sb;
|
1994-12-23 04:10:55 +03:00
|
|
|
int ch, fd, termchar;
|
2009-04-12 18:01:20 +04:00
|
|
|
char *back, *front, *string, *p;
|
|
|
|
const char *file;
|
2009-04-26 19:55:50 +04:00
|
|
|
size_t len;
|
1993-04-09 16:58:42 +04:00
|
|
|
|
1997-10-19 08:21:27 +04:00
|
|
|
string = NULL;
|
1993-04-09 16:58:42 +04:00
|
|
|
file = _PATH_WORDS;
|
1994-12-23 04:10:55 +03:00
|
|
|
termchar = '\0';
|
1997-10-19 08:21:27 +04:00
|
|
|
while ((ch = getopt(argc, argv, "dft:")) != -1)
|
1993-04-09 16:58:42 +04:00
|
|
|
switch(ch) {
|
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
fflag = 1;
|
|
|
|
break;
|
1994-12-23 04:10:55 +03:00
|
|
|
case 't':
|
|
|
|
termchar = *optarg;
|
|
|
|
break;
|
1993-04-09 16:58:42 +04:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
switch (argc) {
|
|
|
|
case 2: /* Don't set -df for user. */
|
|
|
|
string = *argv++;
|
|
|
|
file = *argv;
|
|
|
|
break;
|
|
|
|
case 1: /* But set -df by default. */
|
|
|
|
dflag = fflag = 1;
|
|
|
|
string = *argv;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
1994-12-23 04:10:55 +03:00
|
|
|
if (termchar != '\0' && (p = strchr(string, termchar)) != NULL)
|
|
|
|
*++p = '\0';
|
|
|
|
|
|
|
|
if ((fd = open(file, O_RDONLY, 0)) < 0 || fstat(fd, &sb))
|
|
|
|
err(2, "%s", file);
|
2009-04-26 19:55:50 +04:00
|
|
|
len = (size_t)sb.st_size;
|
|
|
|
if ((off_t)len != sb.st_size) {
|
|
|
|
errno = EFBIG;
|
|
|
|
err(2, "%s", file);
|
|
|
|
}
|
|
|
|
if ((front = mmap(NULL, len,
|
1998-02-20 12:26:00 +03:00
|
|
|
PROT_READ, MAP_FILE|MAP_SHARED, fd, (off_t)0)) == NULL)
|
1994-12-23 04:10:55 +03:00
|
|
|
err(2, "%s", file);
|
2009-04-26 19:55:50 +04:00
|
|
|
back = front + len;
|
1993-04-09 16:58:42 +04:00
|
|
|
exit(look(string, front, back));
|
|
|
|
}
|
|
|
|
|
2011-09-05 00:29:12 +04:00
|
|
|
static int
|
|
|
|
look(char *string, char *front, char *back)
|
1993-04-09 16:58:42 +04:00
|
|
|
{
|
1997-10-19 08:21:27 +04:00
|
|
|
int ch;
|
|
|
|
char *readp, *writep;
|
1993-04-09 16:58:42 +04:00
|
|
|
|
|
|
|
/* Reformat string string to avoid doing it multiple times later. */
|
1997-10-19 08:21:27 +04:00
|
|
|
for (readp = writep = string; (ch = *readp++) != 0; ) {
|
1993-04-09 16:58:42 +04:00
|
|
|
if (fflag)
|
|
|
|
ch = FOLD(ch);
|
|
|
|
if (dflag)
|
|
|
|
ch = DICT(ch);
|
|
|
|
if (ch != NO_COMPARE)
|
|
|
|
*(writep++) = ch;
|
|
|
|
}
|
|
|
|
*writep = '\0';
|
|
|
|
|
|
|
|
front = binary_search(string, front, back);
|
|
|
|
front = linear_search(string, front, back);
|
|
|
|
|
|
|
|
if (front)
|
|
|
|
print_from(string, front, back);
|
|
|
|
return (front ? 0 : 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Binary search for "string" in memory between "front" and "back".
|
|
|
|
*
|
|
|
|
* This routine is expected to return a pointer to the start of a line at
|
|
|
|
* *or before* the first word matching "string". Relaxing the constraint
|
|
|
|
* this way simplifies the algorithm.
|
|
|
|
*
|
|
|
|
* Invariants:
|
|
|
|
* front points to the beginning of a line at or before the first
|
|
|
|
* matching string.
|
|
|
|
*
|
|
|
|
* back points to the beginning of a line at or after the first
|
|
|
|
* matching line.
|
|
|
|
*
|
|
|
|
* Base of the Invariants.
|
|
|
|
* front = NULL;
|
|
|
|
* back = EOF;
|
|
|
|
*
|
|
|
|
* Advancing the Invariants:
|
|
|
|
*
|
|
|
|
* p = first newline after halfway point from front to back.
|
|
|
|
*
|
|
|
|
* If the string at "p" is not greater than the string to match,
|
|
|
|
* p is the new front. Otherwise it is the new back.
|
|
|
|
*
|
|
|
|
* Termination:
|
|
|
|
*
|
|
|
|
* The definition of the routine allows it return at any point,
|
|
|
|
* since front is always at or before the line to print.
|
|
|
|
*
|
|
|
|
* In fact, it returns when the chosen "p" equals "back". This
|
|
|
|
* implies that there exists a string is least half as long as
|
|
|
|
* (back - front), which in turn implies that a linear search will
|
|
|
|
* be no more expensive than the cost of simply printing a string or two.
|
|
|
|
*
|
|
|
|
* Trying to continue with binary search at this point would be
|
|
|
|
* more trouble than it's worth.
|
|
|
|
*/
|
|
|
|
#define SKIP_PAST_NEWLINE(p, back) \
|
|
|
|
while (p < back && *p++ != '\n');
|
|
|
|
|
2011-09-05 00:29:12 +04:00
|
|
|
static char *
|
|
|
|
binary_search(char *string, char *front, char *back)
|
1993-04-09 16:58:42 +04:00
|
|
|
{
|
1997-10-19 08:21:27 +04:00
|
|
|
char *p;
|
1993-04-09 16:58:42 +04:00
|
|
|
|
|
|
|
p = front + (back - front) / 2;
|
|
|
|
SKIP_PAST_NEWLINE(p, back);
|
|
|
|
|
1994-12-23 04:10:55 +03:00
|
|
|
/*
|
|
|
|
* If the file changes underneath us, make sure we don't
|
|
|
|
* infinitely loop.
|
|
|
|
*/
|
|
|
|
while (p < back && back > front) {
|
1993-04-09 16:58:42 +04:00
|
|
|
if (compare(string, p, back) == GREATER)
|
|
|
|
front = p;
|
|
|
|
else
|
|
|
|
back = p;
|
|
|
|
p = front + (back - front) / 2;
|
|
|
|
SKIP_PAST_NEWLINE(p, back);
|
|
|
|
}
|
|
|
|
return (front);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the first line that starts with string, linearly searching from front
|
|
|
|
* to back.
|
|
|
|
*
|
|
|
|
* Return NULL for no such line.
|
|
|
|
*
|
|
|
|
* This routine assumes:
|
|
|
|
*
|
|
|
|
* o front points at the first character in a line.
|
|
|
|
* o front is before or at the first line to be printed.
|
|
|
|
*/
|
2011-09-05 00:29:12 +04:00
|
|
|
static char *
|
|
|
|
linear_search(char *string, char *front, char *back)
|
1993-04-09 16:58:42 +04:00
|
|
|
{
|
|
|
|
while (front < back) {
|
|
|
|
switch (compare(string, front, back)) {
|
|
|
|
case EQUAL: /* Found it. */
|
|
|
|
return (front);
|
|
|
|
break;
|
|
|
|
case LESS: /* No such string. */
|
|
|
|
return (NULL);
|
|
|
|
break;
|
|
|
|
case GREATER: /* Keep going. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
SKIP_PAST_NEWLINE(front, back);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print as many lines as match string, starting at front.
|
|
|
|
*/
|
2011-09-05 00:29:12 +04:00
|
|
|
static void
|
|
|
|
print_from(char *string, char *front, char *back)
|
1993-04-09 16:58:42 +04:00
|
|
|
{
|
|
|
|
for (; front < back && compare(string, front, back) == EQUAL; ++front) {
|
|
|
|
for (; front < back && *front != '\n'; ++front)
|
|
|
|
if (putchar(*front) == EOF)
|
1994-12-23 04:10:55 +03:00
|
|
|
err(2, "stdout");
|
1993-04-09 16:58:42 +04:00
|
|
|
if (putchar('\n') == EOF)
|
1994-12-23 04:10:55 +03:00
|
|
|
err(2, "stdout");
|
1993-04-09 16:58:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return LESS, GREATER, or EQUAL depending on how the string1 compares with
|
|
|
|
* string2 (s1 ??? s2).
|
|
|
|
*
|
|
|
|
* o Matches up to len(s1) are EQUAL.
|
|
|
|
* o Matches up to len(s2) are GREATER.
|
|
|
|
*
|
|
|
|
* Compare understands about the -f and -d flags, and treats comparisons
|
|
|
|
* appropriately.
|
|
|
|
*
|
|
|
|
* The string "s1" is null terminated. The string s2 is '\n' terminated (or
|
|
|
|
* "back" terminated).
|
|
|
|
*/
|
2011-09-05 00:29:12 +04:00
|
|
|
static int
|
|
|
|
compare(char *s1, char *s2, char *back)
|
1993-04-09 16:58:42 +04:00
|
|
|
{
|
1997-10-19 08:21:27 +04:00
|
|
|
int ch;
|
1993-04-09 16:58:42 +04:00
|
|
|
|
|
|
|
for (; *s1 && s2 < back && *s2 != '\n'; ++s1, ++s2) {
|
|
|
|
ch = *s2;
|
|
|
|
if (fflag)
|
|
|
|
ch = FOLD(ch);
|
|
|
|
if (dflag)
|
|
|
|
ch = DICT(ch);
|
|
|
|
|
|
|
|
if (ch == NO_COMPARE) {
|
|
|
|
++s2; /* Ignore character in comparison. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*s1 != ch)
|
|
|
|
return (*s1 < ch ? LESS : GREATER);
|
|
|
|
}
|
|
|
|
return (*s1 ? GREATER : EQUAL);
|
|
|
|
}
|
|
|
|
|
2011-09-05 00:29:12 +04:00
|
|
|
static void
|
|
|
|
usage(void)
|
1993-04-09 16:58:42 +04:00
|
|
|
{
|
1994-12-23 04:10:55 +03:00
|
|
|
(void)fprintf(stderr, "usage: look [-df] [-t char] string [file]\n");
|
1993-04-09 16:58:42 +04:00
|
|
|
exit(2);
|
|
|
|
}
|