DRC | 2ff39b8 | 2011-07-28 08:38:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * "$Id: flstring.c 7903 2010-11-28 21:06:39Z matt $" |
| 3 | * |
| 4 | * BSD string functions for the Fast Light Tool Kit (FLTK). |
| 5 | * |
| 6 | * Copyright 1998-2010 by Bill Spitzak and others. |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Library General Public |
| 10 | * License as published by the Free Software Foundation; either |
| 11 | * version 2 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This library 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 GNU |
| 16 | * Library General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Library General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 21 | * USA. |
| 22 | * |
| 23 | * Please report all bugs and problems on the following page: |
| 24 | * |
| 25 | * http://www.fltk.org/str.php |
| 26 | */ |
| 27 | |
| 28 | #include "flstring.h" |
| 29 | |
| 30 | |
| 31 | /* |
| 32 | * 'fl_strlcat()' - Safely concatenate two strings. |
| 33 | */ |
| 34 | |
| 35 | size_t /* O - Length of string */ |
| 36 | fl_strlcat(char *dst, /* O - Destination string */ |
| 37 | const char *src, /* I - Source string */ |
| 38 | size_t size) { /* I - Size of destination string buffer */ |
| 39 | size_t srclen; /* Length of source string */ |
| 40 | size_t dstlen; /* Length of destination string */ |
| 41 | |
| 42 | |
| 43 | /* |
| 44 | * Figure out how much room is left... |
| 45 | */ |
| 46 | |
| 47 | dstlen = strlen(dst); |
| 48 | size -= dstlen + 1; |
| 49 | |
| 50 | if (!size) return (dstlen); /* No room, return immediately... */ |
| 51 | |
| 52 | /* |
| 53 | * Figure out how much room is needed... |
| 54 | */ |
| 55 | |
| 56 | srclen = strlen(src); |
| 57 | |
| 58 | /* |
| 59 | * Copy the appropriate amount... |
| 60 | */ |
| 61 | |
| 62 | if (srclen > size) srclen = size; |
| 63 | |
| 64 | memcpy(dst + dstlen, src, srclen); |
| 65 | dst[dstlen + srclen] = '\0'; |
| 66 | |
| 67 | return (dstlen + srclen); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /* |
| 72 | * 'fl_strlcpy()' - Safely copy two strings. |
| 73 | */ |
| 74 | |
| 75 | size_t /* O - Length of string */ |
| 76 | fl_strlcpy(char *dst, /* O - Destination string */ |
| 77 | const char *src, /* I - Source string */ |
| 78 | size_t size) { /* I - Size of destination string buffer */ |
| 79 | size_t srclen; /* Length of source string */ |
| 80 | |
| 81 | |
| 82 | /* |
| 83 | * Figure out how much room is needed... |
| 84 | */ |
| 85 | |
| 86 | size --; |
| 87 | |
| 88 | srclen = strlen(src); |
| 89 | |
| 90 | /* |
| 91 | * Copy the appropriate amount... |
| 92 | */ |
| 93 | |
| 94 | if (srclen > size) srclen = size; |
| 95 | |
| 96 | memcpy(dst, src, srclen); |
| 97 | dst[srclen] = '\0'; |
| 98 | |
| 99 | return (srclen); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /* |
| 104 | * End of "$Id: flstring.c 7903 2010-11-28 21:06:39Z matt $". |
| 105 | */ |