xref: /titanic_50/usr/src/lib/libldap5/sources/ldap/common/friendly.c (revision 0bb073995ac5a95bd35f2dd790df1ea3d8c2d507)
1 /*
2  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 #pragma ident	"%Z%%M%	%I%	%E% SMI"
6 
7 /*
8  * The contents of this file are subject to the Netscape Public
9  * License Version 1.1 (the "License"); you may not use this file
10  * except in compliance with the License. You may obtain a copy of
11  * the License at http://www.mozilla.org/NPL/
12  *
13  * Software distributed under the License is distributed on an "AS
14  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
15  * implied. See the License for the specific language governing
16  * rights and limitations under the License.
17  *
18  * The Original Code is Mozilla Communicator client code, released
19  * March 31, 1998.
20  *
21  * The Initial Developer of the Original Code is Netscape
22  * Communications Corporation. Portions created by Netscape are
23  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
24  * Rights Reserved.
25  *
26  * Contributor(s):
27  */
28 /*
29  *  Copyright (c) 1990 Regents of the University of Michigan.
30  *  All rights reserved.
31  */
32 /*
33  *  friendly.c
34  */
35 
36 #if 0
37 #ifndef lint
38 static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
39 #endif
40 #endif
41 
42 #include "ldap-int.h"
43 
44 char *
45 LDAP_CALL
46 ldap_friendly_name( char *filename, char *name, FriendlyMap *map )
47 {
48 	int	i, entries;
49 	FILE	*fp;
50 	char	*s;
51 	char	buf[BUFSIZ];
52 
53 	if ( map == NULL ) {
54 		return( name );
55 	}
56     if ( NULL == name)
57     {
58         return (name);
59     }
60 
61 	if ( *map == NULL ) {
62 		if ( (fp = fopen( filename, "rF" )) == NULL )
63 			return( name );
64 
65 		entries = 0;
66 		while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
67 			if ( buf[0] != '#' )
68 				entries++;
69 		}
70 		rewind( fp );
71 
72 		if ( (*map = (FriendlyMap)NSLDAPI_MALLOC( (entries + 1) *
73 		    sizeof(struct friendly) )) == NULL ) {
74 			fclose( fp );
75 			return( name );
76 		}
77 
78 		i = 0;
79 		while ( fgets( buf, sizeof(buf), fp ) != NULL && i < entries ) {
80 			if ( buf[0] == '#' )
81 				continue;
82 
83 			if ( (s = strchr( buf, '\n' )) != NULL )
84 				*s = '\0';
85 
86 			if ( (s = strchr( buf, '\t' )) == NULL )
87 				continue;
88 			*s++ = '\0';
89 
90 			if ( *s == '"' ) {
91 				int	esc = 0, found = 0;
92 
93 				for ( ++s; *s && !found; s++ ) {
94 					switch ( *s ) {
95 					case '\\':
96 						esc = 1;
97 						break;
98 					case '"':
99 						if ( !esc )
100 							found = 1;
101 						/* FALL */
102 					default:
103 						esc = 0;
104 						break;
105 					}
106 				}
107 			}
108 
109 			(*map)[i].f_unfriendly = nsldapi_strdup( buf );
110 			(*map)[i].f_friendly = nsldapi_strdup( s );
111 			i++;
112 		}
113 
114 		fclose( fp );
115 		(*map)[i].f_unfriendly = NULL;
116 	}
117 
118 	for ( i = 0; (*map)[i].f_unfriendly != NULL; i++ ) {
119 		if ( strcasecmp( name, (*map)[i].f_unfriendly ) == 0 )
120 			return( (*map)[i].f_friendly );
121 	}
122 	return( name );
123 }
124 
125 
126 void
127 LDAP_CALL
128 ldap_free_friendlymap( FriendlyMap *map )
129 {
130 	struct friendly* pF;
131 
132 	if ( map == NULL || *map == NULL ) {
133 		return;
134 	}
135 
136 	for ( pF = *map; pF->f_unfriendly; pF++ ) {
137 		NSLDAPI_FREE( pF->f_unfriendly );
138 		NSLDAPI_FREE( pF->f_friendly );
139 	}
140 	NSLDAPI_FREE( *map );
141 	*map = NULL;
142 }
143