|
Last change
on this file since 809 was
785,
checked in by alain, 10 years ago
|
|
Avoid a warning in strcpy()
|
-
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* dst = destination; |
|---|
| 14 | do |
|---|
| 15 | { |
|---|
| 16 | *dst = *source; |
|---|
| 17 | dst++; |
|---|
| 18 | source++; |
|---|
| 19 | } while (*source); |
|---|
| 20 | |
|---|
| 21 | *dst = 0; |
|---|
| 22 | |
|---|
| 23 | return destination; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | //////////////////////////////////////////////////// |
|---|
| 28 | char * strncpy(char * dest, const char * src, int n) |
|---|
| 29 | { |
|---|
| 30 | int i; |
|---|
| 31 | for (i = 0; i < n && src[i] != '\0'; i++) |
|---|
| 32 | { |
|---|
| 33 | dest[i] = src[i]; |
|---|
| 34 | } |
|---|
| 35 | for (; i < n ; i++) |
|---|
| 36 | { |
|---|
| 37 | dest[i] = '\0'; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | return dest; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | ///////////////////////////////////////////////// |
|---|
| 45 | int strcmp (const char * str1, const char * str2) |
|---|
| 46 | { |
|---|
| 47 | if (!str1 || !str2) |
|---|
| 48 | { |
|---|
| 49 | return -123456; // return a value out of the char's bounds |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | while (*str1 && *str1 == *str2) |
|---|
| 53 | { |
|---|
| 54 | str1++; |
|---|
| 55 | str2++; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | return (*str1 - *str2); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | ///////////////////////////// |
|---|
| 62 | int strlen (const char * str) |
|---|
| 63 | { |
|---|
| 64 | const char * s = str; |
|---|
| 65 | |
|---|
| 66 | while (*s) |
|---|
| 67 | { |
|---|
| 68 | s++; |
|---|
| 69 | } |
|---|
| 70 | return (s - str); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | ////////////////////////////////////// |
|---|
| 75 | char * strchr(const char * str, int c) |
|---|
| 76 | { |
|---|
| 77 | const char * s = str; |
|---|
| 78 | const char ch = c; |
|---|
| 79 | while (*s != ch && *s != '\0') |
|---|
| 80 | { |
|---|
| 81 | s++; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | return (char *) s; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | // Local Variables: |
|---|
| 89 | // tab-width: 4 |
|---|
| 90 | // c-basic-offset: 4 |
|---|
| 91 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
|---|
| 92 | // indent-tabs-mode: nil |
|---|
| 93 | // End: |
|---|
| 94 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
|---|
Note: See
TracBrowser
for help on using the repository browser.