|
Last change
on this file since 782 was
781,
checked in by alain, 10 years ago
|
|
Fixing two bugs:
1) In the string library: the strcpy() function must copy
the terminating NUL character in the destination buffer.
2) In the malloc library: the free() function must reset
the entry associated to the released buffer in the alloc[array].
|
-
Property svn:executable set to
*
|
|
File size:
1.8 KB
|
| Line | |
|---|
| 1 | ////////////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // File : string.c |
|---|
| 3 | // Date : 23/05/2013 |
|---|
| 4 | // Author : Alexandre JOANNOU, Laurent LAMBERT |
|---|
| 5 | // Copyright (c) UPMC-LIP6 |
|---|
| 6 | /////////////////////////////////////////////////////////////////////////////////// |
|---|
| 7 | |
|---|
| 8 | /////////////////////////////////////////////////////// |
|---|
| 9 | char * strcpy (char * destination, const char * source) |
|---|
| 10 | { |
|---|
| 11 | if (!destination || !source) return destination; |
|---|
| 12 | |
|---|
| 13 | char* src = source; |
|---|
| 14 | char* dst = destination; |
|---|
| 15 | do |
|---|
| 16 | { |
|---|
| 17 | *dst = *src; |
|---|
| 18 | dst++; |
|---|
| 19 | src++; |
|---|
| 20 | } while (*src); |
|---|
| 21 | |
|---|
| 22 | *dst = 0; |
|---|
| 23 | |
|---|
| 24 | return destination; |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | //////////////////////////////////////////////////// |
|---|
| 29 | char * strncpy(char * dest, const char * src, int n) |
|---|
| 30 | { |
|---|
| 31 | int i; |
|---|
| 32 | for (i = 0; i < n && src[i] != '\0'; i++) |
|---|
| 33 | { |
|---|
| 34 | dest[i] = src[i]; |
|---|
| 35 | } |
|---|
| 36 | for (; i < n ; i++) |
|---|
| 37 | { |
|---|
| 38 | dest[i] = '\0'; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | return dest; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | ///////////////////////////////////////////////// |
|---|
| 46 | int strcmp (const char * str1, const char * str2) |
|---|
| 47 | { |
|---|
| 48 | if (!str1 || !str2) |
|---|
| 49 | { |
|---|
| 50 | return -123456; // return a value out of the char's bounds |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | while (*str1 && *str1 == *str2) |
|---|
| 54 | { |
|---|
| 55 | str1++; |
|---|
| 56 | str2++; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | return (*str1 - *str2); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | ///////////////////////////// |
|---|
| 63 | int strlen (const char * str) |
|---|
| 64 | { |
|---|
| 65 | const char * s = str; |
|---|
| 66 | |
|---|
| 67 | while (*s) |
|---|
| 68 | { |
|---|
| 69 | s++; |
|---|
| 70 | } |
|---|
| 71 | return (s - str); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | ////////////////////////////////////// |
|---|
| 76 | char * strchr(const char * str, int c) |
|---|
| 77 | { |
|---|
| 78 | const char * s = str; |
|---|
| 79 | const char ch = c; |
|---|
| 80 | while (*s != ch && *s != '\0') |
|---|
| 81 | { |
|---|
| 82 | s++; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | return (char *) s; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | // Local Variables: |
|---|
| 90 | // tab-width: 4 |
|---|
| 91 | // c-basic-offset: 4 |
|---|
| 92 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
|---|
| 93 | // indent-tabs-mode: nil |
|---|
| 94 | // End: |
|---|
| 95 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
|---|
Note: See
TracBrowser
for help on using the repository browser.