STRCPY(3POSIX)

πŸ’– STRCPY β€” ok bestie, let's copy a string πŸ’–

So you opened the POSIX Programmer's Manual page for stpcpy and strcpy and honestly? iconic choice. Sit down, grab a boba, let's get into it. πŸ§‹βœ¨


🌸 PROLOG 🌸

Real talk first: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, go check the corresponding Linux manual page if you need the Linux tea β˜•), or the interface may not even be implemented on Linux. Not every baddie ships everywhere, you know? πŸ’…


✨ NAME ✨

stpcpy, strcpy β€” copy a string and return a pointer to the end of the result πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <string.h>

char *stpcpy(char *restrict s1, const char *restrict s2);
char *strcpy(char *restrict s1, const char *restrict s2);

Look at those restrict qualifiers. So demure. So mindful. 🫢


πŸ’œ DESCRIPTION πŸ’œ

Okay so for strcpy() specifically: the functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional β€” no drama intended, fr fr. This volume of POSIX.1‐2017 defers to the ISO C standard. She knows her place and it's ✨respectful✨.

The stpcpy() and strcpy() functions shall copy the string pointed to by s2 β€” including the terminating NUL character, don't forget the NUL, she's part of the package β€” into the array pointed to by s1.

⚠️ And now the scary part: if copying takes place between objects that overlap, the behavior is undefined. Not "kinda weird", not "a little off" β€” undefined. Absolutely no cap. Do not do this. It's giving chaos. πŸ’€

🌈 RETURN VALUE 🌈

  • The stpcpy() function shall return a pointer to the terminating NUL character copied into the s1 buffer. She hands you the end of the party, not the start. πŸŽ‰
  • The strcpy() function shall return s1. Simple. Classic. The original main character. πŸ‘‘

And listen β€” no return values are reserved to indicate an error. So if you were waiting for an error code, bestie, there isn't one. That's just not the vibe here. 😭


πŸ“ ERRORS πŸ“

No errors are defined.

That's it. That's the section. Nothing to report, everything's fine, we move. πŸ’–

The following sections are informative.


πŸŽ€ EXAMPLES πŸŽ€

🧚 Construction of a Multi-Part Message in a Single Buffer

#include <string.h>
#include <stdio.h>

int
main (void)
{
    char buffer [10];
    char *name = buffer;

    name = stpcpy (stpcpy (stpcpy (name, "ice"),"-"), "cream");
    puts (buffer);
    return 0;
}

Nested stpcpy calls like little matryoshka dolls β€” each one returns the end of what it just wrote, so you can chain them and build "ice-cream" in one go. The way that *works*… obsessed. 🍨

🌸 Initializing a String

The following example copies the string "----------" into the permstring variable.

#include <string.h>
...
static char permstring[11];
...
strcpy(permstring, "----------");
...

Eleven characters of buffer, ten dashes and one NUL. Math is math. ✨

πŸ’… Storing a Key and Data

The following example allocates space for a key using malloc() then uses strcpy() to place the key there. Then it allocates space for data using malloc(), and uses strcpy() to place data there. (The user-defined function dbfree() frees memory previously allocated to an array of type struct element *.)

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
...
/* Structure used to read data and store it. */
struct element {
    char *key;
    char *data;
};

struct element *tbl, *curtbl;
char *key, *data;
int count;
...
void dbfree(struct element *, int);
...
if ((curtbl->key = malloc(strlen(key) + 1)) == NULL) {
    perror("malloc"); dbfree(tbl, count); return NULL;
}
strcpy(curtbl->key, key);

if ((curtbl->data = malloc(strlen(data) + 1)) == NULL) {
    perror("malloc"); free(curtbl->key); dbfree(tbl, count); return NULL;
}
strcpy(curtbl->data, data);
...

See how it's strlen(key) + 1? That + 1 is for the NUL terminator. Never forget the + 1. Never. It's the little detail that saves your whole program from disaster. πŸ™ˆ


πŸ’€ APPLICATION USAGE πŸ’€

Character movement is performed differently in different implementations. So overlapping moves may yield surprises. And by "surprises" we mean "things you did not ask for". 🎁

This version is aligned with the ISO C standard; this does not affect compatibility with XPG3 applications. Reliable error detection by this function was never guaranteed. She's not a detective, she's a copier. Manage your expectations accordingly. πŸ’…


πŸ”₯ RATIONALE πŸ”₯

None.

She said what she said and she's not elaborating. Periodt. πŸ‘‘


⭐ FUTURE DIRECTIONS ⭐

None.

The future? Unchanged. Stable. Unbothered. Moisturized. In her lane. Focused. Flourishing. 🌱


🫢 SEE ALSO 🫢

  • strncpy()
  • wcscpy()

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 .


Okay bestie that's the whole page, we ate and left no crumbs 🍽️✨. This was IEEE/The Open Group, 2017, and the page is STRCPY(3POSIX). Stay safe, never overlap your buffers, and always add that + 1. Love u, bye!! πŸ’–πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«Ά