freebsd-dev/usr.bin/look/look.c

349 lines
9.1 KiB
C
Raw Normal View History

1994-05-27 12:33:43 +00:00
/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* 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.
*/
#ifndef lint
static const char copyright[] =
1994-05-27 12:33:43 +00:00
"@(#) Copyright (c) 1991, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif /* not lint */
#ifndef lint
#if 0
1997-03-11 13:43:33 +00:00
static char sccsid[] = "@(#)look.c 8.2 (Berkeley) 5/4/95";
#endif
1994-05-27 12:33:43 +00:00
#endif /* not lint */
2002-06-30 05:25:07 +00:00
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
1994-05-27 12:33:43 +00:00
/*
* look -- find lines in a sorted list.
1995-05-30 06:41:30 +00:00
*
1994-05-27 12:33:43 +00:00
* 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>
#include <err.h>
1994-05-27 12:33:43 +00:00
#include <errno.h>
#include <fcntl.h>
1997-03-11 13:43:33 +00:00
#include <limits.h>
#include <locale.h>
1994-05-27 12:33:43 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
1997-03-11 13:43:33 +00:00
#include <unistd.h>
#include <wchar.h>
#include <wctype.h>
1997-03-11 13:43:33 +00:00
1994-05-27 12:33:43 +00:00
#include "pathnames.h"
2002-04-28 12:39:12 +00:00
static char _path_words[] = _PATH_WORDS;
1994-05-27 12:33:43 +00:00
#define EQUAL 0
#define GREATER 1
#define LESS (-1)
int dflag, fflag;
char *binary_search(wchar_t *, unsigned char *, unsigned char *);
int compare(wchar_t *, unsigned char *, unsigned char *);
char *linear_search(wchar_t *, unsigned char *, unsigned char *);
int look(wchar_t *, unsigned char *, unsigned char *);
wchar_t *prepkey(const char *, wchar_t);
void print_from(wchar_t *, unsigned char *, unsigned char *);
1994-05-27 12:33:43 +00:00
2002-03-22 01:22:50 +00:00
static void usage(void);
1994-05-27 12:33:43 +00:00
int
main(int argc, char *argv[])
1994-05-27 12:33:43 +00:00
{
struct stat sb;
int ch, fd, match;
wchar_t termchar;
unsigned char *back, *front;
unsigned const char *file;
wchar_t *key;
(void) setlocale(LC_CTYPE, "");
1994-05-27 12:33:43 +00:00
2002-04-28 12:39:12 +00:00
file = _path_words;
termchar = L'\0';
while ((ch = getopt(argc, argv, "dft:")) != -1)
1994-05-27 12:33:43 +00:00
switch(ch) {
case 'd':
dflag = 1;
break;
case 'f':
fflag = 1;
break;
case 't':
if (mbrtowc(&termchar, optarg, MB_LEN_MAX, NULL) !=
strlen(optarg))
errx(2, "invalid termination character");
1994-05-27 12:33:43 +00:00
break;
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
if (argc == 0)
1994-05-27 12:33:43 +00:00
usage();
if (argc == 1) /* But set -df by default. */
dflag = fflag = 1;
key = prepkey(*argv++, termchar);
if (argc >= 2)
file = *argv++;
1994-05-27 12:33:43 +00:00
match = 1;
do {
if ((fd = open(file, O_RDONLY, 0)) < 0 || fstat(fd, &sb))
err(2, "%s", file);
if (sb.st_size > SIZE_T_MAX)
errx(2, "%s: %s", file, strerror(EFBIG));
if ((front = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_SHARED, fd, (off_t)0)) == MAP_FAILED)
err(2, "%s", file);
back = front + sb.st_size;
match *= (look(key, front, back));
close(fd);
} while (argc-- > 2 && (file = *argv++));
exit(match);
1994-05-27 12:33:43 +00:00
}
wchar_t *
prepkey(const char *string, wchar_t termchar)
1994-05-27 12:33:43 +00:00
{
const char *readp;
wchar_t *key, *writep;
wchar_t ch;
size_t clen;
1994-05-27 12:33:43 +00:00
/*
* Reformat search string and convert to wide character representation
* to avoid doing it multiple times later.
*/
if ((key = malloc(sizeof(wchar_t) * (strlen(string) + 1))) == NULL)
err(2, NULL);
readp = string;
writep = key;
while ((clen = mbrtowc(&ch, readp, MB_LEN_MAX, NULL)) != 0) {
if (clen == (size_t)-1 || clen == (size_t)-2)
errc(2, EILSEQ, NULL);
1994-05-27 12:33:43 +00:00
if (fflag)
ch = towlower(ch);
if (!dflag || iswalnum(ch))
*writep++ = ch;
readp += clen;
1994-05-27 12:33:43 +00:00
}
*writep = L'\0';
if (termchar != L'\0' && (writep = wcschr(key, termchar)) != NULL)
*++writep = L'\0';
return (key);
}
int
look(wchar_t *string, unsigned char *front, unsigned char *back)
{
1994-05-27 12:33:43 +00:00
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".
1995-05-30 06:41:30 +00:00
*
1994-05-27 12:33:43 +00:00
* 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.
1995-05-30 06:41:30 +00:00
*
1994-05-27 12:33:43 +00:00
* Invariants:
1995-05-30 06:41:30 +00:00
* front points to the beginning of a line at or before the first
1994-05-27 12:33:43 +00:00
* matching string.
1995-05-30 06:41:30 +00:00
*
* back points to the beginning of a line at or after the first
1994-05-27 12:33:43 +00:00
* matching line.
1995-05-30 06:41:30 +00:00
*
1994-05-27 12:33:43 +00:00
* Base of the Invariants.
1995-05-30 06:41:30 +00:00
* front = NULL;
1994-05-27 12:33:43 +00:00
* back = EOF;
1995-05-30 06:41:30 +00:00
*
1994-05-27 12:33:43 +00:00
* Advancing the Invariants:
1995-05-30 06:41:30 +00:00
*
1994-05-27 12:33:43 +00:00
* p = first newline after halfway point from front to back.
1995-05-30 06:41:30 +00:00
*
* If the string at "p" is not greater than the string to match,
1994-05-27 12:33:43 +00:00
* p is the new front. Otherwise it is the new back.
1995-05-30 06:41:30 +00:00
*
1994-05-27 12:33:43 +00:00
* Termination:
1995-05-30 06:41:30 +00:00
*
* The definition of the routine allows it return at any point,
1994-05-27 12:33:43 +00:00
* since front is always at or before the line to print.
1995-05-30 06:41:30 +00:00
*
* 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
1994-05-27 12:33:43 +00:00
* be no more expensive than the cost of simply printing a string or two.
1995-05-30 06:41:30 +00:00
*
* Trying to continue with binary search at this point would be
1994-05-27 12:33:43 +00:00
* more trouble than it's worth.
*/
#define SKIP_PAST_NEWLINE(p, back) \
while (p < back && *p++ != '\n');
char *
binary_search(wchar_t *string, unsigned char *front, unsigned char *back)
1994-05-27 12:33:43 +00:00
{
unsigned char *p;
1994-05-27 12:33:43 +00:00
p = front + (back - front) / 2;
SKIP_PAST_NEWLINE(p, back);
/*
* If the file changes underneath us, make sure we don't
* infinitely loop.
*/
while (p < back && back > front) {
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.
1995-05-30 06:41:30 +00:00
*
1994-05-27 12:33:43 +00:00
* Return NULL for no such line.
1995-05-30 06:41:30 +00:00
*
1994-05-27 12:33:43 +00:00
* This routine assumes:
1995-05-30 06:41:30 +00:00
*
* o front points at the first character in a line.
1994-05-27 12:33:43 +00:00
* o front is before or at the first line to be printed.
*/
char *
linear_search(wchar_t *string, unsigned char *front, unsigned char *back)
1994-05-27 12:33:43 +00:00
{
while (front < back) {
switch (compare(string, front, back)) {
case EQUAL: /* Found it. */
return (front);
case LESS: /* No such string. */
return (NULL);
case GREATER: /* Keep going. */
break;
}
SKIP_PAST_NEWLINE(front, back);
}
return (NULL);
}
/*
* Print as many lines as match string, starting at front.
*/
1995-05-30 06:41:30 +00:00
void
print_from(wchar_t *string, unsigned char *front, unsigned char *back)
1994-05-27 12:33:43 +00:00
{
for (; front < back && compare(string, front, back) == EQUAL; ++front) {
for (; front < back && *front != '\n'; ++front)
if (putchar(*front) == EOF)
err(2, "stdout");
1994-05-27 12:33:43 +00:00
if (putchar('\n') == EOF)
err(2, "stdout");
1994-05-27 12:33:43 +00:00
}
}
/*
* Return LESS, GREATER, or EQUAL depending on how the string1 compares with
* string2 (s1 ??? s2).
1995-05-30 06:41:30 +00:00
*
* o Matches up to len(s1) are EQUAL.
1994-05-27 12:33:43 +00:00
* o Matches up to len(s2) are GREATER.
1995-05-30 06:41:30 +00:00
*
1994-05-27 12:33:43 +00:00
* Compare understands about the -f and -d flags, and treats comparisons
* appropriately.
1995-05-30 06:41:30 +00:00
*
1994-05-27 12:33:43 +00:00
* The string "s1" is null terminated. The string s2 is '\n' terminated (or
* "back" terminated).
*/
int
compare(wchar_t *s1, unsigned char *s2, unsigned char *back)
1994-05-27 12:33:43 +00:00
{
wchar_t ch1, ch2;
size_t len2;
for (; *s1 && s2 < back && *s2 != '\n'; ++s1, s2 += len2) {
ch1 = *s1;
len2 = mbrtowc(&ch2, s2, back - s2, NULL);
if (len2 == (size_t)-1 || len2 == (size_t)-2) {
ch2 = *s2;
len2 = 1;
}
1994-05-27 12:33:43 +00:00
if (fflag)
ch2 = towlower(ch2);
if (dflag && !iswalnum(ch2)) {
/* Ignore character in comparison. */
--s1;
1994-05-27 12:33:43 +00:00
continue;
}
if (ch1 != ch2)
return (ch1 < ch2 ? LESS : GREATER);
1994-05-27 12:33:43 +00:00
}
return (*s1 ? GREATER : EQUAL);
}
static void
usage(void)
1994-05-27 12:33:43 +00:00
{
(void)fprintf(stderr, "usage: look [-df] [-t char] string [file ...]\n");
1994-05-27 12:33:43 +00:00
exit(2);
}