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 | */ |
| 9 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
| 11 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 12 | /* Like strncpy but make sure the resulting string is always 0 terminated. */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 13 | 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] | 14 | { |
Denis Vlasenko | de59c0f | 2006-10-05 22:50:22 +0000 | [diff] [blame] | 15 | if (!size) return dst; |
| 16 | dst[--size] = '\0'; |
| 17 | return strncpy(dst, src, size); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 18 | } |
Denis Vlasenko | 0f293b9 | 2008-07-22 20:16:55 +0000 | [diff] [blame] | 19 | |
| 20 | /* Like strcpy but can copy overlapping strings. */ |
| 21 | void FAST_FUNC overlapping_strcpy(char *dst, const char *src) |
| 22 | { |
Denys Vlasenko | 0016bce | 2010-10-19 23:07:49 +0200 | [diff] [blame] | 23 | /* Cheap optimization for dst == src case - |
| 24 | * better to have it here than in many callers. |
| 25 | */ |
| 26 | if (dst != src) { |
| 27 | while ((*dst = *src) != '\0') { |
| 28 | dst++; |
| 29 | src++; |
| 30 | } |
Denis Vlasenko | 0f293b9 | 2008-07-22 20:16:55 +0000 | [diff] [blame] | 31 | } |
| 32 | } |