linux之basename

  1 NAME         top
  2
  3        basename, dirname - parse pathname components
  4 SYNOPSIS         top
  5
  6        #include <libgen.h>
  7
  8        char *dirname(char *path);
  9
 10        char *basename(char *path);
 11 DESCRIPTION         top
 12
 13        Warning: there are two different functions basename() - see below.
 14
 15        The functions dirname() and basename() break a null-terminated
 16        pathname string into directory and filename components.  In the usual
 17        case, dirname() returns the string up to, but not including, the
 18        final ‘/‘, and basename() returns the component following the final
 19        ‘/‘.  Trailing ‘/‘ characters are not counted as part of the
 20        pathname.
 21
 22        If path does not contain a slash, dirname() returns the string "."
 23        while basename() returns a copy of path.  If path is the string "/",
 24        then both dirname() and basename() return the string "/".  If path is
 25        a null pointer or points to an empty string, then both dirname() and
 26        basename() return the string ".".
 27
 28        Concatenating the string returned by dirname(), a "/", and the string
 29        returned by basename() yields a complete pathname.
 30
 31        Both dirname() and basename() may modify the contents of path, so it
 32        may be desirable to pass a copy when calling one of these functions.
 33
 34        These functions may return pointers to statically allocated memory
 35        which may be overwritten by subsequent calls.  Alternatively, they
 36        may return a pointer to some part of path, so that the string
 37        referred to by path should not be modified or freed until the pointer
 38        returned by the function is no longer required.
 39
 40        The following list of examples (taken from SUSv2) shows the strings
 41        returned by dirname() and basename() for different paths:
 42
 43        path       dirname   basename
 44        /usr/lib   /usr      lib
 45        /usr/      /         usr
 46        usr        .         usr
 47        /          /         /
 48        .          .         .
 49        ..         .         ..
 50 RETURN VALUE         top
 51
 52        Both dirname() and basename() return pointers to null-terminated
 53        strings.  (Do not pass these pointers to free(3).)
 54 ATTRIBUTES         top
 55
 56        For an explanation of the terms used in this section, see
 57        attributes(7).
 58
 59        ┌──────────────────────┬───────────────┬─────────┐
 60        │Interface             │ Attribute     │ Value   │
 61        ├──────────────────────┼───────────────┼─────────┤
 62        │basename(), dirname() │ Thread safety │ MT-Safe │
 63        └──────────────────────┴───────────────┴─────────┘
 64 CONFORMING TO         top
 65
 66        POSIX.1-2001.
 67 NOTES         top
 68
 69        There are two different versions of basename() - the POSIX version
 70        described above, and the GNU version, which one gets after
 71
 72            #define _GNU_SOURCE         /* See feature_test_macros(7) */
 73            #include <string.h>
 74
 75        The GNU version never modifies its argument, and returns the empty
 76        string when path has a trailing slash, and in particular also when it
 77        is "/".  There is no GNU version of dirname().
 78
 79        With glibc, one gets the POSIX version of basename() when <libgen.h>
 80        is included, and the GNU version otherwise.
 81 BUGS         top
 82
 83        In the glibc implementation of the POSIX versions of these functions
 84        they modify their argument, and segfault when called with a static
 85        string like "/usr/".  Before glibc 2.2.1, the glibc version of
 86        dirname() did not correctly handle pathnames with trailing ‘/‘
 87        characters, and generated a segfault if given a NULL argument.
 88 EXAMPLE         top
 89
 90            char *dirc, *basec, *bname, *dname;
 91            char *path = "/etc/passwd";
 92
 93            dirc = strdup(path);
 94            basec = strdup(path);
 95            dname = dirname(dirc);
 96            bname = basename(basec);
 97            printf("dirname=%s, basename=%s\n", dname, bname);
 98 SEE ALSO         top
 99
100        basename(1), dirname(1)
101 COLOPHON         top
102
103        This page is part of release 3.82 of the Linux man-pages project.  A
104        description of the project, information about reporting bugs, and the
105        latest version of this page, can be found at
106        http://www.kernel.org/doc/man-pages/.
107
108 GNU                              2015-03-02                      BASENAME(3)

man 3 basename

译文:

时间: 2024-08-24 06:24:52

linux之basename的相关文章

关于Linux系统basename函数缺陷的思考

某模块作为前台进程独立运行时,运行命令携带命令行参数:作为某平台下守护进程子进程运行时,需要将命令行参数固化在代码里.类似如下写法: char *argv[] = {"./DslDriver", "-t", "/bin/VdslModemSco.bin"}; int argc = sizeof(argv) / sizeof(argv[0]); 随后,调用basename函数(头文件为libgen.h)解析argv[0],即"./DslD

