]> git.sur5r.net Git - openldap/blob - libraries/libldap/friendly.c
1bf6c4403388800d05b120b953c058655b778e9b
[openldap] / libraries / libldap / friendly.c
1 /*
2  *  Copyright (c) 1990 Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  friendly.c
6  */
7
8 #include "portable.h"
9
10 #ifndef lint 
11 static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
12 #endif
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <ctype.h>
17
18 #include <ac/errno.h>
19 #include <ac/socket.h>
20 #include <ac/string.h>
21
22 #include "lber.h"
23 #include "ldap.h"
24
25 char *
26 ldap_friendly_name( char *filename, char *uname, FriendlyMap **map )
27 {
28         int     i, entries;
29         FILE    *fp;
30         char    *s;
31         char    buf[BUFSIZ];
32
33         if ( map == NULL ) {
34 #if !defined( MACOS ) && !defined( DOS )
35                 errno = EINVAL;
36 #endif
37                 return( uname );
38         }
39
40         if ( *map == NULL ) {
41                 if ( (fp = fopen( filename, "r" )) == NULL )
42                         return( uname );
43
44                 entries = 0;
45                 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
46                         if ( buf[0] != '#' )
47                                 entries++;
48                 }
49                 rewind( fp );
50
51                 if ( (*map = (FriendlyMap *) malloc( (entries + 1) *
52                     sizeof(FriendlyMap) )) == NULL ) {
53                         fclose( fp );
54                         return( uname );
55                 }
56
57                 i = 0;
58                 while ( fgets( buf, sizeof(buf), fp ) != NULL && i < entries ) {
59                         if ( buf[0] == '#' )
60                                 continue;
61
62                         if ( (s = strchr( buf, '\n' )) != NULL )
63                                 *s = '\0';
64
65                         if ( (s = strchr( buf, '\t' )) == NULL )
66                                 continue;
67                         *s++ = '\0';
68
69                         if ( *s == '"' ) {
70                                 int     esc = 0, found = 0;
71
72                                 for ( ++s; *s && !found; s++ ) {
73                                         switch ( *s ) {
74                                         case '\\':
75                                                 esc = 1;
76                                                 break;
77                                         case '"':
78                                                 if ( !esc )
79                                                         found = 1;
80                                                 /* FALL */
81                                         default:
82                                                 esc = 0;
83                                                 break;
84                                         }
85                                 }
86                         }
87
88                         (*map)[i].f_unfriendly = strdup( buf );
89                         (*map)[i].f_friendly = strdup( s );
90                         i++;
91                 }
92
93                 fclose( fp );
94                 (*map)[i].f_unfriendly = NULL;
95         }
96
97         for ( i = 0; (*map)[i].f_unfriendly != NULL; i++ ) {
98                 if ( strcasecmp( uname, (*map)[i].f_unfriendly ) == 0 )
99                         return( (*map)[i].f_friendly );
100         }
101         return( uname );
102 }
103
104
105 void
106 ldap_free_friendlymap( FriendlyMap **map )
107 {
108         struct friendly* pF = *map;
109
110         if ( pF == NULL )
111                 return;
112
113         while ( pF->f_unfriendly )
114         {
115                 free( pF->f_unfriendly );
116                 free( pF->f_friendly );
117                 pF++;
118         }
119         free( *map );
120         *map = NULL;
121 }