Man page - stpncpy(3)

Packages contains this manual

Available languages:

en fr ja ro

Manual

stpncpy

NOM
BIBLIOTHÈQUE
SYNOPSIS
DESCRIPTION
VALEUR RENVOYÉE
ATTRIBUTS
STANDARDS
HISTORIQUE
AVERTISSEMENTS
EXEMPLES
VOIR AUSSI
TRADUCTION

NOM

stpncpy, strncpy - fill a fixed-size buffer with non-null bytes from a string, padding with null bytes as needed

BIBLIOTHÈQUE

BibliothĂšque C standard ( libc , -lc )

SYNOPSIS

#include <string.h>

char *strncpy(char dst [restrict . dsize ], const char *restrict src ,
size_t
dsize );
char *stpncpy(char
dst [restrict . dsize ], const char *restrict src ,
size_t
dsize );

Exigences de macros de test de fonctionnalités pour la glibc (consulter feature_test_macros (7)) :

stpncpy () :
Depuis la glibc 2.10 :
_POSIX_C_SOURCE >= 200809L
Avant la glibc 2.10 :
_GNU_SOURCE

DESCRIPTION

These functions copy non-null bytes from the string pointed to by src into the array pointed to by dst . If the source has too few non-null bytes to fill the destination, the functions pad the destination with trailing null bytes. If the destination buffer, limited by its size, isn’t large enough to hold the copy, the resulting character sequence is truncated. For the difference between the two functions, see RETURN VALUE.

Une implĂ©mentation de ces fonctions pourrait ĂȘtre cela :

char *
strncpy(char *restrict dst, const char *restrict src, size_t dsize)
{
stpncpy(dst, src, dsize);
return dst;
}
char *
stpncpy(char *restrict dst, const char *restrict src, size_t dsize)
{
size_t dlen;
dlen = strnlen(src, dsize);
return memset(mempcpy(dst, src, dlen), 0, dsize - dlen);
}

VALEUR RENVOYÉE

strncpy ()

renvoie dst .

stpncpy ()

renvoie un pointeur sur un octet aprÚs le dernier caractÚre dans la séquence de caractÚres de destination.

ATTRIBUTS

Pour une explication des termes utilisés dans cette section, consulter attributes (7).

Image grohtml-3882433-1.png

STANDARDS

strncpy ()

C11, POSIX.1-2008.

stpncpy ()

POSIX.1-2008.

HISTORIQUE

strncpy ()

C89, POSIX.1-2001, SVr4, 4.3BSD.

stpncpy ()

glibc 1.07. POSIX.1-2008.

AVERTISSEMENTS

The name of these functions is confusing. These functions produce a null-padded character sequence, not a string (see string_copying (7)). For example:

strncpy(buf, "1", 5); // { '1', 0, 0, 0, 0 }
strncpy(buf, "1234", 5); // { '1', '2', '3', '4', 0 }
strncpy(buf, "12345", 5); // { '1', '2', '3', '4', '5' }
strncpy(buf, "123456", 5); // { '1', '2', '3', '4', '5' }

Il est impossible de distinguer une troncature comme rĂ©sultat de l’appel d’une sĂ©quence de caractĂšres qui s’ajuste au tampon de destination ; la troncature peut ĂȘtre dĂ©tectĂ©e en comparant la longueur de la chaĂźne d’entrĂ©e Ă  celle du tampon de destination.

Si vous comptez utiliser cette fonction dans une chaĂźne d’appels, il pourrait ĂȘtre utile de dĂ©velopper une fonction similaire qui accepte un pointeur sur la fin (un octet aprĂšs le dernier Ă©lĂ©ment) du tampon de destination plutĂŽt qu’en donnant sa taille.

EXEMPLES

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(void)
{
char *p;
char buf1[20];
char buf2[20];
size_t len;
if (sizeof(buf2) < strlen("Hello world!"))
errx("strncpy: truncating character sequence");
strncpy(buf2, "Hello world!", sizeof(buf2));
len = strnlen(buf2, sizeof(buf2));
printf("[len = %zu]: ", len);
fwrite(buf2, 1, len, stdout);
putchar('\n');
if (sizeof(buf1) < strlen("Hello world!"))
errx("stpncpy: truncating character sequence");
p = stpncpy(buf1, "Hello world!", sizeof(buf1));
len = p - buf1;
printf("[len = %zu]: ", len);
fwrite(buf1, 1, len, stdout);
putchar('\n');
exit(EXIT_SUCCESS);
}

VOIR AUSSI

wcpncpy (3), string_copying (7)

TRADUCTION

La traduction française de cette page de manuel a été créée par Christophe Blaess <https://www.blaess.fr/christophe/>, Stéphan Rafin <stephan.rafin@laposte.net>, Thierry Vignaud <tvignaud@mandriva.com>, François Micaux, Alain Portal <aportal@univ-montp2.fr>, Jean-Philippe Guérard <fevrier@tigreraye.org>, Jean-Luc Coulon (f5ibh) <jean-luc.coulon@wanadoo.fr>, Julien Cristau <jcristau@debian.org>, Thomas Huriaux <thomas.huriaux@gmail.com>, Nicolas François <nicolas.francois@centraliens.net>, Florentin Duneau <fduneau@gmail.com>, Simon Paillard <simon.paillard@resel.enst-bretagne.fr>, Denis Barbier <barbier@debian.org>, David Prévot <david@tilapin.org>, Frédéric Hantrais <fhantrais@gmail.com>, Grégoire Scano <gregoire.scano@malloc.fr> et Jean-Pierre Giraud <jean-pierregiraud@neuf.fr>

Cette traduction est une documentation libre ; veuillez vous reporter à la GNU General Public License version 3 concernant les conditions de copie et de distribution. Il n’y a aucune RESPONSABILITÉ LÉGALE.

Si vous découvrez un bogue dans la traduction de cette page de manuel, veuillez envoyer un message à debian-l10n-french@lists.debian.org .