STRDUP(3P)

πŸ’– STRDUP(3P) β€” the POSIX Programmer's Manual one πŸ’–

okay bestie, quick vibe check before we start: this page lives in the POSIX Programmer's Manual. The Linux version of this interface may lowkey do stuff differently (so go peek at the corresponding Linux manual page for the deets on Linux behavior), or honestly it might not even be implemented on Linux at all. πŸ™ˆ


✨ PROLOG ✨

Real talk: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ β€” consult the corresponding Linux manual page for details of Linux behavior β€” or the interface may not be implemented on Linux. So yeah, check before you commit, queen. πŸ‘‘


🌸 NAME 🌸

strdup, strndup β€” duplicate a specific number of bytes from a string. literally copy-paste but for strings πŸ’…


πŸ¦‹ 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, which 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? Null pointer. Sad but true. 😭

Now strndup() β€” she shall be equivalent to the strdup() function, duplicating the provided s in a new block of memory allocated as if by using malloc(), with the exception being that strndup() copies at most size plus one bytes into the newly allocated memory, terminating the new string with a NUL character. Okay so:

  • If the length of s is larger than size, only size bytes shall be duplicated. cut it off, periodt βœ‚οΈ
  • 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. no cap, always. 🫢

πŸ”₯ RETURN VALUE πŸ”₯

The strdup() function shall return a pointer to a new string on success. Otherwise, it shall return a null pointer and set errno to indicate the error. 🫠

Upon successful completion, the strndup() function 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, same rules.


πŸ’€ ERRORS πŸ’€

These functions shall fail if:

  • ENOMEM β€” Storage space available is insufficient. bestie the memory said no πŸ’”

The following sections are informative.


πŸŽ€ EXAMPLES πŸŽ€

None. (yeah. nothing. 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, girl. don't leave it hanging. 🧚

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. this is the plot twist. do not assume. 🚨

πŸ’… RATIONALE πŸ’…

None. (nothing to explain, it just works like that ⭐)


⭐ FUTURE DIRECTIONS ⭐

None. the future is… unchanged. slay. ✨


🫢 SEE ALSO 🫢

  • free()
  • wcsdup()

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 πŸ’– this one came to you from IEEE/The Open Group, dated 2017 β€” so when you cite it, it's IEEE/The Open Group, 2017, STRDUP(3P). stan a standardized string duplicator. πŸŒΈπŸ’…βœ¨

πŸ“¦ where this page comes from