]> git.saurik.com Git - apple/system_cmds.git/blob - chkpasswd.tproj/stringops.c
system_cmds-336.tar.gz
[apple/system_cmds.git] / chkpasswd.tproj / stringops.c
1 /*
2 * Copyright (c) 1999 Apple Computer, 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 *copyString(char *s)
31 {
32 int len;
33 char *t;
34
35 if (s == NULL) return NULL;
36
37 len = strlen(s) + 1;
38 t = malloc(len);
39 bcopy(s, t, len);
40 return t;
41 }
42
43 char *concatString(char *s, char *t)
44 {
45 int len;
46
47 if (t == NULL) return s;
48
49 len = strlen(s) + strlen(t) + 1;
50 s = realloc(s, len);
51 strcat(s, t);
52 return s;
53 }
54
55 char **insertString(char *s, char **l, unsigned int x)
56 {
57 int i, len;
58
59 if (s == NULL) return l;
60 if (l == NULL)
61 {
62 l = (char **)malloc(2 * sizeof(char *));
63 l[0] = copyString(s);
64 l[1] = NULL;
65 return l;
66 }
67
68 for (i = 0; l[i] != NULL; i++);
69 len = i + 1; /* count the NULL on the end of the list too! */
70
71 l = (char **)realloc(l, (len + 1) * sizeof(char *));
72
73 if ((x >= (len - 1)) || (x == IndexNull))
74 {
75 l[len - 1] = copyString(s);
76 l[len] = NULL;
77 return l;
78 }
79
80 for (i = len; i > x; i--) l[i] = l[i - 1];
81 l[x] = copyString(s);
82 return l;
83 }
84
85 char **appendString(char *s, char **l)
86 {
87 return insertString(s, l, IndexNull);
88 }
89
90 void freeList(char **l)
91 {
92 int i;
93
94 if (l == NULL) return;
95 for (i = 0; l[i] != NULL; i++)
96 {
97 if (l[i] != NULL) free(l[i]);
98 l[i] = NULL;
99 }
100 if (l != NULL) free(l);
101 }
102
103 void freeString(char *s)
104 {
105 if (s == NULL) return;
106 free(s);
107 }
108
109 unsigned int listLength(char **l)
110 {
111 int i;
112
113 if (l == NULL) return 0;
114 for (i = 0; l[i] != NULL; i++);
115 return i;
116 }
117
118 unsigned int listIndex(char *s,char **l)
119 {
120 int i;
121
122 if (l == NULL) return IndexNull;
123 for (i = 0; l[i] != NULL; i++)
124 {
125 if (strcmp(s, l[i]) == 0) return i;
126 }
127 return IndexNull;
128 }
129
130 char *prefix(char *s, char c)
131 {
132 int i;
133 char *t;
134
135 if (s == NULL) return NULL;
136
137 for (i = 0; ((s[i] != '\0') && (s[i] != c)); i++);
138 if (i == 0) return NULL;
139 if (s[i] == '\0') return copyString(s);
140
141 t = malloc(i + 1);
142 bcopy(s, t, i);
143 t[i] = '\0';
144 return t;
145 }
146
147 char *postfix(char *s, char c)
148 {
149 int i, len;
150 char *t;
151
152 if (s == NULL) return NULL;
153
154 for (i = 0; ((s[i] != '\0') && (s[i] != c)); i++);
155 if (s[i] == '\0') return NULL;
156 len = strlen(s) - i;
157 if (len == 1) return NULL;
158
159 t = malloc(len);
160 len--;
161 bcopy((s + i + 1), t, len);
162 t[len] = '\0';
163 return t;
164 }
165
166 char *presuffix(char *s, char c)
167 {
168 int i, len;
169 char *t;
170
171 if (s == NULL) return NULL;
172
173 len = strlen(s);
174 for (i = len - 1; ((i >= 0) && (s[i] != c)); i--);
175 if (i == 0) return NULL;
176 if (s[0] == '\0') return NULL;
177
178 t = malloc(i + 1);
179 bcopy(s, t, i);
180 t[i] = '\0';
181 return t;
182 }
183
184 char *suffix(char *s, char c)
185 {
186 int i, len;
187 char *t;
188
189 if (s == NULL) return NULL;
190
191 len = strlen(s);
192 for (i = len - 1; ((i >= 0) && (s[i] != c)); i--);
193 if (i == 0) return NULL;
194 len -= i;
195 if (len == 1) return NULL;
196 t = malloc(len);
197 len--;
198 bcopy((s + i + 1), t, len);
199 t[len] = '\0';
200 return t;
201 }
202
203 char *lowerCase(char *s)
204 {
205 int i;
206 char *t;
207
208 if (s == NULL) return NULL;
209 t = malloc(strlen(s) + 1);
210
211 for (i = 0; s[i] != '\0'; i++)
212 {
213 if ((s[i] >= 'A') && (s[i] <= 'Z')) t[i] = s[i] + 32;
214 else t[i] = s[i];
215 }
216 t[i] = '\0';
217 return t;
218 }
219
220 char **explode(char *s, char c)
221 {
222 char **l = NULL;
223 char *p, *t;
224 int i, n;
225
226 if (s == NULL) return NULL;
227
228 p = s;
229 while (p[0] != '\0')
230 {
231 for (i = 0; ((p[i] != '\0') && p[i] != c); i++);
232 n = i;
233 t = malloc(n + 1);
234 for (i = 0; i < n; i++) t[i] = p[i];
235 t[n] = '\0';
236 l = appendString(t, l);
237 free(t);
238 t = NULL;
239 if (p[i] == '\0') return l;
240 if (p[i + 1] == '\0') l = appendString("", l);
241 p = p + i + 1;
242 }
243 return l;
244 }