]> git.sur5r.net Git - openldap/blob - libraries/libldap/string.c
Add OpenLDAP RCSid to *.[ch] in clients, libraries, and servers.
[openldap] / libraries / libldap / string.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <ac/stdlib.h>
10 #include <ac/string.h>
11 #include <ac/time.h>
12 #include <ac/ctype.h>
13
14 #include "ldap-int.h"
15
16
17 #if defined ( HAVE_STRSPN )
18 #define int_strspn strspn
19 #else
20 static int int_strspn( const char *str, const char *delim )
21 {
22         int pos;
23         const char *p=delim;
24
25         for( pos=0; (*str) ; pos++,str++) {
26                 if (*str!=*p) {
27                         for( p=delim; (*p) ; p++ ) {
28                                 if (*str==*p) {
29                                         break;
30                                 }
31                         }
32                 }
33
34                 if (*p=='\0') {
35                         return pos;
36                 }
37         }
38         return pos;
39 }
40 #endif
41
42 #if defined( HAVE_STRPBRK )
43 #define int_strpbrk strpbrk
44 #else
45 static char *(int_strpbrk)( const char *str, const char *accept )
46 {
47         const char *p;
48
49         for( ; (*str) ; str++ ) {
50                 for( p=accept; (*p) ; p++) {
51                         if (*str==*p) {
52                                 return str;
53                         }
54                 }
55         }
56
57         return NULL;
58 }
59 #endif
60
61 char *(ldap_pvt_strtok)( char *str, const char *delim, char **pos )
62 {
63         char *p;
64
65         if (pos==NULL) {
66                 return NULL;
67         }
68
69         if (str==NULL) {
70                 if (*pos==NULL) {
71                         return NULL;
72                 }
73
74                 str=*pos;
75         }
76
77         /* skip any initial delimiters */
78         str += int_strspn( str, delim );
79         if (*str == '\0') {
80                 return NULL;
81         }
82
83         p = int_strpbrk( str, delim );
84         if (p==NULL) {
85                 *pos = NULL;
86
87         } else {
88                 *p ='\0';
89                 *pos = p+1;
90         }
91
92         return str;
93 }
94
95 char *
96 (ldap_pvt_strdup)( const char *s )
97 {
98         char    *p;
99         size_t  len = strlen( s ) + 1;
100
101         if ( (p = (char *) malloc( len )) == NULL ) {
102                 return( NULL );
103         }
104
105         memcpy( p, s, len );
106         return( p );
107 }
108
109 char *
110 ldap_pvt_str2upper( char *str )
111 {
112         char    *s;
113
114         /* to upper */
115         for ( s = str; *s; s++ ) {
116                 *s = TOUPPER( (unsigned char) *s );
117         }
118
119         return( str );
120 }
121
122 char *
123 ldap_pvt_str2lower( char *str )
124 {
125         char    *s;
126
127         /* to lower */
128         for ( s = str; *s; s++ ) {
129                 *s = TOLOWER( (unsigned char) *s );
130         }
131
132         return( str );
133 }