mystring.c revision 229219
1178476Sjb/*- 2178476Sjb * Copyright (c) 1991, 1993 3178476Sjb * The Regents of the University of California. All rights reserved. 4178476Sjb * 5178476Sjb * This code is derived from software contributed to Berkeley by 6178476Sjb * Kenneth Almquist. 7178476Sjb * 8178476Sjb * Redistribution and use in source and binary forms, with or without 9178476Sjb * modification, are permitted provided that the following conditions 10178476Sjb * are met: 11178476Sjb * 1. Redistributions of source code must retain the above copyright 12178476Sjb * notice, this list of conditions and the following disclaimer. 13178476Sjb * 2. Redistributions in binary form must reproduce the above copyright 14178476Sjb * notice, this list of conditions and the following disclaimer in the 15178476Sjb * documentation and/or other materials provided with the distribution. 16178476Sjb * 4. Neither the name of the University nor the names of its contributors 17178476Sjb * may be used to endorse or promote products derived from this software 18178476Sjb * without specific prior written permission. 19178476Sjb * 20178476Sjb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21178476Sjb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22178476Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23178476Sjb * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24178476Sjb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25178476Sjb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26178476Sjb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27178476Sjb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28178476Sjb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29178476Sjb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30178476Sjb * SUCH DAMAGE. 31178476Sjb */ 32178476Sjb 33178476Sjb#ifndef lint 34178476Sjb#if 0 35178476Sjbstatic char sccsid[] = "@(#)mystring.c 8.2 (Berkeley) 5/4/95"; 36178476Sjb#endif 37178476Sjb#endif /* not lint */ 38178476Sjb#include <sys/cdefs.h> 39178476Sjb__FBSDID("$FreeBSD: head/bin/sh/mystring.c 229219 2012-01-01 22:15:38Z jilles $"); 40178476Sjb 41178476Sjb/* 42178476Sjb * String functions. 43178476Sjb * 44178476Sjb * equal(s1, s2) Return true if strings are equal. 45178476Sjb * scopy(from, to) Copy a string. 46178476Sjb * number(s) Convert a string of digits to an integer. 47178476Sjb * is_number(s) Return true if s is a string of digits. 48178476Sjb */ 49178476Sjb 50178476Sjb#include <stdlib.h> 51178476Sjb#include "shell.h" 52178476Sjb#include "syntax.h" 53178476Sjb#include "error.h" 54178476Sjb#include "mystring.h" 55178476Sjb 56178476Sjb 57178476Sjbchar nullstr[1]; /* zero length string */ 58178476Sjb 59178476Sjb/* 60178476Sjb * equal - #defined in mystring.h 61178476Sjb */ 62178476Sjb 63178476Sjb/* 64178476Sjb * scopy - #defined in mystring.h 65178476Sjb */ 66178476Sjb 67178476Sjb 68178476Sjb/* 69178476Sjb * prefix -- see if pfx is a prefix of string. 70178476Sjb */ 71178476Sjb 72178476Sjbint 73178476Sjbprefix(const char *pfx, const char *string) 74178476Sjb{ 75178476Sjb while (*pfx) { 76178476Sjb if (*pfx++ != *string++) 77178476Sjb return 0; 78178476Sjb } 79178476Sjb return 1; 80178476Sjb} 81178476Sjb 82178476Sjb 83/* 84 * Convert a string of digits to an integer, printing an error message on 85 * failure. 86 */ 87 88int 89number(const char *s) 90{ 91 if (! is_number(s)) 92 error("Illegal number: %s", s); 93 return atoi(s); 94} 95 96 97 98/* 99 * Check for a valid number. This should be elsewhere. 100 */ 101 102int 103is_number(const char *p) 104{ 105 do { 106 if (! is_digit(*p)) 107 return 0; 108 } while (*++p != '\0'); 109 return 1; 110} 111