"Fossies" - the Fresh Open Source Software Archive

Member "ifhp-3.5.22/src/safestrutil.c" (5 Oct 2004, 5684 Bytes) of package /linux/misc/old/ifhp-3.5.22.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "safestrutil.c" see the Fossies "Dox" file reference documentation.

    1 /***************************************************************************
    2  * LPRng - An Extended Print Spooler System
    3  *
    4  * Copyright 1988-2001, Patrick Powell, San Diego, CA
    5  *     papowell@lprng.com
    6  * See LICENSE for conditions of use.
    7  *
    8  ***************************************************************************/
    9 
   10  static char *const _id =
   11 "$Id: safestrutil.c,v 1.2 2002/04/02 04:38:40 papowell Exp papowell $";
   12 
   13 #include "portable.h"
   14 #include "safemalloc.h"
   15 #include "safestrutil.h"
   16 
   17 /**************************************************************
   18  * Bombproof versions of string functions
   19  **************************************************************/
   20 
   21 int safestrlen( const char *s1 )
   22 {
   23     return( s1? strlen(s1) : 0 );
   24 }
   25 
   26 /* case insensitive compare for OS without it */
   27 int safestrcasecmp (const char *s1, const char *s2)
   28 {
   29     int c1, c2, d = 0;
   30     if( (s1 == s2) ) return(0);
   31     if( (s1 == 0 ) && s2 ) return( -1 );
   32     if( s1 && (s2 == 0 ) ) return( 1 );
   33     for (;;) {
   34         c1 = *((unsigned char *)s1); s1++;
   35         c2 = *((unsigned char *)s2); s2++;
   36         if( isupper(c1) ) c1 = tolower(c1);
   37         if( isupper(c2) ) c2 = tolower(c2);
   38         if( (d = (c1 - c2 )) || c1 == 0 ) break;
   39     }
   40     return( d );
   41 }
   42 
   43 /* case insensitive compare for OS without it */
   44 int safestrncasecmp (const char *s1, const char *s2, int len )
   45 {
   46     int c1, c2, d = 0;
   47     if( (s1 == s2) && s1 == 0 ) return(0);
   48     if( (s1 == 0 ) && s2 ) return( -1 );
   49     if( s1 && (s2 == 0 ) ) return( 1 );
   50     for (;len>0;--len){
   51         c1 = *((unsigned char *)s1); s1++;
   52         c2 = *((unsigned char *)s2); s2++;
   53         if( isupper(c1) ) c1 = tolower(c1);
   54         if( isupper(c2) ) c2 = tolower(c2);
   55         if( (d = (c1 - c2 )) || c1 == 0 ) return(d);
   56     }
   57     return( 0 );
   58 }
   59 
   60 /* perform safe comparison, even with null pointers */
   61 int safestrcmp( const char *s1, const char *s2 )
   62 {
   63     if( (s1 == s2) ) return(0);
   64     if( (s1 == 0 ) && s2 ) return( -1 );
   65     if( s1 && (s2 == 0 ) ) return( 1 );
   66     return( strcmp(s1, s2) );
   67 }
   68 
   69 
   70 /* perform safe comparison, even with null pointers */
   71 int safestrncmp( const char *s1, const char *s2, int len )
   72 {
   73     if( (s1 == s2) && s1 == 0 ) return(0);
   74     if( (s1 == 0 ) && s2 ) return( -1 );
   75     if( s1 && (s2 == 0 ) ) return( 1 );
   76     return( strncmp(s1, s2, len) );
   77 }
   78 
   79 
   80 /* perform safe strchr, even with null pointers */
   81 char *safestrchr( const char *s1, int c )
   82 {
   83     if( s1 ) return( strchr( s1, c ) );
   84     return( 0 );
   85 }
   86 
   87 
   88 /* perform safe strrchr, even with null pointers */
   89 char *safestrrchr( const char *s1, int c )
   90 {
   91     if( s1 ) return( strrchr( s1, c ) );
   92     return( 0 );
   93 }
   94 
   95 
   96 /* perform safe strchr, even with null pointers */
   97 char *safestrpbrk( const char *s1, const char *s2 )
   98 {
   99     if( s1 && s2 ) return( strpbrk( s1, s2 ) );
  100     return( 0 );
  101 }
  102 
  103 
  104 /* perform safe strstr, even with null pointers */
  105 char *safestrstr( const char *s1, const char *s2 )
  106 {
  107     if( s1 && s2 ) return( strstr( s1, s2 ) );
  108     return( 0 );
  109 }
  110 
  111 /* lowercase and uppercase (destructive) a string */
  112 void lowercase( char *s )
  113 {
  114     int c;
  115     if( s ){
  116         for( ; (c = cval(s)); ++s ){
  117             if( isupper(c) ) *s = tolower(c);
  118         }
  119     }
  120 }
  121 void uppercase( char *s )
  122 {
  123     int c;
  124     if( s ){
  125         for( ; (c = cval(s)); ++s ){
  126             if( islower(c) ) *s = toupper(c);
  127         }
  128     }
  129 }
  130 
  131 /*
  132  * Trunc str - remove trailing white space (destructive)
  133  */
  134 
  135 char *trunc_str( char *s)
  136 {
  137     int len = safestrlen(s);
  138     if( len ){
  139         while( len > 0 && isspace(cval(s+len-1)) ) --len;
  140         s[len] = 0;
  141     }
  142     return( s );
  143 }
  144 
  145 int Lastchar( char *s )
  146 {
  147     int c = 0, n = safestrlen(s);
  148     if( n ){
  149         c = cval(s+n-1);
  150     }
  151     return(c);
  152 }
  153 
  154 #define UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  155 #define LOWER "abcdefghijklmnopqrstuvwxyz"
  156 #define DIGIT "01234567890"
  157 #define SAFE "-_."
  158 #define LESS_SAFE SAFE "@/:()=,+-%"
  159 
  160 const char *Is_clean_name( const char *s )
  161 {
  162     int c;
  163     if( s ){
  164         for( ; (c = cval(s)); ++s ){
  165             if( !(isalnum(c) || safestrchr( SAFE, c )) ) return( s );
  166         }
  167     }
  168     return( 0 );
  169 }
  170 
  171 void Clean_name( char *s )
  172 {
  173     int c;
  174     if( s ){
  175         for( ; (c = cval(s)); ++s ){
  176             if( !(isalnum(c) || safestrchr( SAFE, c )) ) *s = '_';
  177         }
  178     }
  179 }
  180 
  181 /*
  182  * Find a possible bad character in a line
  183  */
  184 
  185 int Is_meta( int c, const char * safe_chars )
  186 {
  187     return( !( isspace(c) || isalnum( c )
  188         || (safe_chars && safestrchr(safe_chars,c))
  189         || safestrchr( LESS_SAFE, c ) ) );
  190 }
  191 
  192 const char *Find_meta( const char *s, const char *safe_chars )
  193 {
  194     int c = 0;
  195     if( s ){
  196         for( ; (c = cval(s)); ++s ){
  197             if( Is_meta( c, safe_chars ) ) return( s );
  198         }
  199         s = 0;
  200     }
  201     return( s );
  202 }
  203 
  204 void Clean_meta( char *t, const char *safe_chars )
  205 {
  206     char *s = t;
  207     if( t ){
  208         while( (s = safestrchr(s,'\\')) ) *s = '/';
  209         s = t;
  210         for( s = t; (s =(char *)Find_meta( s, safe_chars )); ++s ){
  211             *s = '_';
  212         }
  213     }
  214 }
  215 
  216 
  217 /*
  218  * char *Make_pathname( char *dir, char *filename )
  219  *  - makes a full pathname from the dir and file part
  220  */
  221 
  222 char *Make_pathname( const char *dir,  const char *filename, MEMPASS )
  223 {
  224     char *s, *path = 0;
  225     if( filename == 0 ){
  226         path = 0;
  227     } else if( filename[0] == '/' ){
  228         path = safestrdup(filename,MEMPASSED);
  229     } else if( dir ){
  230         path = safestrdup3(dir,"/",filename,MEMPASSED);
  231     } else {
  232         path = safestrdup2("./",filename,MEMPASSED);
  233     }
  234     if( (s = path) ) while((s = strstr(s,"//"))) memmove(s,s+1,safestrlen(s)+1 );
  235     return(path);
  236 }
  237 
  238 /*
  239  * safeunlink - this is a silly place to put it...
  240  */
  241 int safeunlink( const char *s )
  242 {
  243     if( s ) return( unlink(s) );
  244     return(0);
  245 }
  246 
  247 /*
  248   safer versions of strncat and strncp
  249  */
  250 char *mystrncat( char *s1, const char *s2, int len )
  251 {
  252     int size;
  253     if( len > 0 ){
  254         s1[len-1] = 0;
  255         size = safestrlen( s1 );
  256         len -= size;
  257         if( s2 && len > 0  ){
  258             strncpy( s1+size, s2, len-1 );
  259         }
  260     }
  261     return( s1 );
  262 }
  263 char *mystrncpy( char *s1, const char *s2, int len )
  264 {
  265     --len;
  266     s1[0] = 0;
  267     if( s2 && len > 0 ){
  268         strncpy( s1, s2, len );
  269         s1[len] = 0;
  270     }
  271     return( s1 );
  272 }