1234848Smav/*-
2234848Smav * Copyright (c) 1991, 1993
3234848Smav *	The Regents of the University of California.  All rights reserved.
4234848Smav *
5234848Smav * This code is derived from software contributed to Berkeley by
6234848Smav * Kenneth Almquist.
7234848Smav *
8234848Smav * Redistribution and use in source and binary forms, with or without
9234848Smav * modification, are permitted provided that the following conditions
10234848Smav * are met:
11234848Smav * 1. Redistributions of source code must retain the above copyright
12234848Smav *    notice, this list of conditions and the following disclaimer.
13234848Smav * 2. Redistributions in binary form must reproduce the above copyright
14234848Smav *    notice, this list of conditions and the following disclaimer in the
15234848Smav *    documentation and/or other materials provided with the distribution.
16234848Smav * 4. Neither the name of the University nor the names of its contributors
17234848Smav *    may be used to endorse or promote products derived from this software
18234848Smav *    without specific prior written permission.
19234848Smav *
20234848Smav * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21234848Smav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22234848Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23234848Smav * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24234848Smav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25234848Smav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26234848Smav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27234848Smav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28234848Smav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29234848Smav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30234848Smav * SUCH DAMAGE.
31234848Smav */
32234848Smav
33234848Smav#ifndef lint
34234848Smav#if 0
35234848Smavstatic char sccsid[] = "@(#)mystring.c	8.2 (Berkeley) 5/4/95";
36234848Smav#endif
37234848Smav#endif /* not lint */
38234848Smav#include <sys/cdefs.h>
39234848Smav__FBSDID("$FreeBSD$");
40234848Smav
41234848Smav/*
42234848Smav * String functions.
43234848Smav *
44234848Smav *	equal(s1, s2)		Return true if strings are equal.
45234848Smav *	number(s)		Convert a string of digits to an integer.
46234848Smav *	is_number(s)		Return true if s is a string of digits.
47234848Smav */
48234848Smav
49234848Smav#include <stdlib.h>
50234848Smav#include "shell.h"
51234848Smav#include "syntax.h"
52234848Smav#include "error.h"
53234848Smav#include "mystring.h"
54234848Smav
55234848Smav
56234848Smavchar nullstr[1];		/* zero length string */
57234848Smav
58234848Smav/*
59234848Smav * equal - #defined in mystring.h
60234848Smav */
61234848Smav
62234848Smav
63234848Smav/*
64234848Smav * Convert a string of digits to an integer, printing an error message on
65234848Smav * failure.
66234848Smav */
67234848Smav
68234848Smavint
69234848Smavnumber(const char *s)
70234848Smav{
71234848Smav	if (! is_number(s))
72234848Smav		error("Illegal number: %s", s);
73234848Smav	return atoi(s);
74234848Smav}
75234848Smav
76234848Smav
77234848Smav
78234848Smav/*
79234848Smav * Check for a valid number.  This should be elsewhere.
80234848Smav */
81234848Smav
82234848Smavint
83234848Smavis_number(const char *p)
84234848Smav{
85234848Smav	const char *q;
86234848Smav
87234848Smav	if (*p == '\0')
88234848Smav		return 0;
89234848Smav	while (*p == '0')
90234848Smav		p++;
91234940Smav	for (q = p; *q != '\0'; q++)
92234848Smav		if (! is_digit(*q))
93234868Smav			return 0;
94234848Smav	if (q - p > 10 ||
95234848Smav	    (q - p == 10 && memcmp(p, "2147483647", 10) > 0))
96234848Smav		return 0;
97234848Smav	return 1;
98234848Smav}
99234940Smav