/* */
This source file includes following definitions.
- strlimcpy
1 /*
2 * Copyright (c) 2002 Tama Communications Corporation
3 *
4 * This file is part of GNU GLOBAL.
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include "die.h"
24 #include "strlimcpy.h"
25
26 /**
27 * strlimcpy: copy string with limit.
28 *
29 * @param[out] dest destination string
30 * @param[in] source source string
31 * @param[in] limit size of @a dest
32 *
33 * @note This function is similar to @NAME{strlcpy()} of @NAME{OpenBSD} but is different
34 * because @NAME{strlimcpy} abort when it beyond the limit.
35 */
36 void
37 strlimcpy(char *dest, const char *const source, const int limit)
38 {
39 int n = (int)limit;
40 const char *s = source;
41
42 while (n--)
43 if (!(*dest++ = *s++))
44 return;
45 die("buffer overflow. strlimcpy(dest, '%s', %d).", source, limit);
46 }
/* */