xref: /onnv-gate/usr/src/lib/libldap5/sources/ldap/common/friendly.c (revision 1914:8a8c5f225b1b)
1*1914Scasper /*
2*1914Scasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
3*1914Scasper  * Use is subject to license terms.
4*1914Scasper  */
50Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
60Sstevel@tonic-gate 
70Sstevel@tonic-gate /*
80Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
90Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
100Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
110Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
140Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
150Sstevel@tonic-gate  * implied. See the License for the specific language governing
160Sstevel@tonic-gate  * rights and limitations under the License.
170Sstevel@tonic-gate  *
180Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
190Sstevel@tonic-gate  * March 31, 1998.
200Sstevel@tonic-gate  *
210Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
220Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
230Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
240Sstevel@tonic-gate  * Rights Reserved.
250Sstevel@tonic-gate  *
260Sstevel@tonic-gate  * Contributor(s):
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  *  Copyright (c) 1990 Regents of the University of Michigan.
300Sstevel@tonic-gate  *  All rights reserved.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  *  friendly.c
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #if 0
370Sstevel@tonic-gate #ifndef lint
380Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
390Sstevel@tonic-gate #endif
400Sstevel@tonic-gate #endif
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include "ldap-int.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate char *
450Sstevel@tonic-gate LDAP_CALL
ldap_friendly_name(char * filename,char * name,FriendlyMap * map)460Sstevel@tonic-gate ldap_friendly_name( char *filename, char *name, FriendlyMap *map )
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	int	i, entries;
490Sstevel@tonic-gate 	FILE	*fp;
500Sstevel@tonic-gate 	char	*s;
510Sstevel@tonic-gate 	char	buf[BUFSIZ];
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	if ( map == NULL ) {
540Sstevel@tonic-gate 		return( name );
550Sstevel@tonic-gate 	}
560Sstevel@tonic-gate     if ( NULL == name)
570Sstevel@tonic-gate     {
580Sstevel@tonic-gate         return (name);
590Sstevel@tonic-gate     }
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	if ( *map == NULL ) {
62*1914Scasper 		if ( (fp = fopen( filename, "rF" )) == NULL )
630Sstevel@tonic-gate 			return( name );
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 		entries = 0;
660Sstevel@tonic-gate 		while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
670Sstevel@tonic-gate 			if ( buf[0] != '#' )
680Sstevel@tonic-gate 				entries++;
690Sstevel@tonic-gate 		}
700Sstevel@tonic-gate 		rewind( fp );
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 		if ( (*map = (FriendlyMap)NSLDAPI_MALLOC( (entries + 1) *
730Sstevel@tonic-gate 		    sizeof(struct friendly) )) == NULL ) {
740Sstevel@tonic-gate 			fclose( fp );
750Sstevel@tonic-gate 			return( name );
760Sstevel@tonic-gate 		}
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 		i = 0;
790Sstevel@tonic-gate 		while ( fgets( buf, sizeof(buf), fp ) != NULL && i < entries ) {
800Sstevel@tonic-gate 			if ( buf[0] == '#' )
810Sstevel@tonic-gate 				continue;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 			if ( (s = strchr( buf, '\n' )) != NULL )
840Sstevel@tonic-gate 				*s = '\0';
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 			if ( (s = strchr( buf, '\t' )) == NULL )
870Sstevel@tonic-gate 				continue;
880Sstevel@tonic-gate 			*s++ = '\0';
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 			if ( *s == '"' ) {
910Sstevel@tonic-gate 				int	esc = 0, found = 0;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 				for ( ++s; *s && !found; s++ ) {
940Sstevel@tonic-gate 					switch ( *s ) {
950Sstevel@tonic-gate 					case '\\':
960Sstevel@tonic-gate 						esc = 1;
970Sstevel@tonic-gate 						break;
980Sstevel@tonic-gate 					case '"':
990Sstevel@tonic-gate 						if ( !esc )
1000Sstevel@tonic-gate 							found = 1;
1010Sstevel@tonic-gate 						/* FALL */
1020Sstevel@tonic-gate 					default:
1030Sstevel@tonic-gate 						esc = 0;
1040Sstevel@tonic-gate 						break;
1050Sstevel@tonic-gate 					}
1060Sstevel@tonic-gate 				}
1070Sstevel@tonic-gate 			}
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 			(*map)[i].f_unfriendly = nsldapi_strdup( buf );
1100Sstevel@tonic-gate 			(*map)[i].f_friendly = nsldapi_strdup( s );
1110Sstevel@tonic-gate 			i++;
1120Sstevel@tonic-gate 		}
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 		fclose( fp );
1150Sstevel@tonic-gate 		(*map)[i].f_unfriendly = NULL;
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	for ( i = 0; (*map)[i].f_unfriendly != NULL; i++ ) {
1190Sstevel@tonic-gate 		if ( strcasecmp( name, (*map)[i].f_unfriendly ) == 0 )
1200Sstevel@tonic-gate 			return( (*map)[i].f_friendly );
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 	return( name );
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate void
1270Sstevel@tonic-gate LDAP_CALL
ldap_free_friendlymap(FriendlyMap * map)1280Sstevel@tonic-gate ldap_free_friendlymap( FriendlyMap *map )
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	struct friendly* pF;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	if ( map == NULL || *map == NULL ) {
1330Sstevel@tonic-gate 		return;
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	for ( pF = *map; pF->f_unfriendly; pF++ ) {
1370Sstevel@tonic-gate 		NSLDAPI_FREE( pF->f_unfriendly );
1380Sstevel@tonic-gate 		NSLDAPI_FREE( pF->f_friendly );
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 	NSLDAPI_FREE( *map );
1410Sstevel@tonic-gate 	*map = NULL;
1420Sstevel@tonic-gate }
143