11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
331556Srgrimes#ifndef lint
3436150Scharnier#if 0
3536150Scharnierstatic char sccsid[] = "@(#)mystring.c	8.2 (Berkeley) 5/4/95";
3636150Scharnier#endif
371556Srgrimes#endif /* not lint */
3899110Sobrien#include <sys/cdefs.h>
3999110Sobrien__FBSDID("$FreeBSD$");
401556Srgrimes
411556Srgrimes/*
421556Srgrimes * String functions.
431556Srgrimes *
441556Srgrimes *	equal(s1, s2)		Return true if strings are equal.
451556Srgrimes *	number(s)		Convert a string of digits to an integer.
461556Srgrimes *	is_number(s)		Return true if s is a string of digits.
471556Srgrimes */
481556Srgrimes
4917987Speter#include <stdlib.h>
501556Srgrimes#include "shell.h"
511556Srgrimes#include "syntax.h"
521556Srgrimes#include "error.h"
531556Srgrimes#include "mystring.h"
541556Srgrimes
551556Srgrimes
561556Srgrimeschar nullstr[1];		/* zero length string */
571556Srgrimes
581556Srgrimes/*
591556Srgrimes * equal - #defined in mystring.h
601556Srgrimes */
611556Srgrimes
621556Srgrimes
631556Srgrimes/*
641556Srgrimes * prefix -- see if pfx is a prefix of string.
651556Srgrimes */
661556Srgrimes
671556Srgrimesint
6890111Simpprefix(const char *pfx, const char *string)
6990111Simp{
701556Srgrimes	while (*pfx) {
711556Srgrimes		if (*pfx++ != *string++)
721556Srgrimes			return 0;
731556Srgrimes	}
741556Srgrimes	return 1;
751556Srgrimes}
761556Srgrimes
771556Srgrimes
781556Srgrimes/*
791556Srgrimes * Convert a string of digits to an integer, printing an error message on
801556Srgrimes * failure.
811556Srgrimes */
821556Srgrimes
831556Srgrimesint
8490111Simpnumber(const char *s)
8590111Simp{
861556Srgrimes	if (! is_number(s))
87201053Sjilles		error("Illegal number: %s", s);
881556Srgrimes	return atoi(s);
891556Srgrimes}
901556Srgrimes
911556Srgrimes
921556Srgrimes
931556Srgrimes/*
941556Srgrimes * Check for a valid number.  This should be elsewhere.
951556Srgrimes */
961556Srgrimes
971556Srgrimesint
9890111Simpis_number(const char *p)
9990111Simp{
1001556Srgrimes	do {
1011556Srgrimes		if (! is_digit(*p))
1021556Srgrimes			return 0;
1031556Srgrimes	} while (*++p != '\0');
1041556Srgrimes	return 1;
1051556Srgrimes}
106