Page 1 of 1

strcpy

Posted: Sat Mar 01, 2008 8:43 am
by houbOS1
Hi. I'm making the basic lib functions, but now I'm stuck on strcpy.
My code looks like

Code: Select all

void strcpy(char *s1, char *s2)
{
    while (*s++ = *s2++)
      ;
}
But when I try to copy a string to another string, it doesn't work, and when I look what's really in s1 and s2 I found that for example in s1[0] there is something with an ASCII value 20, but when I run my program again, some other thing is here, etc...

Where is the bug? :?: :!:
Thnx for replies

Posted: Sat Mar 01, 2008 9:21 am
by Jef
you have to copy bytes until it find null char (ASCIIZ)

Posted: Sat Mar 01, 2008 11:47 am
by Combuster
It looks like you're mixing up s and s1

Posted: Sat Mar 01, 2008 2:57 pm
by Brynet-Inc
Standard functions like that... should probably be standards compliant ;)

Code: Select all

char *strcpy(char *s1, const char *s2)
{
    char *s1_p = s1;
    while (*s1++ = *s2++)
      ;
    return s1_p;
}
Have fun.

Posted: Sun Mar 02, 2008 4:38 am
by bluecode
Brynet-Inc wrote:Standard functions like that... should probably be standards compliant ;)
And when you do it standard compliant, you should actually choose the newest standard. This is the C99 function declaration:

Code: Select all

char *strcpy(char * restrict s1, const char * restrict s2);
Be aware that some compilers don't support C99 (fully).

Posted: Sun Mar 02, 2008 8:19 am
by houbOS1
Thnx, now it works :)

Posted: Thu Mar 13, 2008 11:27 am
by eddyb
Jef wrote:you have to copy bytes until it find null char (ASCIIZ)
null asci char is '\0', i'm right?

Posted: Thu Mar 13, 2008 11:46 am
by JamesM
Why dont you STFW and find out?

Posted: Thu Mar 13, 2008 12:26 pm
by quok
You shouldn't use strcpy... use strncpy instead, or even better strlcpy (from OpenBSD) as that way you'll always end up with a null terminated string.

Posted: Thu Mar 13, 2008 5:02 pm
by binutils
plan9 strcpy.c wrote:

Code: Select all

#include <u.h>
#include <libc.h>
#define	N	10000

char*
strcpy(char *s1, char *s2)
{
	char *os1;

	os1 = s1;
	while(!memccpy(s1, s2, 0, N)) {
		s1 += N;
		s2 += N;
	}
	return os1;
}

Posted: Fri Mar 14, 2008 3:15 am
by Solar
@ quok:

1) He is writing the standard functions. Whether or not it is wise to use strcpy() is not an issue here.

2) There are perfectly good reasons in favor of strcpy(), because sometimes the preconditions are clear.

@ binutils:

Huh... That implementation not only doesn't adhere to the current definition (const, restrict), it is also depending on a non-standard helper function (memccpy())...

Posted: Fri Mar 14, 2008 8:38 am
by binutils
yes it is p2001 and not c99/ISO
http://www.opengroup.org/onlinepubs/009 ... mccpy.html
http://www.schweikhardt.net/identifiers.html

--
PS: and this one is my favorite part:
msdn2 wrote:Run-Time Library Reference
memccpy

This POSIX function is deprecated beginning in Visual C++ 2005. Use the ISO C++ conformant _memccpy instead.
PPS: http://www.youtube.com/watch?v=JQM0_EaJ7jk :) anti-j to j

PPPS: BTW, in plan 9, strcpy for x86 is not written in c, it is asm

Code: Select all


/*
 * http://plan9.bell-labs.com/sys/doc/asm.html
 * http://plan9.bell-labs.com/sources/plan9/sys/src/libc/386/strcpy.s
 */

	TEXT	strcpy(SB),$0

	MOVL	$0, AX
	MOVL	$-1, CX
	CLD
/*
 * find end of second string
 */

	MOVL	p2+4(FP), DI
	REPN;	SCASB

	MOVL	DI, BX
	SUBL	p2+4(FP), BX

/*
 * copy the memory
 */
	MOVL	p1+0(FP), DI
	MOVL	p2+4(FP), SI
/*
 * copy whole longs
 */
	MOVL	BX, CX
	SHRL	$2, CX
	REP;	MOVSL
/*
 * copy the rest, by bytes
 */
	ANDL	$3, BX
	MOVL	BX, CX
	REP;	MOVSB

	MOVL	p1+0(FP), AX
	RET