1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin *                                                                      *
3da2e3ebdSchin *               This software is part of the ast package               *
4*b30d1939SAndy Fiddaman *          Copyright (c) 1992-2012 AT&T Intellectual Property          *
5da2e3ebdSchin *                      and is licensed under the                       *
6*b30d1939SAndy Fiddaman *                 Eclipse Public License, Version 1.0                  *
77c2fbfb3SApril Chin *                    by AT&T Intellectual Property                     *
8da2e3ebdSchin *                                                                      *
9da2e3ebdSchin *                A copy of the License is available at                 *
10*b30d1939SAndy Fiddaman *          http://www.eclipse.org/org/documents/epl-v10.html           *
11*b30d1939SAndy Fiddaman *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
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[] =
32*b30d1939SAndy Fiddaman "[-?\n@(#)$Id: basename (AT&T Research) 2010-05-06 $\n]"
33da2e3ebdSchin USAGE_LICENSE
34da2e3ebdSchin "[+NAME?basename - strip directory and suffix from filenames]"
35da2e3ebdSchin "[+DESCRIPTION?\bbasename\b removes all leading directory components "
36*b30d1939SAndy Fiddaman     "from the file name defined by \astring\a. If the file name defined by "
37*b30d1939SAndy Fiddaman     "\astring\a has a suffix that ends in \asuffix\a, it is removed as "
38*b30d1939SAndy Fiddaman     "well.]"
39*b30d1939SAndy Fiddaman "[+?If \astring\a consists solely of \b/\b characters the output will be "
40*b30d1939SAndy Fiddaman     "a single \b/\b unless \bPATH_LEADING_SLASHES\b returned by "
41*b30d1939SAndy Fiddaman     "\bgetconf\b(1) is \b1\b and \astring\a consists of multiple \b/\b "
42*b30d1939SAndy Fiddaman     "characters in which case \b//\b will be output. Otherwise, trailing "
43*b30d1939SAndy Fiddaman     "\b/\b characters are removed, and if there are any remaining \b/\b "
44*b30d1939SAndy Fiddaman     "characters in \astring\a, all characters up to and including the last "
45*b30d1939SAndy Fiddaman     "\b/\b are removed. Finally, if \asuffix\a is specified, and is "
46*b30d1939SAndy Fiddaman     "identical the end of \astring\a, these characters are removed. The "
47*b30d1939SAndy Fiddaman     "characters not removed from \astring\a will be written on a single line "
48*b30d1939SAndy Fiddaman     "to the standard output.]"
49*b30d1939SAndy Fiddaman "[a:all?All operands are treated as \astring\a and each modified "
50*b30d1939SAndy Fiddaman     "pathname is printed on a separate line on the standard output.]"
51*b30d1939SAndy Fiddaman "[s:suffix?All operands are treated as \astring\a and each modified "
52*b30d1939SAndy Fiddaman     "pathname, with \asuffix\a removed if it exists, is printed on a "
53*b30d1939SAndy Fiddaman     "separate line on the standard output.]:[suffix]"
54da2e3ebdSchin "\n"
55da2e3ebdSchin "\n string [suffix]\n"
56*b30d1939SAndy Fiddaman "string ...\n"
57da2e3ebdSchin "\n"
58*b30d1939SAndy Fiddaman "[+EXIT STATUS?]"
59*b30d1939SAndy Fiddaman     "{"
60da2e3ebdSchin         "[+0?Successful Completion.]"
61da2e3ebdSchin         "[+>0?An error occurred.]"
62*b30d1939SAndy Fiddaman     "}"
63da2e3ebdSchin "[+SEE ALSO?\bdirname\b(1), \bgetconf\b(1), \bbasename\b(3)]"
64da2e3ebdSchin ;
65da2e3ebdSchin 
66da2e3ebdSchin 
67da2e3ebdSchin #include <cmd.h>
68da2e3ebdSchin 
namebase(Sfio_t * outfile,register char * pathname,char * suffix)69da2e3ebdSchin static void namebase(Sfio_t *outfile, register char *pathname, char *suffix)
70da2e3ebdSchin {
71da2e3ebdSchin 	register char *first, *last;
72da2e3ebdSchin 	register int n=0;
73da2e3ebdSchin 	for(first=last=pathname; *last; last++);
74da2e3ebdSchin 	/* back over trailing '/' */
75da2e3ebdSchin 	if(last>first)
76da2e3ebdSchin 		while(*--last=='/' && last > first);
77da2e3ebdSchin 	if(last==first && *last=='/')
78da2e3ebdSchin 	{
79da2e3ebdSchin 		/* all '/' or "" */
80da2e3ebdSchin 		if(*first=='/')
81da2e3ebdSchin 			if(*++last=='/')	/* keep leading // */
82da2e3ebdSchin 				last++;
83da2e3ebdSchin 	}
84da2e3ebdSchin 	else
85da2e3ebdSchin 	{
86da2e3ebdSchin 		for(first=last++;first>pathname && *first!='/';first--);
87da2e3ebdSchin 		if(*first=='/')
88da2e3ebdSchin 			first++;
89da2e3ebdSchin 		/* check for trailing suffix */
90da2e3ebdSchin 		if(suffix && (n=strlen(suffix)) && n<(last-first))
91da2e3ebdSchin 		{
92da2e3ebdSchin 			if(memcmp(last-n,suffix,n)==0)
93da2e3ebdSchin 				last -=n;
94da2e3ebdSchin 		}
95da2e3ebdSchin 	}
96da2e3ebdSchin 	if(last>first)
97da2e3ebdSchin 		sfwrite(outfile,first,last-first);
98da2e3ebdSchin 	sfputc(outfile,'\n');
99da2e3ebdSchin }
100da2e3ebdSchin 
101da2e3ebdSchin int
b_basename(int argc,register char ** argv,Shbltin_t * context)102*b30d1939SAndy Fiddaman b_basename(int argc, register char** argv, Shbltin_t* context)
103da2e3ebdSchin {
104*b30d1939SAndy Fiddaman 	char*	string;
105*b30d1939SAndy Fiddaman 	char*	suffix = 0;
106*b30d1939SAndy Fiddaman 	int	all = 0;
107da2e3ebdSchin 
108da2e3ebdSchin 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
109*b30d1939SAndy Fiddaman 	for (;;)
110da2e3ebdSchin 	{
111*b30d1939SAndy Fiddaman 		switch (optget(argv, usage))
112*b30d1939SAndy Fiddaman 		{
113*b30d1939SAndy Fiddaman 		case 'a':
114*b30d1939SAndy Fiddaman 			all = 1;
115*b30d1939SAndy Fiddaman 			continue;
116*b30d1939SAndy Fiddaman 		case 's':
117*b30d1939SAndy Fiddaman 			all = 1;
118*b30d1939SAndy Fiddaman 			suffix = opt_info.arg;
119*b30d1939SAndy Fiddaman 			continue;
120*b30d1939SAndy Fiddaman 		case ':':
121*b30d1939SAndy Fiddaman 			error(2, "%s", opt_info.arg);
122*b30d1939SAndy Fiddaman 			break;
123*b30d1939SAndy Fiddaman 		case '?':
124*b30d1939SAndy Fiddaman 			error(ERROR_usage(2), "%s", opt_info.arg);
125*b30d1939SAndy Fiddaman 			break;
126*b30d1939SAndy Fiddaman 		}
127da2e3ebdSchin 		break;
128da2e3ebdSchin 	}
129da2e3ebdSchin 	argv += opt_info.index;
130da2e3ebdSchin 	argc -= opt_info.index;
131*b30d1939SAndy Fiddaman 	if (error_info.errors || argc < 1 || !all && argc > 2)
132da2e3ebdSchin 		error(ERROR_usage(2), "%s", optusage(NiL));
133*b30d1939SAndy Fiddaman 	if (!all)
134*b30d1939SAndy Fiddaman 		namebase(sfstdout, argv[0], argv[1]);
135*b30d1939SAndy Fiddaman 	else
136*b30d1939SAndy Fiddaman 		while (string = *argv++)
137*b30d1939SAndy Fiddaman 			namebase(sfstdout, string, suffix);
138*b30d1939SAndy Fiddaman 	return 0;
139da2e3ebdSchin }
140