Mercurial > hg > CbC > CbC_gcc
comparison libiberty/xstrndup.c @ 111:04ced10e8804
gcc 7
author | kono |
---|---|
date | Fri, 27 Oct 2017 22:46:09 +0900 |
parents | a06113de4d67 |
children | 84e7813d76e9 |
comparison
equal
deleted
inserted
replaced
68:561a7518be6b | 111:04ced10e8804 |
---|---|
1 /* Implement the xstrndup function. | 1 /* Implement the xstrndup function. |
2 Copyright (C) 2005 Free Software Foundation, Inc. | 2 Copyright (C) 2005-2017 Free Software Foundation, Inc. |
3 Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>. | 3 Written by Kaveh R. Ghazi <ghazi@caip.rutgers.edu>. |
4 | 4 |
5 This file is part of the libiberty library. | 5 This file is part of the libiberty library. |
6 Libiberty is free software; you can redistribute it and/or | 6 Libiberty is free software; you can redistribute it and/or |
7 modify it under the terms of the GNU Library General Public | 7 modify it under the terms of the GNU Library General Public |
46 | 46 |
47 char * | 47 char * |
48 xstrndup (const char *s, size_t n) | 48 xstrndup (const char *s, size_t n) |
49 { | 49 { |
50 char *result; | 50 char *result; |
51 size_t len = strlen (s); | 51 size_t len = strnlen (s, n); |
52 | |
53 if (n < len) | |
54 len = n; | |
55 | 52 |
56 result = XNEWVEC (char, len + 1); | 53 result = XNEWVEC (char, len + 1); |
57 | 54 |
58 result[len] = '\0'; | 55 result[len] = '\0'; |
59 return (char *) memcpy (result, s, len); | 56 return (char *) memcpy (result, s, len); |