stpncpy(3) Library Functions Manual stpncpy(3) NOM stpncpy, strncpy - fill a fixed-size buffer with non-null bytes from a string, padding with null bytes as needed BIBLIOTHEQUE Bibliotheque C standard (libc, -lc) SYNOPSIS #include 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 fonctionnalites 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 implementation de ces fonctions pourrait etre 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 RENVOYEE strncpy() renvoie dst. stpncpy() renvoie un pointeur sur un octet apres le dernier caractere dans la sequence de caracteres de destination. ATTRIBUTS Pour une explication des termes utilises dans cette section, consulter attributes(7). +---------------------------------+--------------------------+---------+ |Interface | Attribut | Valeur | +---------------------------------+--------------------------+---------+ |stpncpy(), strncpy() | Securite des threads | MT-Safe | +---------------------------------+--------------------------+---------+ 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 resultat de l'appel d'une sequence de caracteres qui s'ajuste au tampon de destination ; la troncature peut etre detectee en comparant la longueur de la chaine d'entree a celle du tampon de destination. Si vous comptez utiliser cette fonction dans une chaine d'appels, il pourrait etre utile de developper une fonction similaire qui accepte un pointeur sur la fin (un octet apres le dernier element) du tampon de destination plutot qu'en donnant sa taille. EXEMPLES #include #include #include #include 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 francaise de cette page de manuel a ete creee par Christophe Blaess , Stephan Rafin , Thierry Vignaud , Francois Micaux, Alain Portal , Jean-Philippe Guerard , Jean-Luc Coulon (f5ibh) , Julien Cristau , Thomas Huriaux , Nicolas Francois , Florentin Duneau , Simon Paillard , Denis Barbier , David Prevot , Frederic Hantrais , Gregoire Scano et Jean-Pierre Giraud Cette traduction est une documentation libre ; veuillez vous reporter a la GNU General Public License version 3 concernant les conditions de copie et de distribution. Il n'y a aucune RESPONSABILITE LEGALE. Si vous decouvrez un bogue dans la traduction de cette page de manuel, veuillez envoyer un message a . Pages du manuel de Linux 6.06 12 fevrier 2024 stpncpy(3)