xref: /csrg-svn/bin/sh/mystring.c (revision 54320)
147133Sbostic /*-
247133Sbostic  * Copyright (c) 1991 The Regents of the University of California.
347133Sbostic  * All rights reserved.
447133Sbostic  *
547133Sbostic  * This code is derived from software contributed to Berkeley by
647133Sbostic  * Kenneth Almquist.
747133Sbostic  *
847133Sbostic  * %sccs.include.redist.c%
947133Sbostic  */
1047133Sbostic 
1147133Sbostic #ifndef lint
12*54320Smarc static char sccsid[] = "@(#)mystring.c	5.2 (Berkeley) 06/23/92";
1347133Sbostic #endif /* not lint */
1447133Sbostic 
1547133Sbostic /*
1647133Sbostic  * String functions.
1747133Sbostic  *
1847133Sbostic  *	equal(s1, s2)		Return true if strings are equal.
1947133Sbostic  *	scopy(from, to)		Copy a string.
2047133Sbostic  *	scopyn(from, to, n)	Like scopy, but checks for overflow.
2147133Sbostic  *	number(s)		Convert a string of digits to an integer.
2247133Sbostic  *	is_number(s)		Return true if s is a string of digits.
2347133Sbostic  */
2447133Sbostic 
2547133Sbostic #include "shell.h"
2647133Sbostic #include "syntax.h"
2747133Sbostic #include "error.h"
2847133Sbostic #include "mystring.h"
2947133Sbostic 
3047133Sbostic 
3147133Sbostic char nullstr[1];		/* zero length string */
3247133Sbostic 
33*54320Smarc /*
34*54320Smarc  * equal - #defined in mystring.h
35*54320Smarc  */
3647133Sbostic 
3747133Sbostic /*
38*54320Smarc  * scopy - #defined in mystring.h
39*54320Smarc  */
40*54320Smarc 
41*54320Smarc 
42*54320Smarc /*
4347133Sbostic  * scopyn - copy a string from "from" to "to", truncating the string
4447133Sbostic  *		if necessary.  "To" is always nul terminated, even if
4547133Sbostic  *		truncation is performed.  "Size" is the size of "to".
4647133Sbostic  */
4747133Sbostic 
4847133Sbostic void
4947133Sbostic scopyn(from, to, size)
5047133Sbostic 	register char const *from;
5147133Sbostic 	register char *to;
5247133Sbostic 	register int size;
5347133Sbostic 	{
5447133Sbostic 
5547133Sbostic 	while (--size > 0) {
5647133Sbostic 		if ((*to++ = *from++) == '\0')
5747133Sbostic 			return;
5847133Sbostic 	}
5947133Sbostic 	*to = '\0';
6047133Sbostic }
6147133Sbostic 
6247133Sbostic 
6347133Sbostic /*
6447133Sbostic  * prefix -- see if pfx is a prefix of string.
6547133Sbostic  */
6647133Sbostic 
6747133Sbostic int
6847133Sbostic prefix(pfx, string)
6947133Sbostic 	register char const *pfx;
7047133Sbostic 	register char const *string;
7147133Sbostic 	{
7247133Sbostic 	while (*pfx) {
7347133Sbostic 		if (*pfx++ != *string++)
7447133Sbostic 			return 0;
7547133Sbostic 	}
7647133Sbostic 	return 1;
7747133Sbostic }
7847133Sbostic 
7947133Sbostic 
8047133Sbostic /*
8147133Sbostic  * Convert a string of digits to an integer, printing an error message on
8247133Sbostic  * failure.
8347133Sbostic  */
8447133Sbostic 
8547133Sbostic int
8647133Sbostic number(s)
8747133Sbostic 	const char *s;
8847133Sbostic 	{
8947133Sbostic 
9047133Sbostic 	if (! is_number(s))
9147133Sbostic 		error2("Illegal number", (char *)s);
9247133Sbostic 	return atoi(s);
9347133Sbostic }
9447133Sbostic 
9547133Sbostic 
9647133Sbostic 
9747133Sbostic /*
9847133Sbostic  * Check for a valid number.  This should be elsewhere.
9947133Sbostic  */
10047133Sbostic 
10147133Sbostic int
10247133Sbostic is_number(p)
10347133Sbostic 	register const char *p;
10447133Sbostic 	{
10547133Sbostic 	do {
10647133Sbostic 		if (! is_digit(*p))
10747133Sbostic 			return 0;
10847133Sbostic 	} while (*++p != '\0');
10947133Sbostic 	return 1;
11047133Sbostic }
111