Back to index

lightning-sunbird  0.9+nobinonly
friendly.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 /*
00038  *  Copyright (c) 1990 Regents of the University of Michigan.
00039  *  All rights reserved.
00040  */
00041 /*
00042  *  friendly.c
00043  */
00044 
00045 #if 0
00046 #ifndef lint 
00047 static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
00048 #endif
00049 #endif
00050 
00051 #include "ldap-int.h"
00052 
00053 char *
00054 LDAP_CALL
00055 ldap_friendly_name( char *filename, char *name, FriendlyMap *map )
00056 {
00057        int    i, entries;
00058        FILE   *fp;
00059        char   *s;
00060        char   buf[BUFSIZ];
00061 
00062        if ( map == NULL ) {
00063               return( name );
00064        }
00065     if ( NULL == name)
00066     {
00067         return (name);
00068     }
00069 
00070        if ( *map == NULL ) {
00071               if ( (fp = fopen( filename, "r" )) == NULL )
00072                      return( name );
00073 
00074               entries = 0;
00075               while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
00076                      if ( buf[0] != '#' )
00077                             entries++;
00078               }
00079               rewind( fp );
00080 
00081               if ( (*map = (FriendlyMap)NSLDAPI_MALLOC( (entries + 1) *
00082                   sizeof(struct friendly) )) == NULL ) {
00083                      fclose( fp );
00084                      return( name );
00085               }
00086 
00087               i = 0;
00088               while ( fgets( buf, sizeof(buf), fp ) != NULL && i < entries ) {
00089                      if ( buf[0] == '#' )
00090                             continue;
00091 
00092                      if ( (s = strchr( buf, '\n' )) != NULL )
00093                             *s = '\0';
00094 
00095                      if ( (s = strchr( buf, '\t' )) == NULL )
00096                             continue;
00097                      *s++ = '\0';
00098 
00099                      if ( *s == '"' ) {
00100                             int    esc = 0, found = 0;
00101 
00102                             for ( ++s; *s && !found; s++ ) {
00103                                    switch ( *s ) {
00104                                    case '\\':
00105                                           esc = 1;
00106                                           break;
00107                                    case '"':
00108                                           if ( !esc )
00109                                                  found = 1;
00110                                           /* FALL */
00111                                    default:
00112                                           esc = 0;
00113                                           break;
00114                                    }
00115                             }
00116                      }
00117 
00118                      (*map)[i].f_unfriendly = nsldapi_strdup( buf );
00119                      (*map)[i].f_friendly = nsldapi_strdup( s );
00120                      i++;
00121               }
00122 
00123               fclose( fp );
00124               (*map)[i].f_unfriendly = NULL;
00125        }
00126 
00127        for ( i = 0; (*map)[i].f_unfriendly != NULL; i++ ) {
00128               if ( strcasecmp( name, (*map)[i].f_unfriendly ) == 0 )
00129                      return( (*map)[i].f_friendly );
00130        }
00131        return( name );
00132 }
00133 
00134 
00135 void
00136 LDAP_CALL
00137 ldap_free_friendlymap( FriendlyMap *map )
00138 {
00139        struct friendly* pF;
00140 
00141        if ( map == NULL || *map == NULL ) {
00142               return;
00143        }
00144 
00145        for ( pF = *map; pF->f_unfriendly; pF++ ) {
00146               NSLDAPI_FREE( pF->f_unfriendly );
00147               NSLDAPI_FREE( pF->f_friendly );
00148        }
00149        NSLDAPI_FREE( *map );
00150        *map = NULL;
00151 }