1 /* $NetBSD: mystring.c,v 1.15 2002/11/24 22:35:42 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Kenneth Almquist. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)mystring.c 8.2 (Berkeley) 5/4/95"; 43 #else 44 __RCSID("$NetBSD: mystring.c,v 1.15 2002/11/24 22:35:42 christos Exp $"); 45 #endif 46 #endif /* not lint */ 47 48 /* 49 * String functions. 50 * 51 * equal(s1, s2) Return true if strings are equal. 52 * scopy(from, to) Copy a string. 53 * scopyn(from, to, n) Like scopy, but checks for overflow. 54 * number(s) Convert a string of digits to an integer. 55 * is_number(s) Return true if s is a string of digits. 56 */ 57 58 #include <stdlib.h> 59 #include "shell.h" 60 #include "syntax.h" 61 #include "error.h" 62 #include "mystring.h" 63 64 65 char nullstr[1]; /* zero length string */ 66 67 /* 68 * equal - #defined in mystring.h 69 */ 70 71 /* 72 * scopy - #defined in mystring.h 73 */ 74 75 76 /* 77 * scopyn - copy a string from "from" to "to", truncating the string 78 * if necessary. "To" is always nul terminated, even if 79 * truncation is performed. "Size" is the size of "to". 80 */ 81 82 void 83 scopyn(const char *from, char *to, int size) 84 { 85 86 while (--size > 0) { 87 if ((*to++ = *from++) == '\0') 88 return; 89 } 90 *to = '\0'; 91 } 92 93 94 /* 95 * prefix -- see if pfx is a prefix of string. 96 */ 97 98 int 99 prefix(const char *pfx, const char *string) 100 { 101 while (*pfx) { 102 if (*pfx++ != *string++) 103 return 0; 104 } 105 return 1; 106 } 107 108 109 /* 110 * Convert a string of digits to an integer, printing an error message on 111 * failure. 112 */ 113 114 int 115 number(const char *s) 116 { 117 118 if (! is_number(s)) 119 error("Illegal number: %s", s); 120 return atoi(s); 121 } 122 123 124 125 /* 126 * Check for a valid number. This should be elsewhere. 127 */ 128 129 int 130 is_number(const char *p) 131 { 132 do { 133 if (! is_digit(*p)) 134 return 0; 135 } while (*++p != '\0'); 136 return 1; 137 } 138