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