OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [bootloaders/] [orpmon/] [common/] [string.c] - Blame information for rev 610

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 marcus.erl
/*
2
    string.h -- String manipulation
3
    Implements (some) of the standard string routines
4
    Copyright (C) 2002 Richard Herveille, rherveille@opencores.org
5
 
6
    This file is part of OpenRISC 1000 Reference Platform Monitor (ORPmon)
7
 
8
    This program is free software; you can redistribute it and/or modify
9
    it under the terms of the GNU General Public License as published by
10
    the Free Software Foundation; either version 2 of the License, or
11
    (at your option) any later version
12
 
13
    This program is distributed in the hope that it will be useful,
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
    GNU General Public License for more details.
17
 
18
    You should have received a copy of the GNU General Public License
19
    along with this program; if not, write to the Free Software
20
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
*/
22
 
23
#include <stddef.h>
24
#include "string.h"
25
 
26
/* Basic string functions */
27
 
28
/*
29
  s t r l e n
30
 
31
  returns number of characters in s (not including terminating null character)
32
*/
33
size_t strlen(const char *s)
34
{
35 406 julius
        size_t cnt = 0;
36 2 marcus.erl
 
37 406 julius
        /* count the length of string s, not including the \0 character */
38
        while (*s++)
39
                cnt++;
40 2 marcus.erl
 
41 406 julius
        return cnt;
42 2 marcus.erl
}
43
 
44
/*
45
  s t r c p y
46
 
47
  Copy 'src' to 'dest'. Strings may not overlap.
48
*/
49
char *strcpy(char *dest, const char *src)
50
{
51 406 julius
        char *d = dest;
52 2 marcus.erl
 
53 406 julius
        /* copy src to dest */
54
        while ((*dest++ = *src++)) ;
55 2 marcus.erl
 
56 406 julius
        return d;
57 2 marcus.erl
}
58
 
59
char *strncpy(char *dest, const char *src, size_t n)
60
{
61 406 julius
        char *d = dest;
62 2 marcus.erl
 
63 406 julius
        /* copy src to dest */
64
        while (*src && n) {
65
                *dest++ = *src++;
66
                n--;
67
        }
68 2 marcus.erl
 
69 406 julius
        /* fill the remainder of d with nulls */
70
        while (n--)
71
                *dest++ = '\0';
72 2 marcus.erl
 
73 406 julius
        return d;
74 2 marcus.erl
}
75
 
76
char *strcat(char *dest, const char *src)
77
{
78 406 julius
        char *d = dest;
79 2 marcus.erl
 
80 406 julius
        /* find the end of the destination string */
81
        while (*dest++) ;
82 2 marcus.erl
 
83 406 julius
        /* append the source string to the destination string */
84
        while ((*dest++ = *src++)) ;
85 2 marcus.erl
 
86 406 julius
        return d;
87 2 marcus.erl
}
88
 
89
char *strncat(char *dest, const char *src, size_t n)
90
{
91 406 julius
        char *d = dest;
92 2 marcus.erl
 
93 406 julius
        /* find the end of the destination string */
94
        while (*dest++) ;
95 2 marcus.erl
 
96 406 julius
        /* copy src to dest */
97
        while ((*dest = *src) && n--) {
98
                dest++;
99
                src++;
100
        }
101 2 marcus.erl
 
102 406 julius
        /* add terminating '\0' character */
103
        *dest = '\0';
104 2 marcus.erl
 
105 406 julius
        return d;
106 2 marcus.erl
}
107
 
108
int strcmp(const char *s1, const char *s2)
109
{
110 406 julius
        while (*s1 && (*s1 == *s2)) {
111
                s1++;
112
                s2++;
113
        }
114 2 marcus.erl
 
115 406 julius
        return *s1 - *s2;
116 2 marcus.erl
}
117
 
118
int strncmp(const char *s1, const char *s2, size_t n)
119
{
120 406 julius
        while (*s1 && (*s1 == *s2) && n--) {
121
                s1++;
122
                s2++;
123
        }
124 2 marcus.erl
 
125 406 julius
        return *s1 - *s2;
126 2 marcus.erl
}
127
 
