1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1992-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
20 ***********************************************************************/
24 * AT&T Bell Laboratories
26 * namebase pathname [suffix]
28 * print the namebase of a pathname
31 static const char usage
[] =
32 "[-?\n@(#)$Id: basename (AT&T Research) 1999-04-10 $\n]"
34 "[+NAME?basename - strip directory and suffix from filenames]"
35 "[+DESCRIPTION?\bbasename\b removes all leading directory components "
36 "from the file name defined by \astring\a. If the file name "
37 "defined by \astring\a has a suffix that ends in \asuffix\a, "
38 "it is removed as well.]"
39 "[+?If \astring\a consists solely of \b/\b characters the output will "
40 "be a single \b/\b unless \bPATH_LEADING_SLASHES\b returned by "
41 "\bgetconf\b(1) is \b1\b and \astring\a consists of multiple "
42 "\b/\b characters in which case \b//\b will be output. "
43 "Otherwise, trailing \b/\b characters are removed, and if "
44 "there are any remaining \b/\b characters in \astring\a, "
45 "all characters up to and including the last \b/\b are removed. "
46 "Finally, if \asuffix\a is specified, and is identical the end "
47 "of \astring\a, these characters are removed. The characters "
48 "not removed from \astring\a will be written to standard output.]"
50 "\n string [suffix]\n"
53 "[+0?Successful Completion.]"
54 "[+>0?An error occurred.]"
56 "[+SEE ALSO?\bdirname\b(1), \bgetconf\b(1), \bbasename\b(3)]"
62 static void namebase(Sfio_t
*outfile
, register char *pathname
, char *suffix
)
64 register char *first
, *last
;
66 for(first
=last
=pathname
; *last
; last
++);
67 /* back over trailing '/' */
69 while(*--last
=='/' && last
> first
);
70 if(last
==first
&& *last
=='/')
74 if(*++last
=='/') /* keep leading // */
79 for(first
=last
++;first
>pathname
&& *first
!='/';first
--);
82 /* check for trailing suffix */
83 if(suffix
&& (n
=strlen(suffix
)) && n
<(last
-first
))
85 if(memcmp(last
-n
,suffix
,n
)==0)
90 sfwrite(outfile
,first
,last
-first
);
95 b_basename(int argc
,register char *argv
[], void* context
)
99 cmdinit(argc
, argv
, context
, ERROR_CATALOG
, 0);
100 while (n
= optget(argv
, usage
)) switch (n
)
103 error(2, "%s", opt_info
.arg
);
106 error(ERROR_usage(2), "%s", opt_info
.arg
);
109 argv
+= opt_info
.index
;
110 argc
-= opt_info
.index
;
111 if(error_info
.errors
|| argc
< 1 || argc
> 2)
112 error(ERROR_usage(2), "%s", optusage(NiL
));
113 namebase(sfstdout
,argv
[0],argv
[1]);