xref: /netbsd-src/bin/sh/mystring.c (revision c591669f0408d33ad7f9518b64d3c076e03a0b1e)
1*c591669fSkre /*	$NetBSD: mystring.c,v 1.21 2024/07/13 13:43:58 kre Exp $	*/
249f0ad86Scgd 
361f28255Scgd /*-
437ed7877Sjtc  * Copyright (c) 1991, 1993
537ed7877Sjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * This code is derived from software contributed to Berkeley by
861f28255Scgd  * Kenneth Almquist.
961f28255Scgd  *
1061f28255Scgd  * Redistribution and use in source and binary forms, with or without
1161f28255Scgd  * modification, are permitted provided that the following conditions
1261f28255Scgd  * are met:
1361f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1561f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd  *    documentation and/or other materials provided with the distribution.
18b5b29542Sagc  * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd  *    may be used to endorse or promote products derived from this software
2061f28255Scgd  *    without specific prior written permission.
2161f28255Scgd  *
2261f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd  * SUCH DAMAGE.
3361f28255Scgd  */
3461f28255Scgd 
35cd799663Schristos #include <sys/cdefs.h>
3661f28255Scgd #ifndef lint
3749f0ad86Scgd #if 0
3807bae7edSchristos static char sccsid[] = "@(#)mystring.c	8.2 (Berkeley) 5/4/95";
3949f0ad86Scgd #else
40*c591669fSkre __RCSID("$NetBSD: mystring.c,v 1.21 2024/07/13 13:43:58 kre Exp $");
4149f0ad86Scgd #endif
4261f28255Scgd #endif /* not lint */
4361f28255Scgd 
4461f28255Scgd /*
4561f28255Scgd  * String functions.
4661f28255Scgd  *
4761f28255Scgd  *	equal(s1, s2)		Return true if strings are equal.
4861f28255Scgd  *	scopy(from, to)		Copy a string.
4961f28255Scgd  *	scopyn(from, to, n)	Like scopy, but checks for overflow.
5061f28255Scgd  *	number(s)		Convert a string of digits to an integer.
5161f28255Scgd  *	is_number(s)		Return true if s is a string of digits.
5261f28255Scgd  */
5361f28255Scgd 
54c6c29888Skre #include <inttypes.h>
55c6c29888Skre #include <limits.h>
56a81e4124Sjtc #include <stdlib.h>
57*c591669fSkre #include <strings.h>
58*c591669fSkre 
5961f28255Scgd #include "shell.h"
6061f28255Scgd #include "syntax.h"
6161f28255Scgd #include "error.h"
6261f28255Scgd #include "mystring.h"
6361f28255Scgd 
6461f28255Scgd 
657e346d08Sdholland const char nullstr[1];		/* zero length string */
6661f28255Scgd 
6737ed7877Sjtc /*
6837ed7877Sjtc  * equal - #defined in mystring.h
6937ed7877Sjtc  */
7037ed7877Sjtc 
7137ed7877Sjtc /*
7237ed7877Sjtc  * scopy - #defined in mystring.h
7337ed7877Sjtc  */
7437ed7877Sjtc 
7561f28255Scgd 
7661f28255Scgd /*
7761f28255Scgd  * scopyn - copy a string from "from" to "to", truncating the string
7861f28255Scgd  *		if necessary.  "To" is always nul terminated, even if
7961f28255Scgd  *		truncation is performed.  "Size" is the size of "to".
8061f28255Scgd  */
8161f28255Scgd 
8261f28255Scgd void
scopyn(const char * from,char * to,int size)83c02b3bbdSchristos scopyn(const char *from, char *to, int size)
8461f28255Scgd {
8561f28255Scgd 
8661f28255Scgd 	while (--size > 0) {
8761f28255Scgd 		if ((*to++ = *from++) == '\0')
8861f28255Scgd 			return;
8961f28255Scgd 	}
9061f28255Scgd 	*to = '\0';
9161f28255Scgd }
9261f28255Scgd 
9361f28255Scgd 
9461f28255Scgd /*
9561f28255Scgd  * prefix -- see if pfx is a prefix of string.
9661f28255Scgd  */
9761f28255Scgd 
9861f28255Scgd int
prefix(const char * pfx,const char * string)99c02b3bbdSchristos prefix(const char *pfx, const char *string)
10061f28255Scgd {
10161f28255Scgd 	while (*pfx) {
10261f28255Scgd 		if (*pfx++ != *string++)
10361f28255Scgd 			return 0;
10461f28255Scgd 	}
10561f28255Scgd 	return 1;
10661f28255Scgd }
10761f28255Scgd 
10861f28255Scgd 
10961f28255Scgd /*
11061f28255Scgd  * Convert a string of digits to an integer, printing an error message on
11161f28255Scgd  * failure.
11261f28255Scgd  */
11361f28255Scgd 
11461f28255Scgd int
number(const char * s)115c02b3bbdSchristos number(const char *s)
11661f28255Scgd {
117c6c29888Skre 	char *ep = NULL;
118c6c29888Skre 	intmax_t n;
11961f28255Scgd 
120c6c29888Skre 	if (!is_digit(*s) || ((n = strtoimax(s, &ep, 10)),
121c6c29888Skre 	    (ep == NULL || ep == s || *ep != '\0')))
12294d6cf1cSkre 		error("Invalid number: '%s'", s);
123c6c29888Skre 	if (n < INT_MIN || n > INT_MAX)
124c6c29888Skre 		error("Number out of range: %s", s);
125c6c29888Skre 	return (int)n;
12661f28255Scgd }
12761f28255Scgd 
12861f28255Scgd 
12961f28255Scgd 
13061f28255Scgd /*
13161f28255Scgd  * Check for a valid number.  This should be elsewhere.
13261f28255Scgd  */
13361f28255Scgd 
13461f28255Scgd int
is_number(const char * p)135c02b3bbdSchristos is_number(const char *p)
13661f28255Scgd {
13761f28255Scgd 	do {
13861f28255Scgd 		if (! is_digit(*p))
13961f28255Scgd 			return 0;
14061f28255Scgd 	} while (*++p != '\0');
14161f28255Scgd 	return 1;
14261f28255Scgd }
143*c591669fSkre 
144*c591669fSkre /*
145*c591669fSkre  * Check a string for common representations of yes/true/...
146*c591669fSkre  * Default result is false
147*c591669fSkre  */
148*c591669fSkre 
149*c591669fSkre int
boolstr(const char * s)150*c591669fSkre boolstr(const char *s)
151*c591669fSkre {
152*c591669fSkre 	size_t len;
153*c591669fSkre 
154*c591669fSkre 	if (s == NULL)
155*c591669fSkre 		return 0;
156*c591669fSkre 
157*c591669fSkre 	len = strlen(s);
158*c591669fSkre 	if (len == 0)
159*c591669fSkre 		return 0;
160*c591669fSkre 
161*c591669fSkre 	if (strncasecmp(s, "true", len) == 0		||
162*c591669fSkre 	    strncasecmp(s, "yes", len) == 0		||
163*c591669fSkre 	    (len == 2 && strcasecmp(s, "on") == 0)	||
164*c591669fSkre 	    is_digit(*s) || atoi(s) != 0)
165*c591669fSkre 		return 1;
166*c591669fSkre 
167*c591669fSkre 	return 0;
168*c591669fSkre }
169