]>
git.saurik.com Git - apple/system_cmds.git/blob - chkpasswd.tproj/stringops.c
2 * Copyright (c) 1999-2016 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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
22 * @APPLE_LICENSE_HEADER_END@
36 if (s
== NULL
) return NULL
;
45 concatString(char *s
, char *t
)
49 if (t
== NULL
) return s
;
51 len
= strlen(s
) + strlen(t
) + 1;
58 insertString(char *s
, char **l
, unsigned int x
)
62 if (s
== NULL
) return l
;
65 l
= (char **)malloc(2 * sizeof(char *));
71 for (i
= 0; l
[i
] != NULL
; i
++);
72 len
= i
+ 1; /* count the NULL on the end of the list too! */
74 l
= (char **)realloc(l
, (len
+ 1) * sizeof(char *));
76 if ((x
>= (len
- 1)) || (x
== IndexNull
))
78 l
[len
- 1] = copyString(s
);
83 for (i
= len
; i
> x
; i
--) l
[i
] = l
[i
- 1];
89 appendString(char *s
, char **l
)
91 return insertString(s
, l
, IndexNull
);
99 if (l
== NULL
) return;
100 for (i
= 0; l
[i
] != NULL
; i
++)
102 if (l
[i
] != NULL
) free(l
[i
]);
105 if (l
!= NULL
) free(l
);
111 if (s
== NULL
) return;
120 if (l
== NULL
) return 0;
121 for (i
= 0; l
[i
] != NULL
; i
++);
126 listIndex(char *s
,char **l
)
130 if (l
== NULL
) return IndexNull
;
131 for (i
= 0; l
[i
] != NULL
; i
++)
133 if (strcmp(s
, l
[i
]) == 0) return i
;
139 prefix(char *s
, char c
)
144 if (s
== NULL
) return NULL
;
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
);
157 postfix(char *s
, char c
)
163 if (s
== NULL
) return NULL
;
165 for (i
= 0; ((s
[i
] != '\0') && (s
[i
] != c
)); i
++);
166 if (s
[i
] == '\0') return NULL
;
168 if (len
== 1) return NULL
;
172 bcopy((s
+ i
+ 1), t
, len
);
178 presuffix(char *s
, char c
)
184 if (s
== NULL
) return NULL
;
187 for (i
= len
- 1; ((i
>= 0) && (s
[i
] != c
)); i
--);
188 if (i
== 0) return NULL
;
189 if (s
[0] == '\0') return NULL
;
198 suffix(char *s
, char c
)
204 if (s
== NULL
) return NULL
;
207 for (i
= len
- 1; ((i
>= 0) && (s
[i
] != c
)); i
--);
208 if (i
== 0) return NULL
;
210 if (len
== 1) return NULL
;
213 bcopy((s
+ i
+ 1), t
, len
);
224 if (s
== NULL
) return NULL
;
225 t
= malloc(strlen(s
) + 1);
227 for (i
= 0; s
[i
] != '\0'; i
++)
229 if ((s
[i
] >= 'A') && (s
[i
] <= 'Z')) t
[i
] = s
[i
] + 32;
237 explode(char *s
, char c
)
243 if (s
== NULL
) return NULL
;
248 for (i
= 0; ((p
[i
] != '\0') && p
[i
] != c
); i
++);
251 for (i
= 0; i
< n
; i
++) t
[i
] = p
[i
];
253 l
= appendString(t
, l
);
256 if (p
[i
] == '\0') return l
;
257 if (p
[i
+ 1] == '\0') l
= appendString("", l
);