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