[c28a023] | 1 | /* $NetBSD: mystring.c,v 1.14 1999/07/09 03:05:50 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.14 1999/07/09 03:05:50 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(from, to, size)
|
---|
| 84 | char const *from;
|
---|
| 85 | char *to;
|
---|
| 86 | int size;
|
---|
| 87 | {
|
---|
| 88 |
|
---|
| 89 | while (--size > 0) {
|
---|
| 90 | if ((*to++ = *from++) == '\0')
|
---|
| 91 | return;
|
---|
| 92 | }
|
---|
| 93 | *to = '\0';
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 |
|
---|
| 97 | /*
|
---|
| 98 | * prefix -- see if pfx is a prefix of string.
|
---|
| 99 | */
|
---|
| 100 |
|
---|
| 101 | int
|
---|
| 102 | prefix(pfx, string)
|
---|
| 103 | char const *pfx;
|
---|
| 104 | char const *string;
|
---|
| 105 | {
|
---|
| 106 | while (*pfx) {
|
---|
| 107 | if (*pfx++ != *string++)
|
---|
| 108 | return 0;
|
---|
| 109 | }
|
---|
| 110 | return 1;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 |
|
---|
| 114 | /*
|
---|
| 115 | * Convert a string of digits to an integer, printing an error message on
|
---|
| 116 | * failure.
|
---|
| 117 | */
|
---|
| 118 |
|
---|
| 119 | int
|
---|
| 120 | number(s)
|
---|
| 121 | const char *s;
|
---|
| 122 | {
|
---|
| 123 |
|
---|
| 124 | if (! is_number(s))
|
---|
| 125 | error("Illegal number: %s", s);
|
---|
| 126 | return atoi(s);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 |
|
---|
| 130 |
|
---|
| 131 | /*
|
---|
| 132 | * Check for a valid number. This should be elsewhere.
|
---|
| 133 | */
|
---|
| 134 |
|
---|
| 135 | int
|
---|
| 136 | is_number(p)
|
---|
| 137 | const char *p;
|
---|
| 138 | {
|
---|
| 139 | do {
|
---|
| 140 | if (! is_digit(*p))
|
---|
| 141 | return 0;
|
---|
| 142 | } while (*++p != '\0');
|
---|
| 143 | return 1;
|
---|
| 144 | }
|
---|