Subversion Repositories Kolibri OS

Rev

Rev 6102 | Rev 6936 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6102 Rev 6934
1
/*
1
/*
2
 *  linux/lib/string.c
2
 *  linux/lib/string.c
3
 *
3
 *
4
 *  Copyright (C) 1991, 1992  Linus Torvalds
4
 *  Copyright (C) 1991, 1992  Linus Torvalds
5
 */
5
 */
6
 
6
 
7
/*
7
/*
8
 * stupid library routines.. The optimized versions should generally be found
8
 * stupid library routines.. The optimized versions should generally be found
9
 * as inline code in 
9
 * as inline code in 
10
 *
10
 *
11
 * These are buggy as well..
11
 * These are buggy as well..
12
 *
12
 *
13
 * * Fri Jun 25 1999, Ingo Oeser 
13
 * * Fri Jun 25 1999, Ingo Oeser 
14
 * -  Added strsep() which will replace strtok() soon (because strsep() is
14
 * -  Added strsep() which will replace strtok() soon (because strsep() is
15
 *    reentrant and should be faster). Use only strsep() in new code, please.
15
 *    reentrant and should be faster). Use only strsep() in new code, please.
16
 *
16
 *
17
 * * Sat Feb 09 2002, Jason Thomas ,
17
 * * Sat Feb 09 2002, Jason Thomas ,
18
 *                    Matthew Hawkins 
18
 *                    Matthew Hawkins 
19
 * -  Kissed strtok() goodbye
19
 * -  Kissed strtok() goodbye
20
 */
20
 */
21
 
21
 
22
#include 
22
#include 
23
#include 
23
#include 
24
#include 
24
#include 
-
 
25
#include 
-
 
26
#include 
25
#include 
27
#include 
-
 
28
#include 
-
 
29
 
26
 
30
 
27
 
31
 
28
#ifndef __HAVE_ARCH_STRLCPY
32
#ifndef __HAVE_ARCH_STRLCPY
29
/**
33
/**
30
 * strlcpy - Copy a C-string into a sized buffer
34
 * strlcpy - Copy a C-string into a sized buffer
31
 * @dest: Where to copy the string to
35
 * @dest: Where to copy the string to
32
 * @src: Where to copy the string from
36
 * @src: Where to copy the string from
33
 * @size: size of destination buffer
37
 * @size: size of destination buffer
34
 *
38
 *
35
 * Compatible with *BSD: the result is always a valid
39
 * Compatible with *BSD: the result is always a valid
36
 * NUL-terminated string that fits in the buffer (unless,
40
 * NUL-terminated string that fits in the buffer (unless,
37
 * of course, the buffer size is zero). It does not pad
41
 * of course, the buffer size is zero). It does not pad
38
 * out the result like strncpy() does.
42
 * out the result like strncpy() does.
39
 */
43
 */
40
size_t strlcpy(char *dest, const char *src, size_t size)
44
size_t strlcpy(char *dest, const char *src, size_t size)
41
{
45
{
42
    size_t ret = strlen(src);
46
    size_t ret = strlen(src);
43
 
47
 
44
    if (size) {
48
    if (size) {
45
        size_t len = (ret >= size) ? size - 1 : ret;
49
        size_t len = (ret >= size) ? size - 1 : ret;
46
        memcpy(dest, src, len);
50
        memcpy(dest, src, len);
47
        dest[len] = '\0';
51
        dest[len] = '\0';
48
    }
52
    }
49
    return ret;
53
    return ret;
50
}
54
}
51
 
55
 
52
#ifndef __HAVE_ARCH_STRLCAT
56
#ifndef __HAVE_ARCH_STRLCAT
53
/**
57
/**
54
 * strlcat - Append a length-limited, C-string to another
58
 * strlcat - Append a length-limited, C-string to another
55
 * @dest: The string to be appended to
59
 * @dest: The string to be appended to
56
 * @src: The string to append to it
60
 * @src: The string to append to it
57
 * @count: The size of the destination buffer.
61
 * @count: The size of the destination buffer.
58
 */
62
 */
59
size_t strlcat(char *dest, const char *src, size_t count)
63
size_t strlcat(char *dest, const char *src, size_t count)
60
{
64
{
61
        size_t dsize = strlen(dest);
65
        size_t dsize = strlen(dest);
62
        size_t len = strlen(src);
66
        size_t len = strlen(src);
63
        size_t res = dsize + len;
67
        size_t res = dsize + len;
64
 
68
 
65
        /* This would be a bug */
69
        /* This would be a bug */
66
        BUG_ON(dsize >= count);
70
        BUG_ON(dsize >= count);
67
 
71
 
68
        dest += dsize;
72
        dest += dsize;
69
        count -= dsize;
73
        count -= dsize;
70
        if (len >= count)
74
        if (len >= count)
71
                len = count-1;
75
                len = count-1;
72
        memcpy(dest, src, len);
76
        memcpy(dest, src, len);
73
        dest[len] = 0;
77
        dest[len] = 0;
74
        return res;
78
        return res;
75
}
79
}
76
#endif
80
#endif
77
 
81
 
78
 
82
 
79
#endif
83
#endif