128
char *strchr(const char *s, int c)
129
{
130 406 julius
        /* search for the character c */
131
        while (*s && (*s != c))
132
                s++;
133 2 marcus.erl
 
134 406 julius
        return (char *)s;
135 2 marcus.erl
}
136
 
137
char *strrchr(const char *s, int c)
138
{
139 406 julius
        char *fnd = NULL;
140 2 marcus.erl
 
141 406 julius
        /* search for the character c */
142
        while (*s) {
143
                if (*s == c)
144
                        fnd = (char *)s;
145
                s++;
146
        }
147 2 marcus.erl
 
148 406 julius
        return fnd;
149 2 marcus.erl
}
150
 
151
/* Basic mem functions */
152
void *memcpy(void *dest, const void *src, size_t n)
153
{
154 406 julius
        /* check if 'src' and 'dest' are on LONG boundaries */
155
        if ((sizeof(unsigned long) -
156
             1) & ((unsigned long)dest | (unsigned long)src)) {
157
                /* no, do a byte-wide copy */
158
                char *cs = (char *)src;
159
                char *cd = (char *)dest;
160 2 marcus.erl
 
161 406 julius
                while (n--)
162
                        *cd++ = *cs++;
163
        } else {
164
                /* yes, speed up copy process */
165
                /* copy as many LONGs as possible */
166
                long *ls = (long *)src;
167
                long *ld = (long *)dest;
168 2 marcus.erl
 
169 406 julius
                size_t cnt = n >> 2;
170
                while (cnt--)
171
                        *ld++ = *ls++;
172 2 marcus.erl
 
173 406 julius
                /* finally copy the remaining bytes */
174
                char *cs = (char *)(src + (n & ~0x03));
175
                char *cd = (char *)(dest + (n & ~0x03));
176 2 marcus.erl
 
177 406 julius
                cnt = n & 0x3;
178
                while (cnt--)
179
                        *cd++ = *cs++;
180
        }
181 2 marcus.erl
 
182 406 julius
        return dest;
183 2 marcus.erl
}
184
 
185 140 julius
/* memcpy, return  checksum of bytes copied instead of pointer to dest */
186
unsigned char memcpy_crc(void *dest, const void *src, size_t n)
187
{
188 406 julius
        unsigned char sum = 0;
189
        unsigned char tmp;
190 2 marcus.erl
 
191 406 julius
        char *cs = (char *)src;
192
        char *cd = (char *)dest;
193
 
194
        while (n--) {
195
                tmp = *cs++;
196
                sum += tmp;
197
                *cd++ = tmp;
198
        }
199
        return sum;
200 140 julius
}
201
 
202 2 marcus.erl
void *memmove(void *dest, void *src, size_t n)
203
{
204 406 julius
        char *d = dest;
205
        char *s = src;
206 2 marcus.erl
 
207 406 julius
        while (n--)
208
                *d++ = *s++;
209 2 marcus.erl
 
210 406 julius
        return dest;
211 2 marcus.erl
}
212
 
213
int memcmp(const void *s1, const void *s2, size_t n)
214
{
215 406 julius
        char *p1 = (void *)s1;
216
        char *p2 = (void *)s2;
217 2 marcus.erl
 
218 406 julius
        while ((*p1 == *p2) && n--) {
219
                p1++;
220
                p2++;
221
        }
222 2 marcus.erl
 
223 406 julius
        return *p1 - *p2;
224 2 marcus.erl
}
225
 
226 140 julius
void *memchr(const void *s, char c, size_t n)
227 2 marcus.erl
{
228 406 julius
        char *p = (void *)s;
229 2 marcus.erl
 
230 406 julius
        /* search for the character c */
231
        while ((*p != c) && n--)
232
                p++;
233 2 marcus.erl
 
234 406 julius
        return (*p == c) ? p : NULL;
235 2 marcus.erl
}
236
 
237
void *memset(void *s, int c, size_t n)
238
{
239 406 julius
        char *p = s;
240 2 marcus.erl
 
241 406 julius
        while (n--)
242
                *p++ = c;
243 2 marcus.erl
 
244 406 julius
        return s;
245 2 marcus.erl
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.