1 /*- 2 * Copyright (c) 1991 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Kenneth Almquist. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #ifndef lint 12 static char sccsid[] = "@(#)mystring.c 5.1 (Berkeley) 03/07/91"; 13 #endif /* not lint */ 14 15 /* 16 * String functions. 17 * 18 * equal(s1, s2) Return true if strings are equal. 19 * scopy(from, to) Copy a string. 20 * scopyn(from, to, n) Like scopy, but checks for overflow. 21 * strchr(s, c) Find first occurance of c in s. 22 * bcopy(from, to, n) Copy a block of memory. 23 * number(s) Convert a string of digits to an integer. 24 * is_number(s) Return true if s is a string of digits. 25 */ 26 27 #include "shell.h" 28 #include "syntax.h" 29 #include "error.h" 30 #include "mystring.h" 31 32 33 char nullstr[1]; /* zero length string */ 34 35 36 /* 37 * scopyn - copy a string from "from" to "to", truncating the string 38 * if necessary. "To" is always nul terminated, even if 39 * truncation is performed. "Size" is the size of "to". 40 */ 41 42 void 43 scopyn(from, to, size) 44 register char const *from; 45 register char *to; 46 register int size; 47 { 48 49 while (--size > 0) { 50 if ((*to++ = *from++) == '\0') 51 return; 52 } 53 *to = '\0'; 54 } 55 56 57 /* 58 * strchr - find first occurrence of a character in a string. 59 */ 60 61 #ifndef SYS5 62 char * 63 mystrchr(s, charwanted) 64 char const *s; 65 register char charwanted; 66 { 67 register char const *scan; 68 69 /* 70 * The odd placement of the two tests is so NUL is findable. 71 */ 72 for (scan = s ; *scan != charwanted ; ) /* ++ moved down for opt. */ 73 if (*scan++ == '\0') 74 return NULL; 75 return (char *)scan; 76 } 77 #endif 78 79 80 81 /* 82 * bcopy - copy bytes 83 * 84 * This routine was derived from code by Henry Spencer. 85 */ 86 87 void 88 mybcopy(src, dst, length) 89 pointer dst; 90 const pointer src; 91 register int length; 92 { 93 register char *d = dst; 94 register char *s = src; 95 96 while (--length >= 0) 97 *d++ = *s++; 98 } 99 100 101 /* 102 * prefix -- see if pfx is a prefix of string. 103 */ 104 105 int 106 prefix(pfx, string) 107 register char const *pfx; 108 register char const *string; 109 { 110 while (*pfx) { 111 if (*pfx++ != *string++) 112 return 0; 113 } 114 return 1; 115 } 116 117 118 /* 119 * Convert a string of digits to an integer, printing an error message on 120 * failure. 121 */ 122 123 int 124 number(s) 125 const char *s; 126 { 127 128 if (! is_number(s)) 129 error2("Illegal number", (char *)s); 130 return atoi(s); 131 } 132 133 134 135 /* 136 * Check for a valid number. This should be elsewhere. 137 */ 138 139 int 140 is_number(p) 141 register const char *p; 142 { 143 do { 144 if (! is_digit(*p)) 145 return 0; 146 } while (*++p != '\0'); 147 return 1; 148 } 149