[OT, probably] stpcpy

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

If you can forgive the mention of a nonStandard library function, can
someone shed some light on where this function came from, and how
widely it's implemented? I had to convince my boss just now that it
was not ANSI, and I'm curious as to the history of this function (as
one might find the history of strdup() interesting).

To bring it on-topic, kind of, I believe this is the gist of how
stpcpy() is implemented:

char *stpcpy( char *dest, const char *source )
{
strcpy( dest, source ); /* all of strcpy()'s conditions must
be met, obviously */
return( dest+strlen(dest) ); /* return pointer to end of dest */
}
 
R

Richard Bos

Grumble said:
The FreeBSD manpage states:

The stpcpy() function is an MS-DOS and GNUism.
The stpcpy() function conforms to no standard.

The Linux manpage states:

This function is not part of the ANSI or POSIX standards,
and is not customary on Unix systems, but is not a GNU
invention either. Perhaps it comes from MS-DOS.

It's not in a 1987 MS C helpfile I happen to have lying around, but it
is in Turbo C 2.01. This means that it's probably a Borland invention.

In any case, though it isn't ISO, it isn't in the implementation's
namespace and it's easy to implement yourself if your implementation
doesn't have it.

Richard
 
G

Grumble

Christopher said:
If you can forgive the mention of a nonStandard library function,
can someone shed some light on where this function came from, and
how widely it's implemented? I had to convince my boss just now
that it was not ANSI, and I'm curious as to the history of this
function.

The FreeBSD manpage states:

The stpcpy() function is an MS-DOS and GNUism.
The stpcpy() function conforms to no standard.

The Linux manpage states:

This function is not part of the ANSI or POSIX standards,
and is not customary on Unix systems, but is not a GNU
invention either. Perhaps it comes from MS-DOS.
 
M

Mark McIntyre

If you can forgive the mention of a nonStandard library function, can
someone shed some light on where this function came from, and how
widely it's implemented?

At least one set of manpages say this:
This function is not part of the ANSI or POSIX standards,
and is not customary on Unix systems, but is not a GNU
invention either. Perhaps it comes from MS-DOS.

The same general comment appears in many places on the web. Looks like a
function to steer clear of IMHO.
 
L

Lew Pitcher

If you can forgive the mention of a nonStandard library function, can
someone shed some light on where this function came from, and how
widely it's implemented? I had to convince my boss just now that it
was not ANSI, and I'm curious as to the history of this function (as
one might find the history of strdup() interesting).

To bring it on-topic, kind of, I believe this is the gist of how
stpcpy() is implemented:

char *stpcpy( char *dest, const char *source )
{
strcpy( dest, source ); /* all of strcpy()'s conditions must
be met, obviously */
return( dest+strlen(dest) ); /* return pointer to end of dest */
}

A quick google turns up a number of pages describing stpcpy, including
http://www.opensource.apple.com/darwinsource/10.3/grep-7/grep/src/stpcpy.c
which credits the GNU C library.

Many of the online 'man' pages pointed to by google include the disclaimer

CONFORMING TO
This function is not part of the ANSI or POSIX standards,
and is not customary on Unix systems, but is not a GNU
invention either. Perhaps it comes from MS-DOS.

I know this doesn't help much, but it /does/ eliminate some sources.

--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
 
L

Lew Pitcher

If you can forgive the mention of a nonStandard library function, can
someone shed some light on where this function came from, and how
widely it's implemented? I had to convince my boss just now that it
was not ANSI, and I'm curious as to the history of this function (as
one might find the history of strdup() interesting).
[snip]
Many of the online 'man' pages pointed to by google include the disclaimer

CONFORMING TO
This function is not part of the ANSI or POSIX standards,
and is not customary on Unix systems, but is not a GNU
invention either. Perhaps it comes from MS-DOS.

I know this doesn't help much, but it /does/ eliminate some sources.

I found
http://developer.apple.com/documentation/Darwin/Reference/ManPages/html/stpcpy.3.html
which includes

