]> git.sur5r.net Git - openldap/blob - libraries/libldap/friendly.c
Updates regarding threads and names.
[openldap] / libraries / libldap / friendly.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*  Portions
6  *  Copyright (c) 1990 Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  friendly.c
10  */
11
12 #include "portable.h"
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 "ldap-int.h"
24
25 char *
26 ldap_friendly_name( char *filename, char *uname, LDAPFriendlyMap **map )
27 {
28         int     i, entries;
29         FILE    *fp;
30         char    *s;
31         char    buf[BUFSIZ];
32
33         if ( map == NULL ) {
34                 errno = EINVAL;
35                 return( uname );
36         }
37
38         if ( *map == NULL ) {
39                 if ( (fp = fopen( filename, "r" )) == NULL )
40                         return( uname );
41
42                 entries = 0;
43                 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
44                         if ( buf[0] != '#' )
45                                 entries++;
46                 }
47                 rewind( fp );
48
49                 if ( (*map = (LDAPFriendlyMap *) malloc( (entries + 1) *
50                     sizeof(LDAPFriendlyMap) )) == NULL ) {
51                         fclose( fp );
52                         return( uname );
53                 }
54
55                 i = 0;
56                 while ( fgets( buf, sizeof(buf), fp ) != NULL && i < entries ) {
57                         if ( buf[0] == '#' )
58                                 continue;
59
60                         if ( (s = strchr( buf, '\n' )) != NULL )
61                                 *s = '\0';
62
63                         if ( (s = strchr( buf, '\t' )) == NULL )
64                                 continue;
65                         *s++ = '\0';
66
67                         if ( *s == '"' ) {
68                                 int     esc = 0, found = 0;
69
70                                 for ( ++s; *s && !found; s++ ) {
71                                         switch ( *s ) {
72                                         case '\\':
73                                                 esc = 1;
74                                                 break;
75                                         case '"':
76                                                 if ( !esc )
77                                                         found = 1;
78                                                 /* FALL */
79                                         default:
80                                                 esc = 0;
81                                                 break;
82                                         }
83                                 }
84                         }
85
86                         (*map)[i].lf_unfriendly = strdup( buf );
87                         (*map)[i].lf_friendly   = strdup( s );
88                         i++;
89                 }
90
91                 fclose( fp );
92                 (*map)[i].lf_unfriendly = NULL;
93         }
94
95         for ( i = 0; (*map)[i].lf_unfriendly != NULL; i++ ) {
96                 if ( strcasecmp( uname, (*map)[i].lf_unfriendly ) == 0 )
97                         return( (*map)[i].lf_friendly );
98         }
99         return( uname );
100 }
101
102
103 void
104 ldap_free_friendlymap( LDAPFriendlyMap **map )
105 {
106         LDAPFriendlyMap* pF = *map;
107
108         if ( pF == NULL )
109                 return;
110
111         while ( pF->lf_unfriendly )
112         {
113                 free( pF->lf_unfriendly );
114                 free( pF->lf_friendly );
115                 pF++;
116         }
117         free( *map );
118         *map = NULL;
119 }