mystring.c revision 99110
139092Srnordier/*- 239092Srnordier * Copyright (c) 1991, 1993 339092Srnordier * The Regents of the University of California. All rights reserved. 439092Srnordier * 539092Srnordier * This code is derived from software contributed to Berkeley by 639092Srnordier * Kenneth Almquist. 739092Srnordier * 839092Srnordier * Redistribution and use in source and binary forms, with or without 939092Srnordier * modification, are permitted provided that the following conditions 1039092Srnordier * are met: 1139092Srnordier * 1. Redistributions of source code must retain the above copyright 1239092Srnordier * notice, this list of conditions and the following disclaimer. 1339092Srnordier * 2. Redistributions in binary form must reproduce the above copyright 1439092Srnordier * notice, this list of conditions and the following disclaimer in the 1539092Srnordier * documentation and/or other materials provided with the distribution. 1639092Srnordier * 3. All advertising materials mentioning features or use of this software 1739092Srnordier * must display the following acknowledgement: 1839092Srnordier * This product includes software developed by the University of 1939092Srnordier * California, Berkeley and its contributors. 2039092Srnordier * 4. Neither the name of the University nor the names of its contributors 2139092Srnordier * may be used to endorse or promote products derived from this software 2239092Srnordier * without specific prior written permission. 2339092Srnordier * 2439092Srnordier * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2539092Srnordier * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2639092Srnordier * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2739092Srnordier * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2839092Srnordier * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2950479Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3039092Srnordier * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3139092Srnordier * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32103917Smike * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33130927Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3439092Srnordier * SUCH DAMAGE. 3539092Srnordier */ 3639092Srnordier 37142108Sru#ifndef lint 38142108Sru#if 0 39142108Srustatic char sccsid[] = "@(#)mystring.c 8.2 (Berkeley) 5/4/95"; 40142108Sru#endif 41142108Sru#endif /* not lint */ 42154711Sdelphij#include <sys/cdefs.h> 43154711Sdelphij__FBSDID("$FreeBSD: head/bin/sh/mystring.c 99110 2002-06-30 05:15:05Z obrien $"); 44130927Sobrien 4539092Srnordier/* 4639092Srnordier * String functions. 4739092Srnordier * 4839092Srnordier * equal(s1, s2) Return true if strings are equal. 4939092Srnordier * scopy(from, to) Copy a string. 5039092Srnordier * scopyn(from, to, n) Like scopy, but checks for overflow. 5139092Srnordier * number(s) Convert a string of digits to an integer. 5239092Srnordier * is_number(s) Return true if s is a string of digits. 5339092Srnordier */ 5439092Srnordier 5539092Srnordier#include <stdlib.h> 5639092Srnordier#include "shell.h" 5739092Srnordier#include "syntax.h" 5839092Srnordier#include "error.h" 5939092Srnordier#include "mystring.h" 6039092Srnordier 6139092Srnordier 6239092Srnordierchar nullstr[1]; /* zero length string */ 6339092Srnordier 6439092Srnordier/* 6539092Srnordier * equal - #defined in mystring.h 6639092Srnordier */ 6739092Srnordier 6839092Srnordier/* 6939092Srnordier * scopy - #defined in mystring.h 7039092Srnordier */ 7139092Srnordier 7239092Srnordier 7339092Srnordier/* 7455416Smarcel * scopyn - copy a string from "from" to "to", truncating the string 7555416Smarcel * if necessary. "To" is always nul terminated, even if 7655416Smarcel * truncation is performed. "Size" is the size of "to". 7755416Smarcel */ 7855416Smarcel 7955416Smarcelvoid 8055416Smarcelscopyn(const char *from, char *to, int size) 8155416Smarcel{ 8239092Srnordier 8339092Srnordier while (--size > 0) { 8439092Srnordier if ((*to++ = *from++) == '\0') 8539092Srnordier return; 8639092Srnordier } 8739092Srnordier *to = '\0'; 8839092Srnordier} 8939092Srnordier 9039092Srnordier 9139092Srnordier/* 9239092Srnordier * prefix -- see if pfx is a prefix of string. 9339092Srnordier */ 9439092Srnordier 9539092Srnordierint 9639092Srnordierprefix(const char *pfx, const char *string) 9739092Srnordier{ 9839092Srnordier while (*pfx) { 9939092Srnordier if (*pfx++ != *string++) 10039092Srnordier return 0; 10139092Srnordier } 10239092Srnordier return 1; 10339092Srnordier} 10455416Smarcel 10539092Srnordier 10639092Srnordier/* 10739092Srnordier * Convert a string of digits to an integer, printing an error message on 10839092Srnordier * failure. 10939125Srnordier */ 11039125Srnordier 11139092Srnordierint 11239092Srnordiernumber(const char *s) 11339092Srnordier{ 11439092Srnordier if (! is_number(s)) 11539092Srnordier error("Illegal number: %s", (char *)s); 11639092Srnordier return atoi(s); 11739092Srnordier} 11839092Srnordier 11939092Srnordier 12039092Srnordier 12139092Srnordier/* 12239092Srnordier * Check for a valid number. This should be elsewhere. 12339092Srnordier */ 12439092Srnordier 12539092Srnordierint 12655416Smarcelis_number(const char *p) 12739092Srnordier{ 12839092Srnordier do { 12939092Srnordier if (! is_digit(*p)) 13039092Srnordier return 0; 13139092Srnordier } while (*++p != '\0'); 13239092Srnordier return 1; 13339092Srnordier} 13439092Srnordier