]>
git.saurik.com Git - apple/system_cmds.git/blob - chkpasswd.tproj/stringops.c
2 * Copyright (c) 1999 Apple Computer, 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@
30 char *copyString(char *s
)
35 if (s
== NULL
) return NULL
;
43 char *concatString(char *s
, char *t
)
47 if (t
== NULL
) return s
;
49 len
= strlen(s
) + strlen(t
) + 1;
55 char **insertString(char *s
, char **l
, unsigned int x
)
59 if (s
== NULL
) return l
;
62 l
= (char **)malloc(2 * sizeof(char *));
68 for (i
= 0; l
[i
] != NULL
; i
++);
69 len
= i
+ 1; /* count the NULL on the end of the list too! */
71 l
= (char **)realloc(l
, (len
+ 1) * sizeof(char *));
73 if ((x
>= (len
- 1)) || (x
== IndexNull
))
75 l
[len
- 1] = copyString(s
);
80 for (i
= len
; i
> x
; i
--) l
[i
] = l
[i
- 1];
85 char **appendString(char *s
, char **l
)
87 return insertString(s
, l
, IndexNull
);
90 void freeList(char **l
)
94 if (l
== NULL
) return;
95 for (i
= 0; l
[i
] != NULL
; i
++)
97 if (l
[i
] != NULL
) free(l
[i
]);
100 if (l
!= NULL
) free(l
);
103 void freeString(char *s
)
105 if (s
== NULL
) return;
109 unsigned int listLength(char **l
)
113 if (l
== NULL
) return 0;
114 for (i
= 0; l
[i
] != NULL
; i
++);
118 unsigned int listIndex(char *s
,char **l
)
122 if (l
== NULL
) return IndexNull
;
123 for (i
= 0; l
[i
] != NULL
; i
++)
125 if (strcmp(s
, l
[i
]) == 0) return i
;
130 char *prefix(char *s
, char c
)
135 if (s
== NULL
) return NULL
;
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
);
147 char *postfix(char *s
, char c
)
152 if (s
== NULL
) return NULL
;
154 for (i
= 0; ((s
[i
] != '\0') && (s
[i
] != c
)); i
++);
155 if (s
[i
] == '\0') return NULL
;
157 if (len
== 1) return NULL
;
161 bcopy((s
+ i
+ 1), t
, len
);
166 char *presuffix(char *s
, char c
)
171 if (s
== NULL
) return NULL
;
174 for (i
= len
- 1; ((i
>= 0) && (s
[i
] != c
)); i
--);
175 if (i
== 0) return NULL
;
176 if (s
[0] == '\0') return NULL
;
184 char *suffix(char *s
, char c
)
189 if (s
== NULL
) return NULL
;
192 for (i
= len
- 1; ((i
>= 0) && (s
[i
] != c
)); i
--);
193 if (i
== 0) return NULL
;
195 if (len
== 1) return NULL
;
198 bcopy((s
+ i
+ 1), t
, len
);
203 char *lowerCase(char *s
)
208 if (s
== NULL
) return NULL
;
209 t
= malloc(strlen(s
) + 1);
211 for (i
= 0; s
[i
] != '\0'; i
++)
213 if ((s
[i
] >= 'A') && (s
[i
] <= 'Z')) t
[i
] = s
[i
] + 32;
220 char **explode(char *s
, char c
)
226 if (s
== NULL
) return NULL
;
231 for (i
= 0; ((p
[i
] != '\0') && p
[i
] != c
); i
++);
234 for (i
= 0; i
< n
; i
++) t
[i
] = p
[i
];
236 l
= appendString(t
, l
);
239 if (p
[i
] == '\0') return l
;
240 if (p
[i
+ 1] == '\0') l
= appendString("", l
);