1 /* $NetBSD: mystring.c,v 1.11 1996/10/16 14:49:03 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 #ifndef lint 40 #if 0 41 static char sccsid[] = "@(#)mystring.c 8.2 (Berkeley) 5/4/95"; 42 #else 43 static char rcsid[] = "$NetBSD: mystring.c,v 1.11 1996/10/16 14:49:03 christos Exp $"; 44 #endif 45 #endif /* not lint */ 46 47 /* 48 * String functions. 49 * 50 * equal(s1, s2) Return true if strings are equal. 51 * scopy(from, to) Copy a string. 52 * scopyn(from, to, n) Like scopy, but checks for overflow. 53 * number(s) Convert a string of digits to an integer. 54 * is_number(s) Return true if s is a string of digits. 55 */ 56 57 #include <stdlib.h> 58 #include "shell.h" 59 #include "syntax.h" 60 #include "error.h" 61 #include "mystring.h" 62 63 64 char nullstr[1]; /* zero length string */ 65 66 /* 67 * equal - #defined in mystring.h 68 */ 69 70 /* 71 * scopy - #defined in mystring.h 72 */ 73 74 75 /* 76 * scopyn - copy a string from "from" to "to", truncating the string 77 * if necessary. "To" is always nul terminated, even if 78 * truncation is performed. "Size" is the size of "to". 79 */ 80 81 void 82 scopyn(from, to, size) 83 register char const *from; 84 register char *to; 85 register int size; 86 { 87 88 while (--size > 0) { 89 if ((*to++ = *from++) == '\0') 90 return; 91 } 92 *to = '\0'; 93 } 94 95 96 /* 97 * prefix -- see if pfx is a prefix of string. 98 */ 99 100 int 101 prefix(pfx, string) 102 register char const *pfx; 103 register char const *string; 104 { 105 while (*pfx) { 106 if (*pfx++ != *string++) 107 return 0; 108 } 109 return 1; 110 } 111 112 113 /* 114 * Convert a string of digits to an integer, printing an error message on 115 * failure. 116 */ 117 118 int 119 number(s) 120 const char *s; 121 { 122 123 if (! is_number(s)) 124 error("Illegal number: %s", (char *)s); 125 return atoi(s); 126 } 127 128 129 130 /* 131 * Check for a valid number. This should be elsewhere. 132 */ 133 134 int 135 is_number(p) 136 register const char *p; 137 { 138 do { 139 if (! is_digit(*p)) 140 return 0; 141 } while (*++p != '\0'); 142 return 1; 143 } 144