1 /*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. 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 8.2 (Berkeley) 05/04/95";
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 * number(s) Convert a string of digits to an integer.
22 * is_number(s) Return true if s is a string of digits.
23 */
24
25 #include <stdlib.h>
26 #include "shell.h"
27 #include "syntax.h"
28 #include "error.h"
29 #include "mystring.h"
30
31
32 char nullstr[1]; /* zero length string */
33
34 /*
35 * equal - #defined in mystring.h
36 */
37
38 /*
39 * scopy - #defined in mystring.h
40 */
41
42
43 /*
44 * scopyn - copy a string from "from" to "to", truncating the string
45 * if necessary. "To" is always nul terminated, even if
46 * truncation is performed. "Size" is the size of "to".
47 */
48
49 void
scopyn(from,to,size)50 scopyn(from, to, size)
51 register char const *from;
52 register char *to;
53 register int size;
54 {
55
56 while (--size > 0) {
57 if ((*to++ = *from++) == '\0')
58 return;
59 }
60 *to = '\0';
61 }
62
63
64 /*
65 * prefix -- see if pfx is a prefix of string.
66 */
67
68 int
prefix(pfx,string)69 prefix(pfx, string)
70 register char const *pfx;
71 register char const *string;
72 {
73 while (*pfx) {
74 if (*pfx++ != *string++)
75 return 0;
76 }
77 return 1;
78 }
79
80
81 /*
82 * Convert a string of digits to an integer, printing an error message on
83 * failure.
84 */
85
86 int
number(s)87 number(s)
88 const char *s;
89 {
90
91 if (! is_number(s))
92 error2("Illegal number", (char *)s);
93 return atoi(s);
94 }
95
96
97
98 /*
99 * Check for a valid number. This should be elsewhere.
100 */
101
102 int
is_number(p)103 is_number(p)
104 register const char *p;
105 {
106 do {
107 if (! is_digit(*p))
108 return 0;
109 } while (*++p != '\0');
110 return 1;
111 }
112