Write strlcpy function

stell-91 Posted messages 519 Registration date   Status Membre Last intervention   -  
 n -
Hello,

I need to write strlcpy in C, I'm having a hard time understanding the man and I've read it several times.

Let me first explain what I've understood, please tell me if you disagree with me.

man strcpy
It takes 2 parameters we should copy src into dest then add a '\0' and return dest
If dest is shorter than src then we return the x characters of src copied into dest

man strncpy
It takes 3 parameters we should copy the first n characters of src into dest then add '\0's if necessary when dest is longer than src.

man strlcpy (this is getting complicated)
It takes 3 parameters (char *dest, char *src, unsigned int size)
It returns the length that is copyable from src into dest.
In parallel, we need to copy size - 1 characters from src into dest and put a '\0' at the end. The returned length will be equal to the number of characters including the '\0'
What should we return if dest is shorter than src, what should we copy into dest?

Thank you for your help

https://www.freebsd.org/cgi/man.cgi?query=strlcpy&sektion=3

http://manpagesfr.free.fr/man/man3/strcpy.3.html

Configuration: Windows / Chrome 62.0.3202.89

2 réponses

flagch Posted messages 1 Status Membre 2
 
Hello friends,

I haven't delved into the issue itself, especially since it's been at least fifteen years since I've done any C...

I just want to make a remark because your exchanges are quite symptomatic of a recurring problem that is becoming increasingly acute: the absolute necessity of mastering language - I'm talking about the language (French in this case) and not a programming language - to solve more or less complex problems. Without language, thought is impossible, and vice versa. So it becomes complicated to communicate as soon as one is not capable of expressing one's thoughts clearly.

Just as important as being able to put words to what one thinks, is the absolute necessity of understanding a text read (or listened to). The best technical documentation is worthless if one is not capable of grasping the nuances of language.

If we want to convey ideas, it is essential to have some basic linguistic knowledge such as:
- a minimum vocabulary rarely hurts
- some vague notions of grammar can occasionally help
- PUNCTUATION: often neglected but oh so important! Misused (or not used), punctuation makes a text as unreadable as a mathematical function where the operators have been randomly mixed.

As a developer myself, but also and above all a lover of the language (particularly my own: French), I have encountered countless colleagues who were unable to articulate a simple problem logically or mathematically in French. Result: we inevitably find ourselves in a deadlock. One can be a genius, but without being able to clearly explain our discovery, we cannot transmit it. You don't need to be a computer scientist to see the damage caused by our poor command of the language: who hasn't experienced a misinterpreted text message due to a comma strike? The comma is, in my opinion, one of the leading causes of divorce these days!

And I'm not even talking about the ellipsis... which has... become, with the advent of (the arrival, for purists!) SMS language... a real calamity of... our... time. What's simpler, indeed, than pressing that PERIOD key three times to completely change the meaning of what precedes it?

I really don't remember why I started writing this post, especially as it doesn't add much to the original question :))))
It was just to draw the reader's attention to the importance of learning French, and not just spelling but especially grammar, and particularly syntax. It's essential. Paramount!
Maybe also to gently take revenge on my colleagues who almost saw me as a "dummy" when I told them that what they were telling or writing me simply wasn’t logical, not French.

Well; I stop here hoping I haven't made too many promising mistakes while writing this rant, if not I’d be in the mire, wouldn’t I? :)
3
n
 
I accidentally voted "thank you."
1
Dalfab Posted messages 638 Registration date   Status Membre Last intervention   101
 
Hello,
I find the man page clearer than your explanations!

strlcpy(dest,src,lgMax)
is supposed to copy as many characters as possible from
src
to
dest
, with a maximum size of
lgMax
in
dest
.
Unlike
strncpy(dest,src,lgMax)
, it must ensure that the terminator is always written in
dest[]
(if
src
is too long, it will be at position
dest[lgMax-1]
, otherwise
dest
will be a simple copy of
src
).
The function must return the length of
src
.
1
stell-91 Posted messages 519 Registration date   Status Membre Last intervention   5
 
Thank you for your response, but I am afraid I do not understand what you are explaining to me.

Should strlcpy return the length of src? Is this valid in all cases?

If dest is longer than src, do I return the length of src?
If dest is shorter than src, do I return the length of src?
0
Dalfab Posted messages 638 Registration date   Status Membre Last intervention   101 > stell-91 Posted messages 519 Registration date   Status Membre Last intervention  
 
yes
yes
yes
This allows the function's usage to know that the entire string has been copied
char dest[5]; int nb = strlcpy(dest, "hello", sizeof(dest)); nb -= sizeof(dest) - 1; if ( nb > 0 ) printf("warning %d characters missing\n" , nb); printf("string = %s\n" , dest); // => "hell"
0
stell-91 Posted messages 519 Registration date   Status Membre Last intervention   5
 
I want to make sure I have understood correctly

example
dest = "airplane"
src = "boat"
unsigned int size = 3

we will copy src into dest so the new dest = "bo\0\0\0\0"
Do you agree with the idea of adding enough '\0'?

returned value 2 + 1, the 1 corresponds to '\0' do you confirm?

I said 2 + 1 because of this phrase in the man that made me doubt: "functions return the total length of the string they tried to create."

another case
dest = "airplane"
src = "boat"
unsigned int size = 8

new dest = "boat\0"
returned value 6 + 1 that's right?

https://www.freebsd.org/cgi/man.cgi?query=strlcpy&sektion=3

Do you agree with me?
If it's good, I think I understand the subtleties of this function.
0
Dalfab Posted messages 638 Registration date   Status Membre Last intervention   101 > stell-91 Posted messages 519 Registration date   Status Membre Last intervention  
 
Relis le man

example
dest = "airplane"
src = "boat"
unsigned int size = 3

we are going to copy src into dest so the new dest = "bo\0an\0" (max 3 characters including the terminator, the others are certainly not modified)
Do you agree with the idea of adding enough '\0'?
No, it's not in the man.
return value 3 + 1, the 1 corresponds to '\0' do you validate?
No, we return 6 the length of "boat" and the '\0' is not counted
I said 3 + 1 because of this phrase in the man that confused me:
"functions return the total length of the string they tried to create."
Indeed ambiguous text, but the man specifies "For strlcpy() that means the length of src."

another case

dest = "airplane"
src = "boat"
unsigned int size = 8

new dest = "boat"\0 Yes
return value 6 + 1 is that correct? No 6
0
stell-91 Posted messages 519 Registration date   Status Membre Last intervention   5 > Dalfab Posted messages 638 Registration date   Status Membre Last intervention  
 
"we're going to copy src to dest so the new dest = "ba\0on\0" (max 3 characters including terminator, the others should not be modified)
Do you agree with the idea of adding enough '\0'?
No, it's not in the man."


Yes, excuse me, I took a twisted shortcut with the strncpy function. We need to add just one '\0'

For the returned value, it corresponds to the value of src. I remind you that src is never modified so its length remains unchanged, only dest is modified.

If you validate my explanations, I think I understand.
0