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