Standards
The strcpy() and strncpy() functions conform to ISO/IEC 9899:1990 (``ISO C89'').
The stpcpy() function is an MS-DOS and GNUism. The stpcpy() function conforms to
no standard.

History

The stpcpy() function first appeared in FreeBSD 4.4, coming from 1998-vintage
Linux.

--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
 
C

Christopher Benson-Manica

Richard Bos said:
It's not in a 1987 MS C helpfile I happen to have lying around, but it
is in Turbo C 2.01. This means that it's probably a Borland invention.

That was definitely my feeling... (guess what compiler I'm stuck with?)
 
L

Leor Zolman

If you can forgive the mention of a nonStandard library function, can
someone shed some light on where this function came from, and how
widely it's implemented? I had to convince my boss just now that it
was not ANSI, and I'm curious as to the history of this function (as
one might find the history of strdup() interesting).

To bring it on-topic, kind of, I believe this is the gist of how
stpcpy() is implemented:

char *stpcpy( char *dest, const char *source )
{
strcpy( dest, source ); /* all of strcpy()'s conditions must
be met, obviously */
return( dest+strlen(dest) ); /* return pointer to end of dest */
}

Of course, this one's just crying out to be totally hand-coded:

char *stpcpy( char *dest, const char *source )
{
while (*dest++ = *source++)
;
return dest;
}


-leor


Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
C

Christopher Benson-Manica

Leor Zolman said:
Of course, this one's just crying out to be totally hand-coded:

Needless to say. I used strcpy() only to make it as clear as possible
what stpcpy() is intended to do.
 
L

Leor Zolman

Needless to say. I used strcpy() only to make it as clear as possible
what stpcpy() is intended to do.

Sorry -- my brain was in alt.lang.learn.c-c++ mode for that one. The
switch must've been sticky ;-)
-leor


Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
S

Slartibartfast

Christopher Benson-Manica said:
If you can forgive the mention of a nonStandard library function, can
someone shed some light on where this function came from, and how
widely it's implemented? I had to convince my boss just now that it
was not ANSI, and I'm curious as to the history of this function (as
one might find the history of strdup() interesting).

To bring it on-topic, kind of, I believe this is the gist of how
stpcpy() is implemented:

char *stpcpy( char *dest, const char *source )
{
strcpy( dest, source ); /* all of strcpy()'s conditions must
be met, obviously */
return( dest+strlen(dest) ); /* return pointer to end of dest */
}

I remember this function existing in Lattice C for MS-DOS in the early 1980s.


~ Let us linux ~
 
C

Chris Torek

char *stpcpy( char *dest, const char *source )
{
strcpy( dest, source ); /* all of strcpy()'s conditions must
be met, obviously */
return( dest+strlen(dest) ); /* return pointer to end of dest */
}
[/QUOTE]

Of course, this one's just crying out to be totally hand-coded:

char *stpcpy( char *dest, const char *source )
{
while (*dest++ = *source++)
;
return dest;
}

Since no one else has pointed it out yet, I will: these two versions
of stpcpy() return different values. Christopher Benson-Manica's
version returns a pointer to the '\0' byte that has been stored
into dest for some index i, while Leor Zolman's version returns
a pointer just *past* the same '\0' byte, i.e., &dest[i+1].

The obvious fix (assuming the first version is the desired one) for
the second is to use, e.g.:

while ((*dest = *source++) != '\0')
dest++;
return dest;

I believe the nonstandard stpcpy() *is* intended to return a pointer
to the '\0', so that one can write, e.g.:

stpcpy(stpcpy(stpcpy(buf, s1), " then "), s2);

to get the same effect as:

sprintf(buf, "%s then %s", s1, s2);

in Standard C. (Note the assumption that all of buf[0] through buf[k]
exist, where k is strlen(s1) + strlen(s2) + 7. snprintf() is safer,
but only available in C99.)
 
C

Christopher Benson-Manica

Chris Torek said:
Since no one else has pointed it out yet, I will: these two versions
of stpcpy() return different values.

Quite so, and another brilliant catch. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top