Statistics
| Branch: | Revision:

scoutos / prex-0.9.0 / bsp / drv / lib / string.c @ 03e9c04a

History | View | Annotate | Download (2.89 KB)

1
/*-
2
 * Copyright (c) 2005-2007, Kohsuke Ohtani
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 * 3. Neither the name of the author nor the names of any co-contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29

    
30
#include <sys/types.h>
31
#include <ddi.h>
32

    
33
/*
34
 * Convert string to long integer
35
 * This version does not support minus value.
36
 */
37
long
38
atol(const char *str)
39
{
40
        long val = 0;
41

    
42
        while (*str == ' ')
43
                str++;
44
        while (*str >= '0' && *str <= '9') {
45
                val *= 10;
46
                val += (*str - '0');
47
                str++;
48
        }
49
        return val;
50
}
51

    
52
size_t
53
strnlen(const char *str, size_t count)
54
{
55
        const char *tmp;
56

    
57
        for (tmp = str; count-- && *tmp != '\0'; ++tmp);
58
        return (size_t)(tmp - str);
59
}
60

    
61
char *
62
strncpy(char *dest, const char *src, size_t count)
63
{
64
        char *tmp = dest;
65

    
66
        while (count-- && (*dest++ = *src++) != '\0');
67
        return tmp;
68
}
69

    
70
int
71
strncmp(const char *src, const char *tgt, size_t count)
72
{
73
        signed char res = 0;
74

    
75
        while (count) {
76
                if ((res = *src - *tgt++) != 0 || !*src++)
77
                        break;
78
                count--;
79
        }
80
        return res;
81
}
82

    
83
/* Safer version of strncpy */
84
size_t
85
strlcpy(char *dest, const char *src, size_t count)
86
{
87
        const char *p = src;
88

    
89
        while (count-- && (*dest++ = *src++) != '\0');
90

    
91
        if (count == 0) {
92
                *dest = '\0';
93
                while (*src++);
94
        }
95
        return (size_t)(src - p - 1); /* count does not include NULL */
96
}
97

    
98
void *
99
memcpy(void *dest, const void *src, size_t count)
100
{
101
        char *tmp = (char *)dest, *s = (char *)src;
102

    
103
        ASSERT(count != 0);
104

    
105
        while (count--)
106
                *tmp++ = *s++;
107
        return dest;
108
}
109

    
110
void *
111
memset(void *dest, int ch, size_t count)
112
{
113
        char *p = (char *)dest;
114

    
115
        ASSERT(count != 0);
116

    
117
        while (count--)
118
                *p++ = (char)ch;
119
        return dest;
120
}