📄 dirname.3
字号:
.\" t.TH DIRNAME 3 04-Jan-2007 MinGW "Programmer's Reference Manual"..SH NAME..BR dirname ,\0 basename\- parse path name components...SH SYNOPSIS..B #include.RB < libgen.h >.PP.B char *dirname( char.BI * path.B );.br.B char *basename( char.BI * path.B );...SH DESCRIPTION.The.B dirnameand.B basenamefunctions parse a null\-terminated path name string,and split it into its.B directory nameand.B file namecomponents.Splitting is performed on the basis of the location of the.B directory separatorcharacters,which,for this MS\-Windows(\(tm) implementation,are the characters.RB \(dq / \(dqand.RB \(dq \e \(dq,each of which is interpreted as being equivalent.Additionally,if the.I secondcharacter of.I pathis a colon.RB (\(dq : \(dq),the first two characters of.I pathare interpreted as an MS\-Windows(\(tm) drive designator,which will be included in the.B directory namecomponent of.IR path ,but is never considered to form part of the.B file namecomponent...PPIn normal usage,.B dirnamereturns a pointer to a string representing the path name component of.IR path ,up to but not including the rightmost directory separator,while.B basenamereturns a pointer to the component following this separator.Any trailing directory separators present in.I pathare disregarded,when determining the rightmost separator,and, in the case of the return value from.BR dirname ,any internal sequences of recurring separator charactersare each reduced to a single such character...PPIf.I pathcontains no MS\-Windows(\(tm) drive designator,and no directory separator character,then.B dirnamereturns the string.RB \(dq . \(dq,and.B basenamereturns a copy of.IR path.If.I pathdoes commence with an MS\-Windows(\(tm) drive designator,but contains no directory separators,then.B dirnamereturns the string.RB \(dq d:. \(dq,where.RB \(dq d: \(dqrepresents the drive designator,while.B basenamereturns a copy of.IR path ,with its initial two characters,(i.e.\ the drive designator),deleted...PPIf.I pathis a NULL pointer,or is a pointer to an empty string,then both.B dirnameand.B basenamereturn the string.RB \(dq . \(dq...PPIf.I pathis the string.RB \(dq / \(dq,or the string.RB \(dq \e \(dq,both.B dirnameand.B basenamereturn the string.RB \(dq / \(dq,or the string.RB \(dq \e \(dq,respectively...PPIf.I pathcommences with.I exactlytwo directory separator characters,which must be similar,then.B dirnamewill preserve these two characters in the returned path name.This construct does not affect the string returned by.BR basename ,neither is this behaviour replicated by.BR dirname ,if.I pathincludes an MS\-Windows(\(tm) drive designator...PPIn the special case,where.I pathis specified as.I exactlytwo identical directory separator characters,with no MS\-Windows(\(tm) drive designator,and no following path name,.B dirnamereturns.I pathunchanged;.B basenamenormalises the return string to only a single character,either.RB \(dq / \(dqor.RB \(dq \e \(dq,matching the characters used to specify.IR path ...PPConcatenating the string returned by.BR dirname ,a.RB \(dq / \(dqor a.RB \(dq \e \(dq,and the string returned by.B basenameyields a complete path name...PPThe.B dirnameand.B basenamefunctions conform generally to SUSv3,extended to accommodate the handling of.RB \(dq / \(dqand.RB \(dq \e \(dqas alternative directory separator characters,and also to accommodate the likelihood of MS\-Windows(\(tm)drive designators appearing in any path name specification.The example,which follows,illustrates the conformance to SUSv3,and also the effects of the extended behaviour....SH EXAMPLE.To verify the behaviour of the.B dirnameand.B basenamefunctions,the test program defines the following function:\(em..PP.RS.nf#include <stdio.h>#include <string.h>#include <libgen.h>void result( char *path ){ char *dir = strdup( path ); char *file = strdup( path ); printf( " %-15s%-15s%-12s", path, dirname( dir ), basename( file ) ); free( dir ); free( file );}.fi.RE.PPThis illustrates the correct use of the.B dirnameand the.B basenamefunctions,with copies of the original.I pathstring being passed in the function calls.Note that the return values from each function are used immediately,in the.B printfcall,and the temporary copies of.I pathare discarded,and the associated memory is freed,before these go out of scope...PPCalling this example function illustrates the effect of each of the.B dirnameand.B basenamefunctions,for various values of.IR path .The following,taken from SUSv3,illustrate general conformance with the standard:\(em.RS.TStab(!);lB lB lBlw(15n) lw(10n) lw(10n).\0path!\0dirname!\0basename\_!\_!\_\0/usr/lib!\0/usr!\0lib\0//usr//lib//!\0//usr!\0lib\0///usr//lib//!\0/usr!\0lib\0/usr/!\0/!\0usr\0usr!\0.!\0usr\0//!\0//!\0/\0/!\0/!\0/\0.!\0.!\0.\0..!\0.!\0...TE.RE..PPSimilarly,for the case where path names are expressed using the MS\-Windows(\(tm).RB \(dq \e \(dqdirectory separator notation,calling the example function displays:\(em.RS.TStab(!);lB lB lBlw(15n) lw(10n) lw(10n).\0path!\0dirname!\0basename\_!\_!\_\0\eusr\elib!\0\eusr!\0lib\0\e\eusr\e\elib\e\e!\0\e\eusr!\0lib\0\e\e\eusr\e\elib\e\e!\0\eusr!\0lib\0\eusr\e!\0\e!\0usr\0usr!\0.!\0usr\0\e\e!\0\e\e!\0\e\0\e!\0\e!\0\e\0.!\0.!\0.\0..!\0.!\0...TE.RE..PPand,when an MS\-Windows(\(tm) drive designator is also specified,this becomes:\(em.RS.TStab(!);lB lB lBlw(15n) lw(10n) lw(10n).\0path!\0dirname!\0basename\_!\_!\_\0d:\eusr\elib!\0d:\eusr!\0lib\0d:\e\eusr\e\elib\e\e!\0d:\eusr!\0lib\0d:\e\e\eusr\e\elib\e\e!\0d:\eusr!\0lib\0d:\eusr\e!\0d:\e!\0usr\0d:usr!\0d:.!\0usr\0d:\e\e!\0d:\e!\0\e\0d:\e!\0d:\e!\0\e\0d:.!\0d:.!\0.\0d:..!\0d:.!\0...TE.RE..PPPlease note,in particular,the special handling of path names which begin with.I exactlytwo directory separator characters,and also that this special handling is suppressedwhen these two characters are dissimilar,or when an MS\-Windows(\(tm) drive designator is specified:\(em.RS.TStab(!);lB lB lBlw(15n) lw(10n) lw(10n).\0path!\0dirname!\0basename\_!\_!\_\0//usr//lib//!\0//usr!\0lib\0\e\eusr\e\elib\e\e!\0\e\eusr!\0lib\0/\eusr\e\elib\e\e!\0/usr!\0lib\0\e/usr\e\elib\e\e!\0\eusr!\0lib\0d:\e\eusr\e\elib\e\e!\0d:\eusr!\0lib\0//!\0//!\0/\0\e\e!\0\e\e!\0\e\0/\e!\0/!\0/\0\e/!\0\e!\0\e\0d:\e\e!\0d:\e!\0\e.TE.RE...SH RETURN VALUE.The.B dirnamefunction returns a pointer to a null terminated string,which represents the directory path component of the passed.I pathstring,without any trailing directory separator character,and with all internal sequences of directory separator charactersnormalised to a single separator at each level of directory nesting...PPThe.B basenamefunctionreturns a pointer to a null terminated string,which represents the rightmost element of the passed.I pathstring,with all trailing directory separator characters removed...PPIf any MS\-Windows(\(tm) drive designator is specified in the input.I pathstring,it is included in the return value of the.B dirnamefunction,but not in that of the.B basenamefunction....SH ERROR RETURNS.None....SH CAVEATS AND BUGS.The.B dirnameand.B basenamefunctions may modify the.I pathstring passed to them.Therefore, it is an error to pass a character constant as the.I pathparameter;to do so may result in memory violation errors,(segmentation faults),and consequent abnormal program termination..PPAlso note that,since the.I pathargument may be modified by the.B dirnameor the.B basenamefunction call,if you wish to preserve the original content of.IR path ,you should pass a copy to the function.Furthermore,either function may return its result in a statically allocated buffer,which may be overwritten on a subsequent function call..PPAlthough the.B dirnameand.B basenamefunctions parse path name strings,they are basically just.I stringfunctions.The presence of an MS\-Windows(\(tm) drive designator is determinedby the appearance of a colon.RB (\(dq : \(dq)as the second character of the.I pathstring,but neither function performs any checkto ensure that the first character represents a valid file system device;neither is any form of validation performed,to ensure that the remainder of the stringrepresents a valid path name....SH AUTHOR.This manpage was written for the MinGW implementation of the.B dirnameand.B basenamefunctions by Keith\ Marshall,<keithmarshall@users.sourceforge.net>.It may copied, modified and redistributed,without restriction of copyright,provided this acknowledgement of contribution bythe original author remains unchanged....\" EOF
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -