xref: /csrg-svn/bin/sh/mystring.c (revision 69272)
147133Sbostic /*-
260698Sbostic  * Copyright (c) 1991, 1993
360698Sbostic  *	The Regents of the University of California.  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*69272Schristos static char sccsid[] = "@(#)mystring.c	8.2 (Berkeley) 05/04/95";
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 
25*69272Schristos #include <stdlib.h>
2647133Sbostic #include "shell.h"
2747133Sbostic #include "syntax.h"
2847133Sbostic #include "error.h"
2947133Sbostic #include "mystring.h"
3047133Sbostic 
3147133Sbostic 
3247133Sbostic char nullstr[1];		/* zero length string */
3347133Sbostic 
3454320Smarc /*
3554320Smarc  * equal - #defined in mystring.h
3654320Smarc  */
3747133Sbostic 
3847133Sbostic /*
3954320Smarc  * scopy - #defined in mystring.h
4054320Smarc  */
4154320Smarc 
4254320Smarc 
4354320Smarc /*
4447133Sbostic  * scopyn - copy a string from "from" to "to", truncating the string
4547133Sbostic  *		if necessary.  "To" is always nul terminated, even if
4647133Sbostic  *		truncation is performed.  "Size" is the size of "to".
4747133Sbostic  */
4847133Sbostic 
4947133Sbostic void
scopyn(from,to,size)5047133Sbostic scopyn(from, to, size)
5147133Sbostic 	register char const *from;
5247133Sbostic 	register char *to;
5347133Sbostic 	register int size;
5447133Sbostic 	{
5547133Sbostic 
5647133Sbostic 	while (--size > 0) {
5747133Sbostic 		if ((*to++ = *from++) == '\0')
5847133Sbostic 			return;
5947133Sbostic 	}
6047133Sbostic 	*to = '\0';
6147133Sbostic }
6247133Sbostic 
6347133Sbostic 
6447133Sbostic /*
6547133Sbostic  * prefix -- see if pfx is a prefix of string.
6647133Sbostic  */
6747133Sbostic 
6847133Sbostic int
prefix(pfx,string)6947133Sbostic prefix(pfx, string)
7047133Sbostic 	register char const *pfx;
7147133Sbostic 	register char const *string;
7247133Sbostic 	{
7347133Sbostic 	while (*pfx) {
7447133Sbostic 		if (*pfx++ != *string++)
7547133Sbostic 			return 0;
7647133Sbostic 	}
7747133Sbostic 	return 1;
7847133Sbostic }
7947133Sbostic 
8047133Sbostic 
8147133Sbostic /*
8247133Sbostic  * Convert a string of digits to an integer, printing an error message on
8347133Sbostic  * failure.
8447133Sbostic  */
8547133Sbostic 
8647133Sbostic int
number(s)8747133Sbostic number(s)
8847133Sbostic 	const char *s;
8947133Sbostic 	{
9047133Sbostic 
9147133Sbostic 	if (! is_number(s))
9247133Sbostic 		error2("Illegal number", (char *)s);
9347133Sbostic 	return atoi(s);
9447133Sbostic }
9547133Sbostic 
9647133Sbostic 
9747133Sbostic 
9847133Sbostic /*
9947133Sbostic  * Check for a valid number.  This should be elsewhere.
10047133Sbostic  */
10147133Sbostic 
10247133Sbostic int
is_number(p)10347133Sbostic is_number(p)
10447133Sbostic 	register const char *p;
10547133Sbostic 	{
10647133Sbostic 	do {
10747133Sbostic 		if (! is_digit(*p))
10847133Sbostic 			return 0;
10947133Sbostic 	} while (*++p != '\0');
11047133Sbostic 	return 1;
11147133Sbostic }
112