source: trunk/kernel/libk/string.c @ 540

Last change on this file since 540 was 457, checked in by alain, 6 years ago

This version modifies the exec syscall and fixes a large number of small bugs.
The version number has been updated (0.1)

File size: 3.6 KB
Line 
1/*
2 * string.c - string helper function implementation.
3 *
4 * Authors      Ghassan Almaless (2007,2009,2010,2011,2012)
5 *              Alain Greiner (2017)
6 *
7 * Copyright (c) UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include <hal_kernel_types.h>
26#include <ctype.h>
27#include <string.h>
28
29///////////////////////////////////
30uint32_t strlen( const char * str )
31{
32        register const char *ptr = str;
33
34        while (*ptr) ptr++;
35
36        return (ptr - str);
37}
38
39///////////////////////////////////
40uint32_t strnlen( const char * str,
41                  uint32_t     maxlen )
42{
43        register const char *ptr = str;
44
45        while (*ptr && (maxlen-- > 0)) ptr++;
46
47        return (ptr - str);
48}
49
50////////////////////////////
51int strcmp( const char * s1,
52            const char * s2 )
53{
54
55        while (*s1 && *s1 == *s2)
56        {
57                s1 ++;
58                s2 ++;
59        }
60
61        return (*s1 - *s2);
62}
63
64/////////////////////////////
65int strncmp( const char * s1,
66             const char * s2,
67             uint32_t     n )
68{ 
69        if (n == 0)
70                return s1 - s2; // XXX ???
71
72        while (*s1 && (*s1 == *s2) && (n > 1))
73        {
74                s1 ++;
75                s2 ++;
76                n--;
77        }
78
79        return (*s1 - *s2);
80}
81
82//////////////////////////////////
83int strcasecmp( const char * str1,
84                const char * str2 )
85{
86        char c1;
87        char c2;
88
89        do{
90                c1 = toupper(*++str1);
91                c2 = toupper(*++str2);
92        }while(c1 && c1 == c2);
93
94        return (c1 - c2);
95}
96
97///////////////////////////
98char * strcpy (char * dest,
99               char * src )
100{
101        char *src_ptr = src;
102        char *dst_ptr = dest;
103
104        while(*src_ptr)
105                *(dst_ptr++) = *(src_ptr++);
106
107        *dst_ptr = 0;
108        return dest;
109}
110
111///////////////////////////////
112char * strncpy( char    * dest,
113                char    * src,
114                uint32_t  n )
115{
116        uint32_t i;
117
118        for (i = 0; (i < n) && (src[i] != '\0') ; i++)
119                dest[i] = src[i];
120
121        for (; i < n; i++)
122                dest[i] = '\0';
123
124        return dest;
125}
126
127////////////////////////////////
128char * strstr( char       * s,
129               const char * find)
130{
131    char     sc;
132    char     c;
133    uint32_t len;
134
135    if ((c = *find++) != 0) {
136        len = strlen(find);
137        do {
138            do {
139                if ((sc = *s++) == 0)
140                    return NULL;
141            } while (sc != c);
142        } while (strncmp(s, find, len) != 0);
143        s--;
144    }
145    return s;
146}
147
148//////////////////////////////
149char * strchr( const char * s,
150               int          c )
151{
152        while(*s && *s != (char) c)
153                s++;
154
155        if(*s == (char) c)
156                return (char*)s;
157
158        return NULL;
159}
160
161///////////////////////////////
162char * strrchr( const char * t,
163                int          c )
164{
165        register char         ch;
166        register const char * l =0;
167
168        ch = c;
169        for (;;) {
170                if (*t == ch)
171                        l=t;
172                if (!*t)
173                        return (char*)l;
174                ++t;
175        }
176        return (char*)l;
177}
178
179/////////////////////////////////////
180inline uint8_t to_lower( uint8_t  c )
181{
182   if (c >= 'A' && c <= 'Z') return (c | 0x20);
183   else                      return c;
184}
185
186
187/////////////////////////////////////
188inline uint8_t to_upper( uint8_t  c )
189{
190   if (c >= 'a' && c <= 'z') return (c & ~(0x20));
191   else                      return c;
192}
193
194
Note: See TracBrowser for help on using the repository browser.