]>
git.saurik.com Git - apple/system_cmds.git/blob - passwd.tproj/stringops.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
31 char *copyString(char *s
)
36 if (s
== NULL
) return NULL
;
44 char *concatString(char *s
, char *t
)
48 if (t
== NULL
) return s
;
50 len
= strlen(s
) + strlen(t
) + 1;
56 char **insertString(char *s
, char **l
, unsigned int x
)
60 if (s
== NULL
) return l
;
63 l
= (char **)malloc(2 * sizeof(char *));
69 for (i
= 0; l
[i
] != NULL
; i
++);
70 len
= i
+ 1; /* count the NULL on the end of the list too! */
72 l
= (char **)realloc(l
, (len
+ 1) * sizeof(char *));
74 if ((x
>= (len
- 1)) || (x
== IndexNull
))
76 l
[len
- 1] = copyString(s
);
81 for (i
= len
; i
> x
; i
--) l
[i
] = l
[i
- 1];
86 char **appendString(char *s
, char **l
)
88 return insertString(s
, l
, IndexNull
);
91 void freeList(char **l
)
95 if (l
== NULL
) return;
96 for (i
= 0; l
[i
] != NULL
; i
++)
98 if (l
[i
] != NULL
) free(l
[i
]);
101 if (l
!= NULL
) free(l
);
104 void freeString(char *s
)
106 if (s
== NULL
) return;
110 unsigned int listLength(char **l
)
114 if (l
== NULL
) return 0;
115 for (i
= 0; l
[i
] != NULL
; i
++);
119 unsigned int listIndex(char *s
,char **l
)
123 if (l
== NULL
) return IndexNull
;
124 for (i
= 0; l
[i
] != NULL
; i
++)
126 if (strcmp(s
, l
[i
]) == 0) return i
;
131 char *prefix(char *s
, char c
)
136 if (s
== NULL
) return NULL
;
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
);
148 char *postfix(char *s
, char c
)
153 if (s
== NULL
) return NULL
;
155 for (i
= 0; ((s
[i
] != '\0') && (s
[i
] != c
)); i
++);
156 if (s
[i
] == '\0') return NULL
;
158 if (len
== 1) return NULL
;
162 bcopy((s
+ i
+ 1), t
, len
);
167 char *presuffix(char *s
, char c
)
172 if (s
== NULL
) return NULL
;
175 for (i
= len
- 1; ((i
>= 0) && (s
[i
] != c
)); i
--);
176 if (i
== 0) return NULL
;
177 if (s
[0] == '\0') return NULL
;
185 char *suffix(char *s
, char c
)
190 if (s
== NULL
) return NULL
;
193 for (i
= len
- 1; ((i
>= 0) && (s
[i
] != c
)); i
--);
194 if (i
== 0) return NULL
;
196 if (len
== 1) return NULL
;
199 bcopy((s
+ i
+ 1), t
, len
);
204 char *lowerCase(char *s
)
209 if (s
== NULL
) return NULL
;
210 t
= malloc(strlen(s
) + 1);
212 for (i
= 0; s
[i
] != '\0'; i
++)
214 if ((s
[i
] >= 'A') && (s
[i
] <= 'Z')) t
[i
] = s
[i
] + 32;
221 char **explode(char *s
, char c
)
227 if (s
== NULL
) return NULL
;
232 for (i
= 0; ((p
[i
] != '\0') && p
[i
] != c
); i
++);
235 for (i
= 0; i
< n
; i
++) t
[i
] = p
[i
];
237 l
= appendString(t
, l
);
240 if (p
[i
] == '\0') return l
;
241 if (p
[i
+ 1] == '\0') l
= appendString("", l
);