Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
| 10 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 11 | /* Like strncpy but make sure the resulting string is always 0 terminated. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 12 | char* FAST_FUNC safe_strncpy(char *dst, const char *src, size_t size) |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 13 | { |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 14 | if (!size) return dst; |
| 15 | dst[--size] = '\0'; |
| 16 | return strncpy(dst, src, size); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 17 | } |
Denis Vlasenko | 0f293b9 | 2008-07-22 20:16:55 +0000 | [diff] [blame] | 18 | |
| 19 | /* Like strcpy but can copy overlapping strings. */ |
| 20 | void FAST_FUNC overlapping_strcpy(char *dst, const char *src) |
| 21 | { |
Denys Vlasenko | 0016bce | 2010-10-19 23:07:49 +0200 | [diff] [blame] | 22 | /* Cheap optimization for dst == src case - |
| 23 | * better to have it here than in many callers. |
| 24 | */ |
| 25 | if (dst != src) { |
| 26 | while ((*dst = *src) != '\0') { |
| 27 | dst++; |
| 28 | src++; |
| 29 | } |
Denis Vlasenko | 0f293b9 | 2008-07-22 20:16:55 +0000 | [diff] [blame] | 30 | } |
| 31 | } |