码迷,mamicode.com
首页 > 系统相关 > 详细

linux之basename

时间:2015-04-01 15:01:27      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
  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

译文:

 

linux之basename

标签:

原文地址:http://www.cnblogs.com/lit10050528/p/4383759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!