]> git.sur5r.net Git - openldap/blob - libraries/libldap/friendly.c
Update copyright of build environment, includes, and liblber/libldap.
[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 "lber.h"
24 #include "ldap.h"
25 #include "ldap-int.h"
26
27 char *
28 ldap_friendly_name( char *filename, char *uname, FriendlyMap **map )
29 {
30         int     i, entries;
31         FILE    *fp;
32         char    *s;
33         char    buf[BUFSIZ];
34
35         if ( map == NULL ) {
36                 errno = EINVAL;
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 = ldap_strdup( buf );
89                         (*map)[i].f_friendly   = ldap_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 }