Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F22791413
strndup.c
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
914 B
Referenced Files
None
Subscribers
None
strndup.c
View Options
# include "defines.h"
#undef strndup
/**
* Allocate a new character array. Copy into it at most
* maxBytes bytes.
*/
char
*
rpl_strndup
(
const
char
*
dupMe
,
size_t
maxBytes
)
{
char
*
ptr
=
NULL
;
char
*
retMe
=
NULL
;
/* Configure maxBytes to be the number of bytes to copy */
maxBytes
=
MIN
(
strlen
(
dupMe
),
maxBytes
);
/* Allocate the return buffer. */
retMe
=
malloc
(
maxBytes
+
1
);
/* Was the allocation successful? */
if
(
NULL
==
retMe
)
{
return
NULL
;
}
/*
* ptr will point to the byte to which to copy the next
* source byte.
*/
ptr
=
retMe
;
/*
* Continue while dupMe is valid and we are < maxBytes number
* of bytes copied. This is typecase here because size_t is
* unsigned, so comparing against > 0 *should* produce a
* warning :)
*/
while
(
dupMe
&&
(
int
)
maxBytes
--
>
0
)
{
*
ptr
++
=
*
dupMe
++
;
}
/* Be sure to NULL terminate the array */
*
ptr
=
0
;
return
retMe
;
}
File Metadata
Details
Attached
Mime Type
text/x-c
Expires
Mon, Feb 2, 15:27 (6 h, 24 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3386053
Default Alt Text
strndup.c (914 B)
Attached To
Mode
rDARKBOT Darkbot
Attached
Detach File
Event Timeline
Log In to Comment