comparison libiberty/strndup.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 strndup function. 1 /* Implement the strndup 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
31 */ 31 */
32 32
33 #include "ansidecl.h" 33 #include "ansidecl.h"
34 #include <stddef.h> 34 #include <stddef.h>
35 35
36 extern size_t strlen (const char*); 36 extern size_t strnlen (const char *s, size_t maxlen);
37 extern PTR malloc (size_t); 37 extern PTR malloc (size_t);
38 extern PTR memcpy (PTR, const PTR, size_t); 38 extern PTR memcpy (PTR, const PTR, size_t);
39 39
40 char * 40 char *
41 strndup (const char *s, size_t n) 41 strndup (const char *s, size_t n)
42 { 42 {
43 char *result; 43 char *result;
44 size_t len = strlen (s); 44 size_t len = strnlen (s, n);
45
46 if (n < len)
47 len = n;
48 45
49 result = (char *) malloc (len + 1); 46 result = (char *) malloc (len + 1);
50 if (!result) 47 if (!result)
51 return 0; 48 return 0;
52 49