1N/A/*
1N/A *
1N/A * Portions Copyright 1998 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A *
1N/A */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A/*
1N/A * Copyright (c) 1990 Regents of the University of Michigan.
1N/A * All rights reserved.
1N/A *
1N/A * friendly.c
1N/A */
1N/A
1N/A#ifndef lint
1N/Astatic char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
1N/A#endif
1N/A
1N/A#include <stdio.h>
1N/A#include <ctype.h>
1N/A#include <string.h>
1N/A#include <stdlib.h> /* malloc(), free() for Solaris */
1N/A#ifdef MACOS
1N/A#include <stdlib.h>
1N/A#include "macos.h"
1N/A#endif /* MACOS */
1N/A
1N/A#if defined( DOS ) || defined( _WIN32 )
1N/A#include <malloc.h>
1N/A#include "msdos.h"
1N/A#endif /* DOS */
1N/A
1N/A#if !defined( MACOS ) && !defined( DOS )
1N/A#include <errno.h>
1N/A#include <sys/types.h>
1N/A#include <sys/socket.h>
1N/A#endif
1N/A
1N/A#include "lber.h"
1N/A#include "ldap.h"
1N/A#include "ldap-private.h"
1N/A#include "ldap-int.h"
1N/A
1N/Achar *
1N/Aldap_friendly_name( char *filename, char *uname, FriendlyMap **map )
1N/A{
1N/A int i, entries;
1N/A FILE *fp;
1N/A char *s;
1N/A char buf[BUFSIZ];
1N/A
1N/A if ( map == NULL ) {
1N/A#if !defined( MACOS ) && !defined( DOS )
1N/A errno = EINVAL;
1N/A#endif
1N/A return( uname );
1N/A }
1N/A
1N/A if ( *map == NULL ) {
1N/A if ( (fp = fopen( filename, "r" )) == NULL )
1N/A return( uname );
1N/A
1N/A entries = 0;
1N/A while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
1N/A if ( buf[0] != '#' )
1N/A entries++;
1N/A }
1N/A rewind( fp );
1N/A
1N/A if ( (*map = (FriendlyMap *) malloc( (entries + 1) *
1N/A sizeof(FriendlyMap) )) == NULL ) {
1N/A (void) fclose( fp );
1N/A return( uname );
1N/A }
1N/A
1N/A i = 0;
1N/A while ( fgets( buf, sizeof(buf), fp ) != NULL && i < entries ) {
1N/A if ( buf[0] == '#' )
1N/A continue;
1N/A
1N/A if ( (s = strchr( buf, '\n' )) != NULL )
1N/A *s = '\0';
1N/A
1N/A if ( (s = strchr( buf, '\t' )) == NULL )
1N/A continue;
1N/A *s++ = '\0';
1N/A
1N/A if ( *s == '"' ) {
1N/A int esc = 0, found = 0;
1N/A
1N/A for ( ++s; *s && !found; s++ ) {
1N/A switch ( *s ) {
1N/A case '\\':
1N/A esc = 1;
1N/A break;
1N/A case '"':
1N/A if ( !esc )
1N/A found = 1;
1N/A /* FALL */
1N/A default:
1N/A esc = 0;
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A
1N/A (*map)[i].f_unfriendly = strdup( buf );
1N/A (*map)[i].f_friendly = strdup( s );
1N/A i++;
1N/A }
1N/A
1N/A (void) fclose( fp );
1N/A (*map)[i].f_unfriendly = NULL;
1N/A }
1N/A
1N/A for ( i = 0; (*map)[i].f_unfriendly != NULL; i++ ) {
1N/A if ( strcasecmp( uname, (*map)[i].f_unfriendly ) == 0 )
1N/A return( (*map)[i].f_friendly );
1N/A }
1N/A return( uname );
1N/A}
1N/A
1N/A
1N/Avoid
1N/Aldap_free_friendlymap( FriendlyMap **map )
1N/A{
1N/A struct friendly* pF = *map;
1N/A
1N/A if ( pF == NULL )
1N/A return;
1N/A
1N/A while ( pF->f_unfriendly )
1N/A {
1N/A free( pF->f_unfriendly );
1N/A free( pF->f_friendly );
1N/A pF++;
1N/A }
1N/A free( *map );
1N/A *map = NULL;
1N/A}