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