1 /*
2 *
3 * Portions Copyright 1998 Sun Microsystems, Inc. All rights reserved.
4 * Use is subject to license terms.
5 *
6 */
7
8 #pragma ident "%Z%%M% %I% %E% SMI"
9
10 /*
11 * Copyright (c) 1990 Regents of the University of Michigan.
12 * All rights reserved.
13 *
14 * friendly.c
15 */
16
17 #ifndef lint
18 static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
19 #endif
20
21 #include <stdio.h>
22 #include <ctype.h>
23 #include <string.h>
24 #include <stdlib.h> /* malloc(), free() for Solaris */
25 #ifdef MACOS
26 #include <stdlib.h>
27 #include "macos.h"
28 #endif /* MACOS */
29
30 #if defined( DOS ) || defined( _WIN32 )
31 #include <malloc.h>
32 #include "msdos.h"
33 #endif /* DOS */
34
35 #if !defined( MACOS ) && !defined( DOS )
36 #include <errno.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #endif
40
41 #include "lber.h"
42 #include "ldap.h"
43 #include "ldap-private.h"
44 #include "ldap-int.h"
45
46 char *
ldap_friendly_name(char * filename,char * uname,FriendlyMap ** map)47 ldap_friendly_name( char *filename, char *uname, FriendlyMap **map )
48 {
49 int i, entries;
50 FILE *fp;
51 char *s;
52 char buf[BUFSIZ];
53
54 if ( map == NULL ) {
55 #if !defined( MACOS ) && !defined( DOS )
56 errno = EINVAL;
57 #endif
58 return( uname );
59 }
60
61 if ( *map == NULL ) {
62 if ( (fp = fopen( filename, "r" )) == NULL )
63 return( uname );
64
65 entries = 0;
66 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
67 if ( buf[0] != '#' )
68 entries++;
69 }
70 rewind( fp );
71
72 if ( (*map = (FriendlyMap *) malloc( (entries + 1) *
73 sizeof(FriendlyMap) )) == NULL ) {
74 (void) fclose( fp );
75 return( uname );
76 }
77
78 i = 0;
79 while ( fgets( buf, sizeof(buf), fp ) != NULL && i < entries ) {
80 if ( buf[0] == '#' )
81 continue;
82
83 if ( (s = strchr( buf, '\n' )) != NULL )
84 *s = '\0';
85
86 if ( (s = strchr( buf, '\t' )) == NULL )
87 continue;
88 *s++ = '\0';
89
90 if ( *s == '"' ) {
91 int esc = 0, found = 0;
92
93 for ( ++s; *s && !found; s++ ) {
94 switch ( *s ) {
95 case '\\':
96 esc = 1;
97 break;
98 case '"':
99 if ( !esc )
100 found = 1;
101 /* FALL */
102 default:
103 esc = 0;
104 break;
105 }
106 }
107 }
108
109 (*map)[i].f_unfriendly = strdup( buf );
110 (*map)[i].f_friendly = strdup( s );
111 i++;
112 }
113
114 (void) fclose( fp );
115 (*map)[i].f_unfriendly = NULL;
116 }
117
118 for ( i = 0; (*map)[i].f_unfriendly != NULL; i++ ) {
119 if ( strcasecmp( uname, (*map)[i].f_unfriendly ) == 0 )
120 return( (*map)[i].f_friendly );
121 }
122 return( uname );
123 }
124
125
126 void
ldap_free_friendlymap(FriendlyMap ** map)127 ldap_free_friendlymap( FriendlyMap **map )
128 {
129 struct friendly* pF = *map;
130
131 if ( pF == NULL )
132 return;
133
134 while ( pF->f_unfriendly )
135 {
136 free( pF->f_unfriendly );
137 free( pF->f_friendly );
138 pF++;
139 }
140 free( *map );
141 *map = NULL;
142 }
143