STRDUP(3POSIX)

πŸ’– STRDUP(3POSIX) β€” the POSIX Programmer's Manual, bestie edition πŸ¦‹

okay so quick vibe check before we even start: this page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so pls consult the corresponding Linux manual page for the actual Linux behavior), or honestly the interface might not even be implemented on Linux at all. πŸ™ˆ just so we're clear.


✨ NAME ✨

strdup, strndup β€” duplicate a specific number of bytes from a string πŸ’…


🌸 SYNOPSIS 🌸

#include <string.h>

char *strdup(const char *s);
char *strndup(const char *s, size_t size);

πŸ’œ DESCRIPTION πŸ’œ

so strdup() shall return a pointer to a new string, and that new string is a duplicate of the string pointed to by s. the returned pointer can be passed to free(). and if the new string cannot be created? a null pointer is returned. no drama, just null.

strndup() shall be equivalent to the strdup() function β€” like, it duplicates the provided s in a new block of memory allocated as if by using malloc() β€” with one exception: strndup() copies at most size plus one bytes into the newly allocated memory, terminating the new string with a NUL character. πŸŽ€

the deets:

  • if the length of s is larger than size, only size bytes shall be duplicated.
  • if size is larger than the length of s, all bytes in s shall be copied into the new memory buffer, including the terminating NUL character.
  • the newly created string shall always be properly terminated. periodt. ✨

πŸ”₯ RETURN VALUE πŸ”₯

on success, strdup() shall return a pointer to a new string. otherwise, it shall return a null pointer and set errno to indicate the error. 😭

upon successful completion, strndup() shall return a pointer to the newly allocated memory containing the duplicated string. otherwise, it shall return a null pointer and set errno to indicate the error. same energy, fr fr.


πŸ’€ ERRORS πŸ’€

these functions shall fail if:

  • ENOMEM β€” storage space available is insufficient. that's it, that's the whole list, the memory said no. 🫠
the following sections are informative. (aka: cute but not binding, iykyk)

πŸ“ EXAMPLES πŸ“

none. literally none. iconic of them honestly. πŸ’…


🧚 APPLICATION USAGE 🧚

for functions that allocate memory as if by malloc(), the application should release such memory when it is no longer required, by a call to free(). for strdup() and strndup(), this is the return value. so like, free your stuff bestie, don't leak. 🌈

also important: implementations are free to malloc() a buffer containing either (size + 1) bytes or (strnlen( s, size) + 1) bytes. applications should not assume that strndup() will allocate (size + 1) bytes when strlen( s) is smaller than size. no cap, don't assume it.


⭐ RATIONALE ⭐

none. 🀷


πŸŒ™ FUTURE DIRECTIONS πŸŒ™

none. 🀷 (again)


πŸ‘‘ SEE ALSO πŸ‘‘

  • free()
  • wcsdup()

and from the Base Definitions volume of POSIX.1‐2017: <string.h> πŸ“š


portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. 🫢

in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


and that's the whole page, bestie!! brought to you by the fine people at IEEE/The Open Group, dated 2017, page tag STRDUP(3POSIX). mwah. πŸ’‹βœ¨