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