Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dirname.man « man « mingw « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 82e1a9acc76c34a4aec3fa789407a0b6ec45e947 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
.\" 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 dirname
and
.B basename
functions parse a null\-terminated path name string,
and split it into its
.B directory name
and
.B file name
components.
Splitting is performed on the basis of the location of the
.B directory separator
characters,
which,
for this MS\-Windows(\(tm) implementation,
are the characters
.RB \(dq / \(dq
and
.RB \(dq \e \(dq,
each of which is interpreted as being equivalent.
Additionally,
if the
.I second
character of
.I path
is a colon
.RB (\(dq : \(dq),
the first two characters of
.I path
are interpreted as an MS\-Windows(\(tm) drive designator,
which will be included in the
.B directory name
component of
.IR path ,
but is never considered to form part of the
.B file name
component.
.
.PP
In normal usage,
.B dirname
returns a pointer to a string representing the path name component of
.IR path ,
up to but not including the rightmost directory separator,
while
.B basename
returns a pointer to the component following this separator.
Any trailing directory separators present in
.I path
are disregarded,
when determining the rightmost separator,
and, in the case of the return value from
.BR dirname ,
any internal sequences of recurring separator characters
are each reduced to a single such character.
.
.PP
If
.I path
contains no MS\-Windows(\(tm) drive designator,
and no directory separator character,
then
.B dirname
returns the string
.RB \(dq . \(dq,
and
.B basename
returns a copy of
.IR path.
If
.I path
does commence with an MS\-Windows(\(tm) drive designator,
but contains no directory separators,
then
.B dirname
returns the string
.RB \(dq d:. \(dq,
where
.RB \(dq d: \(dq
represents the drive designator,
while
.B basename
returns a copy of
.IR path ,
with its initial two characters,
(i.e.\ the drive designator),
deleted.
.
.PP
If
.I path
is a NULL pointer,
or is a pointer to an empty string,
then both
.B dirname
and
.B basename
return the string
.RB \(dq . \(dq.
.
.PP
If
.I path
is the string
.RB \(dq / \(dq,
or the string
.RB \(dq \e \(dq,
both
.B dirname
and
.B basename
return the string
.RB \(dq / \(dq,
or the string
.RB \(dq \e \(dq,
respectively.
.
.PP
If
.I path
commences with
.I exactly
two directory separator characters,
which must be similar,
then
.B dirname
will 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 path
includes an MS\-Windows(\(tm) drive designator.
.
.PP
In the special case,
where
.I path
is specified as
.I exactly
two identical directory separator characters,
with no MS\-Windows(\(tm) drive designator,
and no following path name,
.B dirname
returns
.I path
unchanged;
.B basename
normalises the return string to only a single character,
either
.RB \(dq / \(dq
or
.RB \(dq \e \(dq,
matching the characters used to specify
.IR path .
.
.PP
Concatenating the string returned by
.BR dirname ,
a
.RB \(dq / \(dq
or a
.RB \(dq \e \(dq,
and the string returned by
.B basename
yields a complete path name.
.
.PP
The
.B dirname
and
.B basename
functions conform generally to SUSv3,
extended to accommodate the handling of
.RB \(dq / \(dq
and
.RB \(dq \e \(dq
as 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 dirname
and
.B basename
functions,
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
.PP
This illustrates the correct use of the
.B dirname
and the
.B basename
functions,
with copies of the original
.I path
string being passed in the function calls.
Note that the return values from each function are used immediately,
in the
.B printf
call,
and the temporary copies of
.I path
are discarded,
and the associated memory is freed,
before these go out of scope.
.
.PP
Calling this example function illustrates the effect of each of the
.B dirname
and
.B basename
functions,
for various values of
.IR path .
The following,
taken from SUSv3,
illustrate general conformance with the standard:\(em
.RS
.TS
tab(!);
lB      lB      lB
lw(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
.
.PP
Similarly,
for the case where path names are expressed using the MS\-Windows(\(tm)
.RB \(dq \e \(dq
directory separator notation,
calling the example function displays:\(em
.RS
.TS
tab(!);
lB      lB      lB
lw(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
.
.PP
and,
when an MS\-Windows(\(tm) drive designator is also specified,
this becomes:\(em
.RS
.TS
tab(!);
lB      lB      lB
lw(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
.
.PP
Please note,
in particular,
the special handling of path names which begin with
.I exactly
two directory separator characters,
and also that this special handling is suppressed
when these two characters are dissimilar,
or when an MS\-Windows(\(tm) drive designator is specified:\(em
.RS
.TS
tab(!);
lB      lB      lB
lw(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 dirname
function returns a pointer to a null terminated string,
which represents the directory path component of the passed
.I path
string,
without any trailing directory separator character,
and with all internal sequences of directory separator characters
normalised to a single separator at each level of directory nesting.
.
.PP
The
.B basename
function
returns a pointer to a null terminated string,
which represents the rightmost element of the passed
.I path
string,
with all trailing directory separator characters removed.
.
.PP
If any MS\-Windows(\(tm) drive designator is specified in the input
.I path
string,
it is included in the return value of the
.B dirname
function,
but not in that of the
.B basename
function.
.
.
.SH ERROR RETURNS
.
None.
.
.
.SH CAVEATS AND BUGS
.
The
.B dirname
and
.B basename
functions may modify the
.I path
string passed to them.
Therefore, it is an error to pass a character constant as the
.I path
parameter;
to do so may result in memory violation errors,
(segmentation faults),
and consequent abnormal program termination.
.PP
Also note that,
since the
.I path
argument may be modified by the
.B dirname
or the
.B basename
function 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.
.PP
Although the
.B dirname
and
.B basename
functions parse path name strings,
they are basically just
.I string
functions.
The presence of an MS\-Windows(\(tm) drive designator is determined
by the appearance of a colon
.RB (\(dq : \(dq)
as the second character of the
.I path
string,
but neither function performs any check
to 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 string
represents a valid path name.
.
.
.SH AUTHOR
.
This manpage was written for the MinGW implementation of the
.B dirname
and
.B basename
functions by Keith\ Marshall,
<keithmarshall@users.sourceforge.net>.
It may copied, modified and redistributed,
without restriction of copyright,
provided this acknowledgement of contribution by
the original author remains unchanged.
.
.
.\" EOF