mystring.c revision 90111
11590Srgrimes/*- 21590Srgrimes * Copyright (c) 1991, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * This code is derived from software contributed to Berkeley by 61590Srgrimes * Kenneth Almquist. 71590Srgrimes * 81590Srgrimes * Redistribution and use in source and binary forms, with or without 91590Srgrimes * modification, are permitted provided that the following conditions 101590Srgrimes * are met: 111590Srgrimes * 1. Redistributions of source code must retain the above copyright 121590Srgrimes * notice, this list of conditions and the following disclaimer. 131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141590Srgrimes * notice, this list of conditions and the following disclaimer in the 151590Srgrimes * documentation and/or other materials provided with the distribution. 161590Srgrimes * 3. All advertising materials mentioning features or use of this software 171590Srgrimes * must display the following acknowledgement: 181590Srgrimes * This product includes software developed by the University of 191590Srgrimes * California, Berkeley and its contributors. 201590Srgrimes * 4. Neither the name of the University nor the names of its contributors 211590Srgrimes * may be used to endorse or promote products derived from this software 221590Srgrimes * without specific prior written permission. 231590Srgrimes * 241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341590Srgrimes * SUCH DAMAGE. 3578201Sdd */ 361590Srgrimes 371590Srgrimes#ifndef lint 381590Srgrimes#if 0 391590Srgrimesstatic char sccsid[] = "@(#)mystring.c 8.2 (Berkeley) 5/4/95"; 401590Srgrimes#endif 4178201Sddstatic const char rcsid[] = 421590Srgrimes "$FreeBSD: head/bin/sh/mystring.c 90111 2002-02-02 06:50:57Z imp $"; 4399112Sobrien#endif /* not lint */ 4499112Sobrien 451590Srgrimes/* 461590Srgrimes * String functions. 471590Srgrimes * 481590Srgrimes * equal(s1, s2) Return true if strings are equal. 491590Srgrimes * scopy(from, to) Copy a string. 501590Srgrimes * scopyn(from, to, n) Like scopy, but checks for overflow. 5174588Sache * number(s) Convert a string of digits to an integer. 5216438Sache * is_number(s) Return true if s is a string of digits. 531590Srgrimes */ 541590Srgrimes 551590Srgrimes#include <stdlib.h> 561590Srgrimes#include "shell.h" 571590Srgrimes#include "syntax.h" 581590Srgrimes#include "error.h" 591590Srgrimes#include "mystring.h" 601590Srgrimes 6111547Sdg 621590Srgrimeschar nullstr[1]; /* zero length string */ 631590Srgrimes 641590Srgrimes/* 6577291Sdd * equal - #defined in mystring.h 661590Srgrimes */ 671590Srgrimes 681590Srgrimes/* 691590Srgrimes * scopy - #defined in mystring.h 701590Srgrimes */ 711590Srgrimes 721590Srgrimes 731590Srgrimes/* 741590Srgrimes * scopyn - copy a string from "from" to "to", truncating the string 751590Srgrimes * if necessary. "To" is always nul terminated, even if 761590Srgrimes * truncation is performed. "Size" is the size of "to". 771590Srgrimes */ 781590Srgrimes 7960938Sjakevoid 8011547Sdgscopyn(const char *from, char *to, int size) 8111547Sdg{ 8236062Sjb 831590Srgrimes while (--size > 0) { 8460938Sjake if ((*to++ = *from++) == '\0') 8511547Sdg return; 861590Srgrimes } 8791536Siedowse *to = '\0'; 881590Srgrimes} 891590Srgrimes 9078201Sdd 9136434Sdanny/* 9236434Sdanny * prefix -- see if pfx is a prefix of string. 9391541Siedowse */ 9474588Sache 9591538Siedowseint 9677291Sddprefix(const char *pfx, const char *string) 9777291Sdd{ 9877291Sdd while (*pfx) { 9977291Sdd if (*pfx++ != *string++) 1001590Srgrimes return 0; 10192920Simp } 10292920Simp return 1; 10392920Simp} 10492920Simp 10592920Simp 10692920Simp/* 10792920Simp * Convert a string of digits to an integer, printing an error message on 10892920Simp * failure. 10992920Simp */ 11092920Simp 1111590Srgrimesint 11236434Sdannynumber(const char *s) 11336434Sdanny{ 11436434Sdanny if (! is_number(s)) 11536434Sdanny error("Illegal number: %s", (char *)s); 11691541Siedowse return atoi(s); 11781161Sdd} 11836434Sdanny 11936434Sdanny 12036434Sdanny 1211590Srgrimes/* 1221590Srgrimes * Check for a valid number. This should be elsewhere. 1231590Srgrimes */ 1241590Srgrimes 1251590Srgrimesint 1261590Srgrimesis_number(const char *p) 1271590Srgrimes{ 1281590Srgrimes do { 12916438Sache if (! is_digit(*p)) 13074588Sache return 0; 13116438Sache } while (*++p != '\0'); 1321590Srgrimes return 1; 13377291Sdd} 13491541Siedowse