/src/proftpd/lib/sstrncpy.c
Line | Count | Source |
1 | | /* |
2 | | * ProFTPD - FTP server daemon |
3 | | * Copyright (c) 1997, 1998 Public Flood Software |
4 | | * Copyright (c) 1999, 2000 MacGyver aka Habeeb J. Dihu <macgyver@tos.net> |
5 | | * Copyright (c) 2001-2026 The ProFTPD Project team |
6 | | * |
7 | | * This program is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 2 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * This program is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
19 | | * |
20 | | * As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu |
21 | | * and other respective copyright holders give permission to link this program |
22 | | * with OpenSSL, and distribute the resulting executable, without including |
23 | | * the source code for OpenSSL in the source distribution. |
24 | | */ |
25 | | |
26 | | #ifdef HAVE_CONFIG_H |
27 | | # include "config.h" |
28 | | #endif |
29 | | |
30 | | #include <errno.h> |
31 | | #include <stdlib.h> |
32 | | #include <stdio.h> |
33 | | #include <stdarg.h> |
34 | | |
35 | | #ifdef HAVE_STRING_H |
36 | | # include <string.h> |
37 | | #endif |
38 | | |
39 | | #include "base.h" |
40 | | |
41 | | /* "safe" strncpy, saves room for \0 at end of dest, and refuses to copy |
42 | | * more than "n" bytes. Returns the number of bytes copied, or -1 if there |
43 | | * was an error. |
44 | | */ |
45 | 0 | int sstrncpy(char *dst, const char *src, size_t n) { |
46 | 0 | register char *d; |
47 | 0 | int res = 0; |
48 | |
|
49 | 0 | if (dst == NULL) { |
50 | 0 | errno = EINVAL; |
51 | 0 | return -1; |
52 | 0 | } |
53 | | |
54 | 0 | if (src == NULL) { |
55 | 0 | errno = EINVAL; |
56 | 0 | return -1; |
57 | 0 | } |
58 | | |
59 | 0 | if (n == 0) { |
60 | 0 | return 0; |
61 | 0 | } |
62 | | |
63 | | /* Avoid attempts to overwrite memory with itself (Bug#4156). */ |
64 | 0 | if (dst == src) { |
65 | 0 | return n; |
66 | 0 | } |
67 | | |
68 | 0 | d = dst; |
69 | 0 | if (src && *src) { |
70 | 0 | for (; *src && n > 1; n--) { |
71 | 0 | *d++ = *src++; |
72 | 0 | res++; |
73 | 0 | } |
74 | 0 | } |
75 | |
|
76 | 0 | *d = '\0'; |
77 | 0 | return res; |
78 | 0 | } |