lobase

Linux port of OpenBSDs userland.
Log | Files | Refs | README

commit 4ff42884df002ccd1728e87d5b4ee7cbddb93cdd
parent 58964fc0db90ff29717abc82e4586d8cd633f3d8
Author: Duncaen <mail@duncano.de>
Date:   Fri, 19 May 2017 18:48:28 +0200

usr.bin/look: update to OPENBSD_6_1

Diffstat:
usr.bin/look/look.c | 90+++++++++++++++++++++++++++++++++----------------------------------------------
1 file changed, 37 insertions(+), 53 deletions(-)

diff --git a/usr.bin/look/look.c b/usr.bin/look/look.c @@ -1,4 +1,4 @@ -/* $OpenBSD: look.c,v 1.18 2015/10/09 01:37:08 deraadt Exp $ */ +/* $OpenBSD: look.c,v 1.21 2017/01/21 10:03:27 krw Exp $ */ /* $NetBSD: look.c,v 1.7 1995/08/31 22:41:02 jtc Exp $ */ /*- @@ -35,7 +35,7 @@ /* * 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. @@ -57,20 +57,9 @@ #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) int dflag, fflag; @@ -145,12 +134,10 @@ look(char *string, char *front, char *back) char *readp, *writep; /* Reformat string to avoid doing it multiple times later. */ - for (readp = writep = string; ch = *readp++;) { + for (readp = writep = string; (ch = *readp++);) { if (fflag) - ch = FOLD((unsigned char)ch); - if (dflag) - ch = DICT((unsigned char)ch); - if (ch != NO_COMPARE) + ch = tolower((unsigned char)ch); + if (!dflag || isalnum((unsigned char)ch)) *(writep++) = ch; } *writep = '\0'; @@ -166,40 +153,40 @@ look(char *string, char *front, char *back) /* * 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 + * 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 + * + * back points to the beginning of a line at or after the first * matching line. - * + * * Base of the Invariants. - * front = NULL; + * 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 = 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, + * + * 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 + * + * 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 + * + * Trying to continue with binary search at this point would be * more trouble than it's worth. */ #define SKIP_PAST_NEWLINE(p, back) \ @@ -231,12 +218,12 @@ binary_search(char *string, char *front, char *back) /* * 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 points at the first character in a line. * o front is before or at the first line to be printed. */ char * @@ -261,7 +248,7 @@ linear_search(char *string, char *front, char *back) /* * Print as many lines as match string, starting at front. */ -void +void print_from(char *string, char *front, char *back) { for (; front < back && compare(string, front, back) == EQUAL; ++front) { @@ -276,13 +263,13 @@ print_from(char *string, char *front, char *back) /* * 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(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). */ @@ -294,11 +281,8 @@ compare(char *s1, char *s2, char *back) for (; *s1 && s2 < back && *s2 != '\n'; ++s1, ++s2) { ch = *s2; if (fflag) - ch = FOLD((unsigned char)ch); - if (dflag) - ch = DICT((unsigned char)ch); - - if (ch == NO_COMPARE) { + ch = tolower((unsigned char)ch); + if (dflag && !isalnum((unsigned char)ch)) { ++s2; /* Ignore character in comparison. */ continue; }