]> git.saurik.com Git - apple/system_cmds.git/blob - passwd.tproj/stringops.c
system_cmds-300.tar.gz
[apple/system_cmds.git] / passwd.tproj / stringops.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 #import <string.h>
26 #import <stdlib.h>
27 #import <stdio.h>
28 #import <stdarg.h>
29 #import "stringops.h"
30
31 char *copyString(char *s)
32 {
33 int len;
34 char *t;
35
36 if (s == NULL) return NULL;
37
38 len = strlen(s) + 1;
39 t = malloc(len);
40 bcopy(s, t, len);
41 return t;
42 }
43
44 char *concatString(char *s, char *t)
45 {
46 int len;
47
48 if (t == NULL) return s;
49
50 len = strlen(s) + strlen(t) + 1;
51 s = realloc(s, len);
52 strcat(s, t);
53 return s;
54 }
55
56 char **insertString(char *s, char **l, unsigned int x)
57 {
58 int i, len;
59
60 if (s == NULL) return l;
61 if (l == NULL)
62 {
63 l = (char **)malloc(2 * sizeof(char *));
64 l[0] = copyString(s);
65 l[1] = NULL;
66 return l;
67 }
68
69 for (i = 0; l[i] != NULL; i++);
70 len = i + 1; /* count the NULL on the end of the list too! */
71
72 l = (char **)realloc(l, (len + 1) * sizeof(char *));
73
74 if ((x >= (len - 1)) || (x == IndexNull))
75 {
76 l[len - 1] = copyString(s);
77 l[len] = NULL;
78 return l;
79 }
80
81 for (i = len; i > x; i--) l[i] = l[i - 1];
82 l[x] = copyString(s);
83 return l;
84 }
85
86 char **appendString(char *s, char **l)
87 {
88 return insertString(s, l, IndexNull);
89 }
90
91 void freeList(char **l)
92 {
93 int i;
94
95 if (l == NULL) return;
96 for (i = 0; l[i] != NULL; i++)
97 {
98 if (l[i] != NULL) free(l[i]);
99 l[i] = NULL;
100 }
101 if (l != NULL) free(l);
102 }
103
104 void freeString(char *s)
105 {
106 if (s == NULL) return;
107 free(s);
108 }
109
110 unsigned int listLength(char **l)
111 {
112 int i;
113
114 if (l == NULL) return 0;
115 for (i = 0; l[i] != NULL; i++);
116 return i;
117 }
118
119 unsigned int listIndex(char *s,char **l)
120 {
121 int i;
122
123 if (l == NULL) return IndexNull;
124 for (i = 0; l[i] != NULL; i++)
125 {
126 if (strcmp(s, l[i]) == 0) return i;
127 }
128 return IndexNull;
129 }
130
131 char *prefix(char *s, char c)
132 {
133 int i;
134 char *t;
135
136 if (s == NULL) return NULL;
137
138 for (i = 0; ((s[i] != '\0') && (s[i] != c)); i++);
139 if (i == 0) return NULL;
140 if (s[i] == '\0') return copyString(s);
141
142 t = malloc(i + 1);
143 bcopy(s, t, i);
144 t[i] = '\0';
145 return t;
146 }
147
148 char *postfix(char *s, char c)
149 {
150 int i, len;
151 char *t;
152
153 if (s == NULL) return NULL;
154
155 for (i = 0; ((s[i] != '\0') && (s[i] != c)); i++);
156 if (s[i] == '\0') return NULL;
157 len = strlen(s) - i;
158 if (len == 1) return NULL;
159
160 t = malloc(len);
161 len--;
162 bcopy((s + i + 1), t, len);
163 t[len] = '\0';
164 return t;
165 }
166
167 char *presuffix(char *s, char c)
168 {
169 int i, len;
170 char *t;
171
172 if (s == NULL) return NULL;
173
174 len = strlen(s);
175 for (i = len - 1; ((i >= 0) && (s[i] != c)); i--);
176 if (i == 0) return NULL;
177 if (s[0] == '\0') return NULL;
178
179 t = malloc(i + 1);
180 bcopy(s, t, i);
181 t[i] = '\0';
182 return t;
183 }
184
185 char *suffix(char *s, char c)
186 {
187 int i, len;
188 char *t;
189
190 if (s == NULL) return NULL;
191
192 len = strlen(s);
193 for (i = len - 1; ((i >= 0) && (s[i] != c)); i--);
194 if (i == 0) return NULL;
195 len -= i;
196 if (len == 1) return NULL;
197 t = malloc(len);
198 len--;
199 bcopy((s + i + 1), t, len);
200 t[len] = '\0';
201 return t;
202 }
203
204 char *lowerCase(char *s)
205 {
206 int i;
207 char *t;
208
209 if (s == NULL) return NULL;
210 t = malloc(strlen(s) + 1);
211
212 for (i = 0; s[i] != '\0'; i++)
213 {
214 if ((s[i] >= 'A') && (s[i] <= 'Z')) t[i] = s[i] + 32;
215 else t[i] = s[i];
216 }
217 t[i] = '\0';
218 return t;
219 }
220
221 char **explode(char *s, char c)
222 {
223 char **l = NULL;
224 char *p, *t;
225 int i, n;
226
227 if (s == NULL) return NULL;
228
229 p = s;
230 while (p[0] != '\0')
231 {
232 for (i = 0; ((p[i] != '\0') && p[i] != c); i++);
233 n = i;
234 t = malloc(n + 1);
235 for (i = 0; i < n; i++) t[i] = p[i];
236 t[n] = '\0';
237 l = appendString(t, l);
238 free(t);
239 t = NULL;
240 if (p[i] == '\0') return l;
241 if (p[i + 1] == '\0') l = appendString("", l);
242 p = p + i + 1;
243 }
244 return l;
245 }