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