GNT sans publicité, site mobile, fonctionnalitées exclusives...

Prop. page man strncpy V2

Le
Marc Boyer
Voici donc, après vos nombreuses remarques, une nouvelle version de
proposition de page man pour strcpy et strncpy


STRCPY(3) Linux Programmer's Manual STRCPY(3)

NAME
strcpy, strncpy - copy a string

SYNOPSIS
#include <string.h>

C89
char *strcpy(char *dest, const char *src);

char *strncpy(char *dest, const char *src, size_t n);

C99
char *strcpy(char *restrict dest, const char *restrict src);

char *strncpy(char *restrict dest, const char *restrict src, size_t n);

DESCRIPTION
The strcpy() function copies the string pointed to by src (including
the terminating `\0' character) to the array pointed to by dest. The
strings may not overlap, and the destination string dest must be large
enough to receive the copy.

The strncpy() function is similar, except that
- not more than n bytes of src are copied,
- src could be not null-terminated.
Thus, if there is no null byte among the first n bytes of src, dest will
not be null-terminated.

In the case where the length of src is less than n, the remainder of
dest will be padded with nulls.

Assuming that src is null-terminated, a (very poor quality)
implementation of strncpy() could be

char*
optimist_strncpy(char *restrict dest, const char *restrict src, size_t n){
if (n != 0) {
size_t src_len= strlen(src); // BUG if src not null-terminated
size_t i;

for(i=0 ; i < src_len && i < n; i++)
dest[i]= src[i];
for( ; i < n ; i++)
dest[i]= '\0';
}

return dest;
}

Another (poor quality but conform) implementation of strncpy() could be:

char*
strncpy(char *restrict dest, const char *restrict src, size_t n){
size_t i;
for(i=0 ; i < n && src[i] != '\0' ; i++)
dest[i]= src[i];
for( ; i < n ; i++)
dest[i]= '\0';
}

return dest;
}



RETURN VALUE
The strcpy() and strncpy() functions return a pointer to the destina-
tion string dest.

BUGS
If the destination string of a strcpy() is not large enough (that is,
if the programmer was stupid/lazy, and failed to check the size before
copying) then anything might happen. Overflowing fixed length strings
is a favourite cracker technique.

A common mistake with strncpy() is to forget that it, if
strlen(src) >= n, then, the '\0' termination character is not set.

NOTE
Some programmers consider strncpy() as inefficient and error
prone. If the programmer knows the the size of dest is greater than
the length of src, then, strcpy() can be used. Otherwise, using
strncpy() makes a non null terminated string.

Programmers often prevent this mistake by forcing '\0' termination
this way:

strncpy(buf, str, n);
if (n) buf[n - 1]= '\0';

If n can not be nul, the test can be removed.


CONFORMING TO
SVID 3, POSIX, BSD 4.3, ISO 9899

SEE ALSO
bcopy(3), memccpy(3), memcpy(3), memmove(3), wcscpy(3), wcsncpy(3)

GNU 1993-04-11 STRCPY(3)

--
Je ne respecte plus le code de la route à vélo depuis une double fracture
due au fait que j'étais le seul à le respecter.
Lire les 26 réponses

Questions / Réponses high-tech
Vidéos High-Tech et Jeu Vidéo
Téléchargements
Vos réponses Page 1 / 6
Gagnez chaque mois un abonnement Premium avec GNT : Inscrivez-vous !
Trier par : date / pertinence
DINH Viêt Hoà
Le #746698

char*
optimist_strncpy(char *restrict dest, const char *restrict src, size_t n){
if (n != 0) {
size_t src_len= strlen(src); // BUG if src not null-terminated
size_t i;

for(i=0 ; i < src_len && i < n; i++)
dest[i]= src[i];
for( ; i < n ; i++)
dest[i]= '';
}

return dest;
}

Another (poor quality but conform) implementation of strncpy() could be:

char*
strncpy(char *restrict dest, const char *restrict src, size_t n){
size_t i;
for(i=0 ; i < n && src[i] != '' ; i++)
dest[i]= src[i];
for( ; i < n ; i++)
dest[i]= '';
}

return dest;
}


perso, les boucles for dans initialization, ça me fait pâlir.

Il n'y a pas trop de feedback user sur ce qu'il se passe
dans la boucle.

--
DINH V. Hoa,

"Les gens sont trop cons." -- Cent Quarante-Six

Marc Boyer
Le #735928
DINH Viêt Hoà wrote:

for( ; i < n ; i++)
dest[i]= '';
}

return dest;
}


perso, les boucles for dans initialization, ça me fait pâlir.


Je peux faire un
for(j= i ; j < n ; j++)

mais est-ce qu'on gagne vraiment ?

Il n'y a pas trop de feedback user sur ce qu'il se passe
dans la boucle.


Je comprends pas ta remarque.

Marc Boyer
--
Je ne respecte plus le code de la route à vélo depuis une double fracture
due au fait que j'étais le seul à le respecter.


DINH Viêt Hoà
Le #735927

DINH Viêt Hoà wrote:

for( ; i < n ; i++)
dest[i]= '';
}

return dest;
}


perso, les boucles for dans initialization, ça me fait pâlir.


Je peux faire un
for(j= i ; j < n ; j++)

mais est-ce qu'on gagne vraiment ?


pas trop, mais peut-être qu'avec un :

end_src = i;
for(i = end_src ; i < n ; i ++)
coin;

end_src ou end_copy au choix ?

Il n'y a pas trop de feedback user sur ce qu'il se passe
dans la boucle.


Je comprends pas ta remarque.


Je pense que les gens qui codent sont en moyenne habitués aux boucles.
for(i = debut ; i < fin ; i ++)
toto(i);

donc, autant laisser les bonnes vieilles habitudes.

--
DINH V. Hoa,

"Les gens sont trop cons." -- Cent Quarante-Six



cedric
Le #735920
DINH Viêt Hoà wrote:
Je pense que les gens qui codent sont en moyenne habitués aux boucles.
for(i = debut ; i < fin ; i ++)
toto(i);


un for sans initialiseur qui suis un autre for qui boucle sur la même
variable, c'est assez classique aussi quand même...

DINH Viêt Hoà
Le #735661

un for sans initialiseur qui suis un autre for qui boucle sur la même
variable, c'est assez classique aussi quand même...


J'avoue qu'il m'arrive peu souvent de continuer une boucle sur le même
indice.

--
DINH V. Hoa,

"Les gens sont trop cons." -- Cent Quarante-Six

Publicité
Suivre les réponses
Poster une réponse
Anonyme