1181905Sed/*-
2181905Sed * Copyright (c) 1991, 1993
3181905Sed *	The Regents of the University of California.  All rights reserved.
4181905Sed *
5181905Sed * This code is derived from software contributed to Berkeley by
6181905Sed * Kenneth Almquist.
7181905Sed *
8181905Sed * Redistribution and use in source and binary forms, with or without
9181905Sed * modification, are permitted provided that the following conditions
10181905Sed * are met:
11181905Sed * 1. Redistributions of source code must retain the above copyright
12181905Sed *    notice, this list of conditions and the following disclaimer.
13181905Sed * 2. Redistributions in binary form must reproduce the above copyright
14181905Sed *    notice, this list of conditions and the following disclaimer in the
15181905Sed *    documentation and/or other materials provided with the distribution.
16181905Sed * 4. Neither the name of the University nor the names of its contributors
17181905Sed *    may be used to endorse or promote products derived from this software
18181905Sed *    without specific prior written permission.
19181905Sed *
20181905Sed * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21181905Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22181905Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23181905Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24181905Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25181905Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26181905Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27181905Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28181905Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29181905Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30181905Sed * SUCH DAMAGE.
31181905Sed */
32181905Sed
33181905Sed#ifndef lint
34181905Sed#if 0
35181905Sedstatic char sccsid[] = "@(#)mystring.c	8.2 (Berkeley) 5/4/95";
36181905Sed#endif
37181905Sed#endif /* not lint */
38181905Sed#include <sys/cdefs.h>
39181905Sed__FBSDID("$FreeBSD$");
40181905Sed
41181905Sed/*
42181905Sed * String functions.
43181905Sed *
44181905Sed *	equal(s1, s2)		Return true if strings are equal.
45181905Sed *	scopy(from, to)		Copy a string.
46181905Sed *	number(s)		Convert a string of digits to an integer.
47181905Sed *	is_number(s)		Return true if s is a string of digits.
48181905Sed */
49181905Sed
50181905Sed#include <stdlib.h>
51181905Sed#include "shell.h"
52181905Sed#include "syntax.h"
53181905Sed#include "error.h"
54181905Sed#include "mystring.h"
55188096Sed
56188096Sed
57181905Sedchar nullstr[1];		/* zero length string */
58181905Sed
59181905Sed/*
60181905Sed * equal - #defined in mystring.h
61188096Sed */
62188096Sed
63188096Sed/*
64188096Sed * scopy - #defined in mystring.h
65188096Sed */
66188096Sed
67188096Sed
68188096Sed/*
69188096Sed * prefix -- see if pfx is a prefix of string.
70188096Sed */
71188096Sed
72188096Sedint
73188096Sedprefix(const char *pfx, const char *string)
74188096Sed{
75188096Sed	while (*pfx) {
76188096Sed		if (*pfx++ != *string++)
77188096Sed			return 0;
78188096Sed	}
79188096Sed	return 1;
80188096Sed}
81188096Sed
82188096Sed
83188096Sed/*
84181905Sed * Convert a string of digits to an integer, printing an error message on
85181905Sed * failure.
86181905Sed */
87181905Sed
88181905Sedint
89181905Sednumber(const char *s)
90181905Sed{
91181905Sed	if (! is_number(s))
92181905Sed		error("Illegal number: %s", s);
93181905Sed	return atoi(s);
94181905Sed}
95181905Sed
96181905Sed
97182471Sed
98181905Sed/*
99182471Sed * Check for a valid number.  This should be elsewhere.
100181905Sed */
101181905Sed
102181905Sedint
103181905Sedis_number(const char *p)
104181905Sed{
105181905Sed	do {
106181905Sed		if (! is_digit(*p))
107181905Sed			return 0;
108181905Sed	} while (*++p != '\0');
109181905Sed	return 1;
110181905Sed}
111181905Sed