Import upstream 2.3.14
[packages/xinetd.git] / libs / src / str / strutil.c
1 /*
2  * (c) Copyright 1992, 1993 by Panagiotis Tsirigotis
3  * All rights reserved.  The file named COPYRIGHT specifies the terms 
4  * and conditions for redistribution.
5  */
6
7 #include "config.h"
8 #include <string.h>
9 #include <ctype.h>
10
11 #ifndef NULL
12 #define NULL                            0
13 #endif
14
15 #define TRIVIAL_STR_FIND 1
16
17 #ifndef TRIVIAL_STR_FIND
18
19 #define LOWER_CASE( c )                                 ( (c) + 'a' - 'A' )
20
21 /*
22  * look for an instance of sstr in str
23  * Returns a pointer to the beginning of sstr in str.
24  * It ignores the case of the alphabetic characters
25  */
26 char *str_casefind( register char *str, char *sstr )
27 {
28         register int ssfc = *sstr++ ;           /* sub-string first char */
29
30         if ( ssfc == 0 )
31                 return( str ) ;
32
33         if ( isalpha( ssfc ) && isupper( ssfc ) )
34                 ssfc = LOWER_CASE( ssfc ) ;
35
36         while ( *str )
37         {
38                 char *current = str ;
39                 register int strc = *str++ ;
40                 char *sp ;                                                      /* string pointer */
41                 char *ssp ;                                                     /* sub-string pointer */
42
43                 if ( isalpha( strc ) && isupper( strc ) )
44                         strc = LOWER_CASE( strc ) ;
45                 if ( strc != ssfc )
46                         continue ;
47                 
48                 for ( sp = str, ssp = sstr ;; sp++, ssp++ )
49                 {
50                         register int sc = *sp ;                         /* string char */
51                         register int ssc = *ssp ;                       /* substring char */
52
53                         /*
54                          * End-of-substring means we got a match
55                          */
56                         if ( ssc == 0 )
57                                 return( current ) ;
58
59                         /*
60                          * Convert to lower case if alphanumeric
61                          */
62                         if ( isalpha( sc ) && isupper( sc ) )
63                                 sc = LOWER_CASE( sc ) ;
64                         if ( isalpha( ssc ) && isupper( ssc ) )
65                                 ssc = LOWER_CASE( ssc ) ;
66                         if ( sc != ssc )
67                                 break ;
68                 }
69         }
70
71         return( 0 ) ;
72 }
73
74
75 #else           /* defined( TRIVIAL_STR_FIND ) */
76
77 /*
78  * look for an instance of s2 in s1
79  * Returns a pointer to the beginning of s2 in s1.
80  * It ignores the case of the alphabetic characters
81  */
82 char *str_casefind( char *s1, const char *s2 )
83 {
84    unsigned int i ;
85    unsigned long l1 = strlen( s1 ) ;
86    unsigned long l2 = strlen( s2 ) ;
87    
88    if ( l2 > l1 )
89       return( NULL ) ;
90
91    for ( i = 0 ; i < l1 - l2 + 1 ; i++ )
92       if ( strncasecmp( &s1[ i ], s2, l2 ) == 0 )
93          return( (char *) &s1[ i ] ) ;
94    return( NULL ) ;
95 }
96
97 #endif  /* TRIVIAL_STR_FIND */
98
99
100 /*
101  * Fill string s with character c
102  */
103 void str_fill( char *s, char c )
104 {
105         while ( *s ) *s++ = c ;
106 }
107