Programming, for all ages and all languages.
houbOS1
Posts: 7 Joined: Tue Jan 22, 2008 1:34 pm
Post
by houbOS1 » Sat Mar 01, 2008 8:43 am
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
HoubOS - small operating system in active development
Jef
Member
Posts: 112 Joined: Tue Jan 08, 2008 7:25 am
Location: Greece
Contact:
Post
by Jef » Sat Mar 01, 2008 9:21 am
you have to copy bytes until it find null char (ASCIIZ)
Combuster
Member
Posts: 9301 Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:
Post
by Combuster » Sat Mar 01, 2008 11:47 am
It looks like you're mixing up s and s1
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[
My OS ] [
VDisk/SFS ]
Brynet-Inc
Member
Posts: 2426 Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:
Post
by Brynet-Inc » Sat Mar 01, 2008 2:57 pm
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.
Twitter: @canadianbryan . Award by smcerm, I stole it. Original was larger.
bluecode
Member
Posts: 202 Joined: Wed Nov 17, 2004 12:00 am
Location: Germany
Contact:
Post
by bluecode » Sun Mar 02, 2008 4:38 am
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).
houbOS1
Posts: 7 Joined: Tue Jan 22, 2008 1:34 pm
Post
by houbOS1 » Sun Mar 02, 2008 8:19 am
Thnx, now it works
HoubOS - small operating system in active development
eddyb
Post
by eddyb » Thu Mar 13, 2008 11:27 am
Jef wrote: you have to copy bytes until it find null char (ASCIIZ)
null asci char is '\0', i'm right?
JamesM
Member
Posts: 2935 Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:
Post
by JamesM » Thu Mar 13, 2008 11:46 am
Why dont you STFW and find out?
quok
Member
Posts: 490 Joined: Wed Oct 18, 2006 10:43 pm
Location: Kansas City, KS, USA
Post
by quok » Thu Mar 13, 2008 12:26 pm
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.
binutils
Member
Posts: 214 Joined: Thu Apr 05, 2007 6:07 am
Post
by binutils » Thu Mar 13, 2008 5:02 pm
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;
}
Solar
Member
Posts: 7615 Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:
Post
by Solar » Fri Mar 14, 2008 3:15 am
@ 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())...
Every good solution is obvious once you've found it.
binutils
Member
Posts: 214 Joined: Thu Apr 05, 2007 6:07 am
Post
by binutils » Fri Mar 14, 2008 8:38 am
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