Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
auth_update crash
Dec 23, 2017, 10:15 pm
By Remcon
check_tumble
Dec 18, 2017, 7:21 pm
By Remcon
parse description bug
Dec 15, 2017, 10:08 pm
By Remcon
Couple bugs
Dec 12, 2017, 5:42 pm
By Remcon
Bug in disarm( )
Nov 12, 2017, 6:54 pm
By GatewaySysop
LoP 1.46
Author: Remcon
Submitted by: Remcon
LOP 1.45
Author: Remcon
Submitted by: Remcon
LOP Heroes Edition
Author: Vladaar
Submitted by: Vladaar
Heroes sound extras
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.3
Author: Vladaar
Submitted by: Vladaar
Users Online
CommonCrawl, Bing

Members: 0
Guests: 8
Stats
Files
Topics
Posts
Members
Newest Member
478
3,708
19,242
612
Jacki72H
Today's Birthdays
There are no member birthdays today.
Related Links
» SmaugMuds.org » General » Coding » String copy functions
Forum Rules | Mark all | Recent Posts

String copy functions
< Newer Topic :: Older Topic >

Pages:<< prev 1 next >>
Post is unread #1 Apr 20, 2004, 8:57 pm
Go to the top of the page
Go to the bottom of the page

Greven
Magician
GroupMembers
Posts204
JoinedMar 5, 2005

I've been reading up on the differences between strcpy/cat, strncpy/cat, and strlcpy/cat. I know that afkmud uses mudstrlcpy/cat, my questions, I guess, since I can't see the source to the strlcpy/cat, is: Whats the difference? Is it just offered in the code to support a larger base, in case the OS doesn't support the function, or is there an actual difference?

Also, do people know if there are any general problems with:

#define strcpy(src, dest) strlcpy((src), (dest), sizeof((src)))

I want to save the time, if I can, and I know its sloppy, but I don't have the time to change the hunders or thousands of calls by hand, heh. Only problem I can see is that src might be a pointer, and thus sizeof(src) will return 4, but strcpy shouldn't work with pointers, right? Just wanna check, kinda tired.
       
Post is unread #2 Apr 21, 2004, 4:31 am
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

mudstrlcpy/cat is the source to strlcpy/cat. Those are the functions as found in BSD code, so there's no difference. They were included and renamed since common OS's like Redhat don't seem to have that available and it's far safer than using strncpy/cat since it will always properly NULL the trailing byte of a string.

As for your define, I'm not sure that would be a good idea. there are some spots in the prog driver code that didn't take kindly to being used with strlcpy/cat or even with strncpy/cat and you'll note they're still using strcpy/cat because of it. Apparently the NULL byte causes problems with some of how it works and I never did figure out why. There are probably also some other spots in the code that are like this that I can't recall right now.
       
Post is unread #3 Apr 21, 2004, 2:09 pm   Last edited Nov 24, 2007, 2:58 pm by Samson
Go to the top of the page
Go to the bottom of the page

Fyrenth

GroupMembers
Posts1
JoinedApr 21, 2004

Greven said:

Only problem I can see is that src might be a pointer, and thus sizeof(src) will return 4, but strcpy shouldn't work with pointers, right? Just wanna check, kinda tired.

Yep, sizeof the ptr will return the size of a word. If you are sure src is a char* then you could substitute with strlen(src), but I'm not entirely sure thats good practice (as strlen() will fall prone to the lack of a terminator, �, char in the char* and cause more problems than intended)

Just for ref though, strcpy() had better be working with pointers, strcpy has always taken a char* I believe, now if thats a preallocated char[] or a ptr to the start of a sequential bytes in memory (i.e. the same thing really) makes no diff really. They're quite interchangeable.

As Samson said, #defining like that usually isn't a good idea, esp. against the more core functions of libc, having your own namespace for your own funcs (be it prefixing in your own code in C, or using namespaces in C++) is always preferable (*remebers when creating a log() func and then trying to link in the mathlib* )
       
Pages:<< prev 1 next >>