stpncpy(3) Library Functions Manual stpncpy(3)

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

Bibliothèque C standard (libc, -lc)

#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

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);
}

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

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

Interface Attribut Valeur
stpncpy(), strncpy() Sécurité des threads MT-Safe

C11, POSIX.1-2008.
POSIX.1-2008.

C89, POSIX.1-2001, SVr4, 4.3BSD.
glibc 1.07. POSIX.1-2008.

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.

#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);
}

wcpncpy(3), string_copying(7)

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.

12 février 2024 Pages du manuel de Linux 6.06