xref: /titanic_50/usr/src/lib/libcmd/common/basename.c (revision 7c2fbfb345896881c631598ee3852ce9ce33fb07)
1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin *                                                                      *
3da2e3ebdSchin *               This software is part of the ast package               *
4*7c2fbfb3SApril Chin *          Copyright (c) 1992-2008 AT&T Intellectual Property          *
5da2e3ebdSchin *                      and is licensed under the                       *
6da2e3ebdSchin *                  Common Public License, Version 1.0                  *
7*7c2fbfb3SApril Chin *                    by AT&T Intellectual Property                     *
8da2e3ebdSchin *                                                                      *
9da2e3ebdSchin *                A copy of the License is available at                 *
10da2e3ebdSchin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11da2e3ebdSchin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12da2e3ebdSchin *                                                                      *
13da2e3ebdSchin *              Information and Software Systems Research               *
14da2e3ebdSchin *                            AT&T Research                             *
15da2e3ebdSchin *                           Florham Park NJ                            *
16da2e3ebdSchin *                                                                      *
17da2e3ebdSchin *                 Glenn Fowler <gsf@research.att.com>                  *
18da2e3ebdSchin *                  David Korn <dgk@research.att.com>                   *
19da2e3ebdSchin *                                                                      *
20da2e3ebdSchin ***********************************************************************/
21da2e3ebdSchin #pragma prototyped
22da2e3ebdSchin /*
23da2e3ebdSchin  * David Korn
24da2e3ebdSchin  * AT&T Bell Laboratories
25da2e3ebdSchin  *
26da2e3ebdSchin  * namebase pathname [suffix]
27da2e3ebdSchin  *
28da2e3ebdSchin  * print the namebase of a pathname
29da2e3ebdSchin  */
30da2e3ebdSchin 
31da2e3ebdSchin static const char usage[] =
32da2e3ebdSchin "[-?\n@(#)$Id: basename (AT&T Research) 1999-04-10 $\n]"
33da2e3ebdSchin USAGE_LICENSE
34da2e3ebdSchin "[+NAME?basename - strip directory and suffix from filenames]"
35da2e3ebdSchin "[+DESCRIPTION?\bbasename\b removes all leading directory components "
36da2e3ebdSchin 	"from the file name defined by \astring\a.  If the file name "
37da2e3ebdSchin 	"defined by \astring\a has a suffix that ends in \asuffix\a, "
38da2e3ebdSchin 	"it is removed as well.]"
39da2e3ebdSchin "[+?If \astring\a consists solely of \b/\b characters the output will "
40da2e3ebdSchin 	"be a single \b/\b unless \bPATH_LEADING_SLASHES\b returned by "
41da2e3ebdSchin 	"\bgetconf\b(1) is \b1\b and \astring\a consists of multiple "
42da2e3ebdSchin 	"\b/\b characters in which case \b//\b will be output.  "
43da2e3ebdSchin 	"Otherwise, trailing \b/\b characters are removed, and if "
44da2e3ebdSchin 	"there are any remaining \b/\b characters in \astring\a, "
45da2e3ebdSchin 	"all characters up to and including the last \b/\b are removed.  "
46da2e3ebdSchin 	"Finally, if \asuffix\a is specified, and is identical the end "
47da2e3ebdSchin 	"of \astring\a, these characters are removed.  The characters "
48da2e3ebdSchin 	"not removed from \astring\a will be written to standard output.]"
49da2e3ebdSchin "\n"
50da2e3ebdSchin "\n string [suffix]\n"
51da2e3ebdSchin "\n"
52da2e3ebdSchin "[+EXIT STATUS?]{"
53da2e3ebdSchin         "[+0?Successful Completion.]"
54da2e3ebdSchin         "[+>0?An error occurred.]"
55da2e3ebdSchin "}"
56da2e3ebdSchin "[+SEE ALSO?\bdirname\b(1), \bgetconf\b(1), \bbasename\b(3)]"
57da2e3ebdSchin ;
58da2e3ebdSchin 
59da2e3ebdSchin 
60da2e3ebdSchin #include <cmd.h>
61da2e3ebdSchin 
62da2e3ebdSchin static void namebase(Sfio_t *outfile, register char *pathname, char *suffix)
63da2e3ebdSchin {
64da2e3ebdSchin 	register char *first, *last;
65da2e3ebdSchin 	register int n=0;
66da2e3ebdSchin 	for(first=last=pathname; *last; last++);
67da2e3ebdSchin 	/* back over trailing '/' */
68da2e3ebdSchin 	if(last>first)
69da2e3ebdSchin 		while(*--last=='/' && last > first);
70da2e3ebdSchin 	if(last==first && *last=='/')
71da2e3ebdSchin 	{
72da2e3ebdSchin 		/* all '/' or "" */
73da2e3ebdSchin 		if(*first=='/')
74da2e3ebdSchin 			if(*++last=='/')	/* keep leading // */
75da2e3ebdSchin 				last++;
76da2e3ebdSchin 	}
77da2e3ebdSchin 	else
78da2e3ebdSchin 	{
79da2e3ebdSchin 		for(first=last++;first>pathname && *first!='/';first--);
80da2e3ebdSchin 		if(*first=='/')
81da2e3ebdSchin 			first++;
82da2e3ebdSchin 		/* check for trailing suffix */
83da2e3ebdSchin 		if(suffix && (n=strlen(suffix)) && n<(last-first))
84da2e3ebdSchin 		{
85da2e3ebdSchin 			if(memcmp(last-n,suffix,n)==0)
86da2e3ebdSchin 				last -=n;
87da2e3ebdSchin 		}
88da2e3ebdSchin 	}
89da2e3ebdSchin 	if(last>first)
90da2e3ebdSchin 		sfwrite(outfile,first,last-first);
91da2e3ebdSchin 	sfputc(outfile,'\n');
92da2e3ebdSchin }
93da2e3ebdSchin 
94da2e3ebdSchin int
95da2e3ebdSchin b_basename(int argc,register char *argv[], void* context)
96da2e3ebdSchin {
97da2e3ebdSchin 	register int  n;
98da2e3ebdSchin 
99da2e3ebdSchin 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
100da2e3ebdSchin 	while (n = optget(argv, usage)) switch (n)
101da2e3ebdSchin 	{
102da2e3ebdSchin 	case ':':
103da2e3ebdSchin 		error(2, "%s", opt_info.arg);
104da2e3ebdSchin 		break;
105da2e3ebdSchin 	case '?':
106da2e3ebdSchin 		error(ERROR_usage(2), "%s", opt_info.arg);
107da2e3ebdSchin 		break;
108da2e3ebdSchin 	}
109da2e3ebdSchin 	argv += opt_info.index;
110da2e3ebdSchin 	argc -= opt_info.index;
111da2e3ebdSchin 	if(error_info.errors || argc < 1 || argc > 2)
112da2e3ebdSchin 		error(ERROR_usage(2), "%s", optusage(NiL));
113da2e3ebdSchin 	namebase(sfstdout,argv[0],argv[1]);
114da2e3ebdSchin 	return(0);
115da2e3ebdSchin }
116da2e3ebdSchin 
117