]> git.sur5r.net Git - openldap/blob - libraries/libldap/friendly.c
fc90fc9cdc567fd63256893c4cba08e95354675b
[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 #include <ac/time.h>
22
23 #include "lber.h"
24 #include "ldap.h"
25
26 char *
27 ldap_friendly_name( char *filename, char *uname, FriendlyMap **map )
28 {
29         int     i, entries;
30         FILE    *fp;
31         char    *s;
32         char    buf[BUFSIZ];
33
34         if ( map == NULL ) {
35 #if !defined( MACOS ) && !defined( DOS )
36                 errno = EINVAL;
37 #endif
38                 return( uname );
39         }
40
41         if ( *map == NULL ) {
42                 if ( (fp = fopen( filename, "r" )) == NULL )
43                         return( uname );
44
45                 entries = 0;
46                 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
47                         if ( buf[0] != '#' )
48                                 entries++;
49                 }
50                 rewind( fp );
51
52                 if ( (*map = (FriendlyMap *) malloc( (entries + 1) *
53                     sizeof(FriendlyMap) )) == NULL ) {
54                         fclose( fp );
55                         return( uname );
56                 }
57
58                 i = 0;
59                 while ( fgets( buf, sizeof(buf), fp ) != NULL && i < entries ) {
60                         if ( buf[0] == '#' )
61                                 continue;
62
63                         if ( (s = strchr( buf, '\n' )) != NULL )
64                                 *s = '\0';
65
66                         if ( (s = strchr( buf, '\t' )) == NULL )
67                                 continue;
68                         *s++ = '\0';
69
70                         if ( *s == '"' ) {
71                                 int     esc = 0, found = 0;
72
73                                 for ( ++s; *s && !found; s++ ) {
74                                         switch ( *s ) {
75                                         case '\\':
76                                                 esc = 1;
77                                                 break;
78                                         case '"':
79                                                 if ( !esc )
80                                                         found = 1;
81                                                 /* FALL */
82                                         default:
83                                                 esc = 0;
84                                                 break;
85                                         }
86                                 }
87                         }
88
89                         (*map)[i].f_unfriendly = strdup( buf );
90                         (*map)[i].f_friendly = strdup( s );
91                         i++;
92                 }
93
94                 fclose( fp );
95                 (*map)[i].f_unfriendly = NULL;
96         }
97
98         for ( i = 0; (*map)[i].f_unfriendly != NULL; i++ ) {
99                 if ( strcasecmp( uname, (*map)[i].f_unfriendly ) == 0 )
100                         return( (*map)[i].f_friendly );
101         }
102         return( uname );
103 }
104
105
106 void
107 ldap_free_friendlymap( FriendlyMap **map )
108 {
109         struct friendly* pF = *map;
110
111         if ( pF == NULL )
112                 return;
113
114         while ( pF->f_unfriendly )
115         {
116                 free( pF->f_unfriendly );
117                 free( pF->f_friendly );
118                 pF++;
119         }
120         free( *map );
121         *map = NULL;
122 }