sourCEntral - mobile manpages

pdf

getcwd

NOM

getcwd, getwd, get_current_dir_name - Obtenir le répertoire de travail actuel

BIBLIOTHÈQUE

Bibliothèque C standard (libc, -lc)

SYNOPSIS

#include <unistd.h>

char *getcwd(char buf[.size], size_t size);
char *get_current_dir_name(void);

[[deprecated]] char *getwd(char buf[PATH_MAX]);

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

get_current_dir_name() :
_GNU_SOURCE

getwd() :
Since glibc 2.12:
(_XOPEN_SOURCE >= 500) && ! (_POSIX_C_SOURCE >= 200809L)
|| /* glibc >= 2.19: */ _DEFAULT_SOURCE
|| /* glibc <= 2.19: */ _BSD_SOURCE
Before glibc 2.12:
_BSD_SOURCE || _XOPEN_SOURCE >= 500

DESCRIPTION

Ces fonctions renvoient une chaîne terminée par une octet nul contenant un chemin absolu correspondant au répertoire de travail actuel du processus appelant. Le chemin est renvoyé comme résultat de la fonction et par le paramètre buf, s’il est présent.

La fonction getcwd() copie le chemin d’accès absolu du répertoire de travail courant dans la chaîne pointée par buf, qui est de longueur size.

Si la taille du chemin absolu du répertoire de travail en cours, caractère nul de fin compris, dépasse size octets, la fonction renvoie NULL et errno contient le code d’erreur ERANGE. Une application doit détecter cette erreur et allouer un tampon plus grand si besoin est.

As an extension to the POSIX.1-2001 standard, glibc’s getcwd() allocates the buffer dynamically using malloc(3) if buf is NULL. In this case, the allocated buffer has the length size unless size is zero, when buf is allocated as big as necessary. The caller should free(3) the returned buffer.

get_current_dir_name() allouera avec malloc(3) une chaîne suffisamment grande pour contenir le nom du chemin absolu du répertoire de travail courant. Si la variable d’environnement PWD est configurée, et correcte, cette valeur sera renvoyée. L’appelant doit libérer avec free(3) le tampon renvoyé.

getwd() n’allouera aucune mémoire (avec malloc(3)). Le paramètre buf doit être un pointeur sur une chaîne comportant au moins PATH_MAX octets. Si la longueur du chemin absolu du répertoire de travail actuel, caractère nul de fin compris, dépasse PATH_MAX octets, NULL est renvoyé et errno prend la valeur ENAMETOOLONG. Notez que sur certains système, PATH_MAX peut ne pas être une constante connue à la compilation ; de plus, sa valeur peut dépendre du système de fichiers, voire être illimitée, consultez pathconf(3). Pour des raisons de portabilité et de sécurité, l’utilisation de getwd() est déconseillée.

VALEUR RENVOYÉE

On success, these functions return a pointer to a string containing the pathname of the current working directory. In the case of getcwd() and getwd() this is the same value as buf.

En cas d’échec, ces fonctions renvoient NULL, et remplissent errno avec le code d’erreur. Le contenu de la chaîne pointée par buf est indéfini en cas d’erreur.

ERREURS

EACCES

Impossible de lire ou de parcourir un composant du chemin d’accès.

EFAULT

buf pointe sur une adresse illégale.

EINVAL

L’argument size vaut zéro et buf n’est pas un pointeur NULL.

EINVAL

getwd() : buf est NULL.

ENAMETOOLONG

getwd() : La taille de la chaîne, terminée par un octet nul, du chemin absolu dépasse PATH_MAX octets.

ENOENT

Le répertoire en cours a été supprimé.

ENOMEM

Plus assez de mémoire.

ERANGE

Le paramètre size est inférieur à la longueur du nom du chemin absolu du répertoire de travail, caractère nul de fin compris. Allouez un tampon plus grand et réessayez.

ATTRIBUTS

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

img

STANDARDS

getcwd() se conforme à POSIX.1-2001. Notez cependant que POSIX.1-2001 laisse le comportement de getcwd() non spécifié si buf est NULL.

getwd() est présent dans POSIX.1-2001, mais marquée « LEGACY ». POSIX.1-2008 supprime la spécification de getwd() et POSIX.1-2001 ne définit aucune erreur pour getwd(). Utilisez getcwd() à la place.

get_current_dir_name() est une extension GNU.

NOTES

Under Linux, these functions make use of the getcwd() system call (available since Linux 2.1.92). On older systems they would query /proc/self/cwd. If both system call and proc filesystem are missing, a generic implementation is called. Only in that case can these calls fail under Linux with EACCES.

Ces fonctions sont souvent utilisées pour sauver le répertoire de travail afin d’y revenir plus tard. Ouvrir le répertoire courant (« . ») et appeler fchdir(2) pour y revenir est habituellement une alternative plus rapide et plus fiable (surtout sur d’autres systèmes que Linux) si l’on dispose de suffisamment de descripteurs de fichier.

différences entre bibliothèque C et noyau
On Linux, the kernel provides a getcwd() system call, which the functions described in this page will use if possible. The system call takes the same arguments as the library function of the same name, but is limited to returning at most PATH_MAX bytes. (Before Linux 3.12, the limit on the size of the returned pathname was the system page size. On many architectures, PATH_MAX and the system page size are both 4096 bytes, but a few architectures have a larger page size.) If the length of the pathname of the current working directory exceeds this limit, then the system call fails with the error ENAMETOOLONG. In this case, the library functions fall back to a (slower) alternative implementation that returns the full pathname.

Following a change in Linux 2.6.36, the pathname returned by the getcwd() system call will be prefixed with the string "(unreachable)" if the current directory is not below the root directory of the current process (e.g., because the process set a new filesystem root using chroot(2) without changing its current directory into the new root). Such behavior can also be caused by an unprivileged user by changing the current directory into another mount namespace. When dealing with pathname from untrusted sources, callers of the functions described in this page should consider checking whether the returned pathname starts with ’/’ or ’(’ to avoid misinterpreting an unreachable path as a relative pathname.

BOGUES

Since the Linux 2.6.36 change that added "(unreachable)" in the circumstances described above, the glibc implementation of getcwd() has failed to conform to POSIX and returned a relative pathname when the API contract requires an absolute pathname. With glibc 2.27 onwards this is corrected; calling getcwd() from such a pathname will now result in failure with ENOENT.

VOIR AUSSI

pwd(1), chdir(2), fchdir(2), open(2), unlink(2), free(3), malloc(3)

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 DOT rafin AT laposte DOT net>, Thierry Vignaud <tvignaud AT mandriva DOT com>, François Micaux, Alain Portal <aportal AT univ-montp2 DOT fr>, Jean-Philippe Guérard <fevrier AT tigreraye DOT org>, Jean-Luc Coulon (f5ibh) <jean-luc DOT coulon AT wanadoo DOT fr>, Julien Cristau <jcristau AT debian DOT org>, Thomas Huriaux <thomas DOT huriaux AT gmail DOT com>, Nicolas François <nicolas DOT francois AT centraliens DOT net>, Florentin Duneau <fduneau AT gmail DOT com>, Simon Paillard <simon DOT paillard AT resel DOT enst-bretagne DOT fr>, Denis Barbier <barbier AT debian DOT org> et David Prévot <david AT tilapin DOT org>

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.

pdf