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