linux命令---basename,dirname

basename:strip directory and suffix from filenames.打印路径中的文件名diename:strip non-directory suffix from file name.打印路径中的路径(除去文件名)实例: [[email protected] tmp]# basename /root/test.txt test.txt [[email protected] tmp]# dirname /root/test.txt /root [[email p

linux命令basename:去掉路径和扩展名,得到指定文件的文件名(去除文件扩展名.xxx)

1.basename的语法是: basename[选项][参数]其中: 选项:为有路径信息的文件名,如/home/test/test.txt 参数:指文件扩展名2如果在编程过程中,想取得纯粹的文件名,则该命令将非常有用.下面举两个例子: (1)需要把某个路径下的文件名赋值给变量file_name: 假设文件的路径是/home/test/test.txt,把test赋值给file_name: [c.plm@localhost ~]$ file_name=`basename /home/test/t

windows下用wubi安装ubuntu14.04

第一步,下载分区助手,分出一个空白区来(注:以wubi这种方式装ubuntu最大为30G,所以分区时分10-30G之间即可) 第二步,从ubuntu官网上下载ubuntu14.04-desktop版本的iso. 第三步,用解压缩工具打开iso文件,注意不需要全部解压,只需将根目录下的wubi.exe解压出来即可.将ubuntu14.04-desktop.iso和wubi.exe放到第一步新分出来的区. 第四步,开始安装,注意安装时要将网络断开,因为在联网状态下安装,会重新下载ubuntu14.0

【wubi】安装ubuntu14.04,并解决可能的问题

一.用wubi安装ubuntu14.04 首先你要有从官网上下载的iso文件,用压缩文件浏览程序打开,你会看到wubi.exe.在一个路径没有中文(最好)的文件夹中,将wubi.exe解压(只有这一个解压),并将iso文件放入同一个文件夹中.断网,打开wubi.exe.按照提示安装,按照提示重启. 二.临时解决方案:解决wubi的ubuntu14.04无法启动的问题 如果只是12.04的话,有上面的几步,ubuntu就成功安装了.但是14.04会出现系统挂载的问题,进入不了图形界面的系统.我们需

C语言内存使用的常见问题及解决之道

一  前言 本文所讨论的“内存”主要指(静态)数据区.堆区和栈区空间(详细的布局和描述参考<Linux虚拟地址空间布局>一文).数据区内存在程序编译时分配,该内存的生存期为程序的整个运行期间,如全局变量和static关键字所声明的静态变量.函数执行时在栈上开辟局部自动变量的储存空间,执行结束时自动释放栈区内存.堆区内存亦称动态内存,由程序在运行时调用malloc/calloc/realloc等库函数申请,并由使用者显式地调用free库函数释放.堆内存比栈内存分配容量更大,生存期由使用者决定,故

C语言通用双向循环链表操作函数集

说明 相比Linux内核链表宿主结构可有多个链表结构的优点,本函数集侧重封装性和易用性,而灵活性和效率有所降低.     可基于该函数集方便地构造栈或队列集.     本函数集暂未考虑并发保护. 一  概念 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序通过链表中的指针链接次序实现.链表由一系列存储结点组成,结点可在运行时动态生成.每个结点均由两部分组成,即存储数据元素的数据域和存储相邻结点地址的指针域.当进行插入或删除操作时,链表只需修改相关结点的指针域即可,因此相比线性

ppc64le centos7 安装confd 并结合etcd实现haproxy的高可用

1. 到http://rpmfind.net/下载以下4个包: golang-1.6.2-1.fc24.ppc64le.rpm golang-bin-1.6.2-1.fc24.ppc64le.rpm golang-src-1.6.2-1.fc24.noarch.rpm go-srpm-macros-2-3.fc23.noarch.rpm 2. yum -y install golang*.rpm 3. 设置GOPATH变量为 $HOME,即: export GOPATH=$HOME 4. 安装g

os模块目录操作详解

os模块是一个功能强大模块,主要提供操作系统相关功能接口,例如:文件目录操作,进程相关操作,执行系统命令,解析环境变量等.使用os模块,我们首先要导入: import os 下面我们对这些功能逐一介绍. 1:目录相关操作: 目录相关常用操作如下表: 方法 说明 os.getcwd() 获取当前脚本工作的目录路径 os.getcwdb() 同上,返回byte对象 os.chdir(path) 修改当前目录为path os.mkdir(path, mode=0o777, *, dir_fd=None