/src/proftpd/lib/sstrncpy.c
Line | Count | Source (jump to first uncovered line) |
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-2015 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, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA. |
20 | | * |
21 | | * As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu |
22 | | * and other respective copyright holders give permission to link this program |
23 | | * with OpenSSL, and distribute the resulting executable, without including |
24 | | * the source code for OpenSSL in the source distribution. |
25 | | */ |
26 | | |
27 | | #ifdef HAVE_CONFIG_H |
28 | | # include "config.h" |
29 | | #endif |
30 | | |
31 | | #include <errno.h> |
32 | | #include <stdlib.h> |
33 | | #include <stdio.h> |
34 | | #include <stdarg.h> |
35 | | |
36 | | #ifdef HAVE_STRING_H |
37 | | # include <string.h> |
38 | | #endif |
39 | | |
40 | | #include "base.h" |
41 | | |
42 | | /* "safe" strncpy, saves room for \0 at end of dest, and refuses to copy |
43 | | * more than "n" bytes. Returns the number of bytes copied, or -1 if there |
44 | | * was an error. |
45 | | */ |
46 | 0 | int sstrncpy(char *dst, const char *src, size_t n) { |
47 | 0 | register char *d; |
48 | 0 | int res = 0; |
49 | |
|
50 | 0 | if (dst == NULL) { |
51 | 0 | errno = EINVAL; |
52 | 0 | return -1; |
53 | 0 | } |
54 | | |
55 | 0 | if (src == NULL) { |
56 | 0 | errno = EINVAL; |
57 | 0 | return -1; |
58 | 0 | } |
59 | | |
60 | 0 | if (n == 0) { |
61 | 0 | return 0; |
62 | 0 | } |
63 | | |
64 | | /* Avoid attempts to overwrite memory with itself (Bug#4156). */ |
65 | 0 | if (dst == src) { |
66 | 0 | return n; |
67 | 0 | } |
68 | | |
69 | 0 | d = dst; |
70 | 0 | if (src && *src) { |
71 | 0 | for (; *src && n > 1; n--) { |
72 | 0 | *d++ = *src++; |
73 | 0 | res++; |
74 | 0 | } |
75 | 0 | } |
76 | |
|
77 | 0 | *d = '\0'; |
78 | 0 | return res; |
79 | 0 | } |