[comp.bugs.4bsd.ucb-fixes] V1.34

bostic@OKEEFFE.BERKELEY.EDU (Keith Bostic) (08/11/87)

Subject: case insensitive string compare routines
Index: lib/libc/gen/strcasecmp.c 4.3BSD

Note -- this is *NOT* a bug fix, and does not need to be installed!!
This is an enhancement to 4.3BSD that has been requested by a number
of people.

Description:
	There are no library routines to do case insensitive string
	comparisons.
Fix:
	Unshar the attached files; install strcasecmp.c in
	/usr/src/lib/libc/gen/strcasecmp.c, and string.3 in
	/usr/man/man3/string.3.  Edit /usr/src/lib/libc/gen/Makefile
	to compile and install strcasecmp.o as part of the C library.

echo x - strcasecmp.c
sed 's/^X//' >strcasecmp.c << 'END-of-strcasecmp.c'
X/*
X * Copyright (c) 1987 Regents of the University of California.
X * All rights reserved.  The Berkeley software License Agreement
X * specifies the terms and conditions for redistribution.
X */
X
X#if defined(LIBC_SCCS) && !defined(lint)
Xstatic char sccsid[] = "@(#)strcasecmp.c	1.3 (Berkeley) 8/3/87";
X#endif LIBC_SCCS and not lint
X
X/*
X * This array is designed for mapping upper and lower case letter
X * together for a case independent comparison.  The mappings are
X * based upon ascii character sequences.
X */
Xstatic char charmap[] = {
X	'\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
X	'\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
X	'\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
X	'\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
X	'\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
X	'\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
X	'\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
X	'\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
X	'\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
X	'\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
X	'\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
X	'\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
X	'\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
X	'\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
X	'\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
X	'\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
X	'\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
X	'\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
X	'\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
X	'\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
X	'\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
X	'\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
X	'\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
X	'\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
X	'\300', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
X	'\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
X	'\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
X	'\370', '\371', '\372', '\333', '\334', '\335', '\336', '\337',
X	'\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
X	'\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
X	'\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
X	'\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
X};
X
Xstrcasecmp(s1, s2)
X	register char *s1, *s2;
X{
X	register char *cm = charmap;
X
X	while (cm[*s1] == cm[*s2++])
X		if (*s1++ == '\0')
X			return(0);
X	return(cm[*s1] - cm[*--s2]);
X}
X
Xstrncasecmp(s1, s2, n)
X	register char *s1, *s2;
X	register int n;
X{
X	register char *cm = charmap;
X
X	while (--n >= 0 && cm[*s1] == cm[*s2++])
X		if (*s1++ == '\0')
X			return(0);
X	return(n < 0 ? 0 : cm[*s1] - cm[*--s2]);
X}
END-of-strcasecmp.c
echo x - string.3
sed 's/^X//' >string.3 << 'END-of-string.3'
X.\" Copyright (c) 1980 Regents of the University of California.
X.\" All rights reserved.  The Berkeley software License Agreement
X.\" specifies the terms and conditions for redistribution.
X.\"
X.\"	@(#)string.3	6.4 (Berkeley) 8/3/87
X.\"
X.TH STRING 3  "August 3, 1987"
X.UC 4
X.SH NAME
Xstrcat, strncat, strcmp, strncmp, strcasecmp, strncasecmp, strcpy,
Xstrncpy, strlen, index, rindex \- string operations
X.SH SYNOPSIS
X.nf
X.B #include <strings.h>
X.PP
X.B char *strcat(s1, s2)
X.B char *s1, *s2;
X.PP
X.B char *strncat(s1, s2, n)
X.B char *s1, *s2;
X.PP
X.B strcmp(s1, s2)
X.B char *s1, *s2;
X.PP
X.B strncmp(s1, s2, n)
X.B char *s1, *s2;
X.PP
X.B strcasecmp(s1, s2)
X.B char *s1, *s2;
X.PP
X.B strncasecmp(s1, s2, n)
X.B char *s1, *s2;
X.PP
X.B char *strcpy(s1, s2)
X.B char *s1, *s2;
X.PP
X.B char *strncpy(s1, s2, n)
X.B char *s1, *s2;
X.PP
X.B strlen(s)
X.B char *s;
X.PP
X.B char *index(s, c)
X.B char *s, c;
X.PP
X.B char *rindex(s, c)
X.B char *s, c;
X.fi
X.SH DESCRIPTION
XThese functions operate on null-terminated strings.
XThey do not check for overflow of any receiving string.
X.PP
X.I Strcat
Xappends a copy of string
X.I s2
Xto the end of string
X.IR s1 .
X.I Strncat
Xcopies at most
X.I n
Xcharacters.  Both return a pointer to the null-terminated result.
X.PP
X.I Strcmp
Xcompares its arguments and returns an integer
Xgreater than, equal to, or less than 0, according as
X.I s1
Xis lexicographically greater than, equal to, or less than
X.IR s2 .
X.I Strncmp
Xmakes the same comparison but looks at at most
X.I n
Xcharacters.  \fIStrcasecmp\fP and \fIstrncasecmp\fP are identical
Xin function, but are case insensitive.  The returned lexicographic
Xdifference reflects a conversion to lower-case.
X.PP
X.I Strcpy
Xcopies string
X.I s2
Xto
X.I s1,
Xstopping after the null character has been moved.
X.I Strncpy
Xcopies exactly
X.I n
Xcharacters, truncating or null-padding
X.I s2;
Xthe target may not be null-terminated if the length of
X.I s2
Xis
X.I n
Xor more.  Both return
X.IR s1 .
X.PP
X.I Strlen
Xreturns the number of non-null characters in
X.IR s .
X.PP
X.I Index
X.RI ( rindex )
Xreturns a pointer to the first (last) occurrence of character 
X\fIc\fP in string \fIs\fP or zero if \fIc\fP does not occur in
Xthe string.  Setting \fIc\fP to NULL works.
END-of-string.3
exit