]> git.saurik.com Git - apple/system_cmds.git/blame - chkpasswd.tproj/stringops.c
system_cmds-880.40.5.tar.gz
[apple/system_cmds.git] / chkpasswd.tproj / stringops.c
CommitLineData
1815bff5 1/*
cf37c299 2 * Copyright (c) 1999-2016 Apple Inc. All rights reserved.
1815bff5
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
cf37c299 5 *
2fc1e207
A
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.
cf37c299 13 *
1815bff5
A
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,
2fc1e207
A
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."
cf37c299 21 *
1815bff5
A
22 * @APPLE_LICENSE_HEADER_END@
23 */
24#import <string.h>
25#import <stdlib.h>
26#import <stdio.h>
20e66415 27#import <stdarg.h>
1815bff5
A
28#import "stringops.h"
29
cf37c299
A
30char *
31copyString(char *s)
1815bff5 32{
cf37c299 33 size_t len;
1815bff5
A
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
cf37c299
A
44char *
45concatString(char *s, char *t)
1815bff5 46{
cf37c299 47 size_t len;
1815bff5
A
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
cf37c299
A
57char **
58insertString(char *s, char **l, unsigned int x)
1815bff5
A
59{
60 int i, len;
61
62 if (s == NULL) return l;
cf37c299 63 if (l == NULL)
1815bff5
A
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
cf37c299
A
88char **
89appendString(char *s, char **l)
1815bff5
A
90{
91 return insertString(s, l, IndexNull);
92}
93
cf37c299
A
94void
95freeList(char **l)
1815bff5
A
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
cf37c299
A
108void
109freeString(char *s)
1815bff5
A
110{
111 if (s == NULL) return;
112 free(s);
113}
114
cf37c299
A
115unsigned int
116listLength(char **l)
1815bff5
A
117{
118 int i;
119
120 if (l == NULL) return 0;
121 for (i = 0; l[i] != NULL; i++);
122 return i;
123}
124
cf37c299
A
125unsigned int
126listIndex(char *s,char **l)
1815bff5
A
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
cf37c299
A
138char *
139prefix(char *s, char c)
1815bff5
A
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
cf37c299
A
156char *
157postfix(char *s, char c)
1815bff5 158{
cf37c299
A
159 int i;
160 size_t len;
1815bff5
A
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}
cf37c299
A
176
177char *
178presuffix(char *s, char c)
1815bff5 179{
cf37c299
A
180 ssize_t i;
181 size_t len;
1815bff5
A
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
cf37c299
A
197char *
198suffix(char *s, char c)
1815bff5 199{
cf37c299
A
200 ssize_t i;
201 size_t len;
1815bff5
A
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
cf37c299
A
218char *
219lowerCase(char *s)
1815bff5
A
220{
221 int i;
222 char *t;
223
224 if (s == NULL) return NULL;
225 t = malloc(strlen(s) + 1);
226
cf37c299 227 for (i = 0; s[i] != '\0'; i++)
1815bff5
A
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
cf37c299
A
236char **
237explode(char *s, char c)
1815bff5
A
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}