basename(3) Library Functions Manual basename(3) NOMBRE basename, dirname - analiza los componentes de un nombre de ruta BIBLIOTECA Biblioteca Estandar C (libc, -lc) SINOPSIS #include char *dirname(char *ruta); char *basename(char *ruta); DESCRIPCION Atencion: existen 2 funciones basename() diferentes; vea mas adelante. The functions dirname() and basename() break a null-terminated pathname string into directory and filename components. In the usual case, dirname() returns the string up to, but not including, the final '/', and basename() returns the component following the final '/'. Trailing '/' characters are not counted as part of the pathname. Si path no contiene una barra, dirname() devuelve la cadena "." mientras que basename() devuelve una copia de path. Si path es la cadena "/", entonces tanto dirname() como basename devuelven la cadena "/". Si path es un puntero a null o apunta a una cadena vacia, entonces tanto dirname() como basename() devuelven la cadena ".". Concatenando la cadena devuelta por dirname(), un caracter "/", y la cadena devuelta por basename() se obtiene el nombre de ruta completo. Both dirname() and basename() may modify the contents of path, so it may be desirable to pass a copy when calling one of these functions. These functions may return pointers to statically allocated memory which may be overwritten by subsequent calls. Alternatively, they may return a pointer to some part of path, so that the string referred to by path should not be modified or freed until the pointer returned by the function is no longer required. La siguiente lista de ejemplos (extraidos de SUSv2) muestra las cadenas devueltas por dirname() y basename() para diferentes rutas: ruta dirname basename /usr/lib /usr lib /usr/ / usr usr . usr / / / . . . .. . .. VALOR DEVUELTO Tanto dirname() como basename() devuelven punteros a cadenas terminadas en null. No los envian a free(3). ATRIBUTOS Para obtener una explicacion de los terminos usados en esta seccion, vease attributes(7). +-----------------------------+--------------------+-------------------+ |Interfaz | Atributo | Valor | +-----------------------------+--------------------+-------------------+ |basename(), dirname() | Seguridad del hilo | Multi-hilo seguro | +-----------------------------+--------------------+-------------------+ VERSIONES There are two different versions of basename() - the POSIX version described above, and the GNU version, which one gets after #define _GNU_SOURCE /* Vease feature_test_macros(7) */ #include The GNU version never modifies its argument, and returns the empty string when path has a trailing slash, and in particular also when it is "/". There is no GNU version of dirname(). With glibc, one gets the POSIX version of basename() when is included, and the GNU version otherwise. ESTANDARES POSIX.1-2008. HISTORIAL POSIX.1-2001. ERRORES In the glibc implementation, the POSIX versions of these functions modify the path argument, and segfault when called with a static string such as "/usr/". En versiones de glibc anteriores a 2.2.1 (incluida), dirname() no gestiona correctamente los nombres de ruta con caracteres '/' al final, y provoca una violacion de segmento si se le pasa un argumento NULL. EJEMPLOS Este fragmento de codigo muestra el uso de basename() y dirname(): char *dirc, *basec, *bname, *dname; char *path = "/etc/passwd"; dirc = strdup(path); basec = strdup(path); dname = dirname(dirc); bname = basename(basec); printf("dirname=%s, basename=%s\n", dname, bname); VEASE TAMBIEN basename(1), dirname(1) TRADUCCION La traduccion al espanol de esta pagina del manual fue creada por Miguel Perez Ibars y Marcos Fouces Esta traduccion es documentacion libre; lea la GNU General Public License Version 3 o posterior con respecto a las condiciones de copyright. No existe NINGUNA RESPONSABILIDAD. Si encuentra algun error en la traduccion de esta pagina del manual, envie un correo electronico a . Paginas de manual de Linux 6.06 31 Octubre 2023 basename(3)