source: trunk/libs/mini-libc/string.c @ 619

Last change on this file since 619 was 619, checked in by alain, 5 years ago

1) Fix a bug in KSH : after the "load" command,

the [ksh] prompt is now printed after completion
of the loaded application.

2) Fix a bug in vmm_handle_cow() : the copy-on-write

use now a hal_remote_memcpy() to replicate the page content.


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