basename.c revision 1
1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1992-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * David Korn
1N/A * AT&T Bell Laboratories
1N/A *
1N/A * namebase pathname [suffix]
1N/A *
1N/A * print the namebase of a pathname
1N/A */
1N/A
1N/Astatic const char usage[] =
1N/A"[-?\n@(#)$Id: basename (AT&T Research) 2010-05-06 $\n]"
1N/AUSAGE_LICENSE
1N/A"[+NAME?basename - strip directory and suffix from filenames]"
1N/A"[+DESCRIPTION?\bbasename\b removes all leading directory components "
1N/A "from the file name defined by \astring\a. If the file name defined by "
1N/A "\astring\a has a suffix that ends in \asuffix\a, it is removed as "
1N/A "well.]"
1N/A"[+?If \astring\a consists solely of \b/\b characters the output will be "
1N/A "a single \b/\b unless \bPATH_LEADING_SLASHES\b returned by "
1N/A "\bgetconf\b(1) is \b1\b and \astring\a consists of multiple \b/\b "
1N/A "characters in which case \b//\b will be output. Otherwise, trailing "
1N/A "\b/\b characters are removed, and if there are any remaining \b/\b "
1N/A "characters in \astring\a, all characters up to and including the last "
1N/A "\b/\b are removed. Finally, if \asuffix\a is specified, and is "
1N/A "identical the end of \astring\a, these characters are removed. The "
1N/A "characters not removed from \astring\a will be written on a single line "
1N/A "to the standard output.]"
1N/A"[a:all?All operands are treated as \astring\a and each modified "
1N/A "pathname is printed on a separate line on the standard output.]"
1N/A"[s:suffix?All operands are treated as \astring\a and each modified "
1N/A "pathname, with \asuffix\a removed if it exists, is printed on a "
1N/A "separate line on the standard output.]:[suffix]"
1N/A"\n"
1N/A"\n string [suffix]\n"
1N/A"string ...\n"
1N/A"\n"
1N/A"[+EXIT STATUS?]"
1N/A "{"
1N/A "[+0?Successful Completion.]"
1N/A "[+>0?An error occurred.]"
1N/A "}"
1N/A"[+SEE ALSO?\bdirname\b(1), \bgetconf\b(1), \bbasename\b(3)]"
1N/A;
1N/A
1N/A
1N/A#include <cmd.h>
1N/A
1N/Astatic void namebase(Sfio_t *outfile, register char *pathname, char *suffix)
1N/A{
1N/A register char *first, *last;
1N/A register int n=0;
1N/A for(first=last=pathname; *last; last++);
1N/A /* back over trailing '/' */
1N/A if(last>first)
1N/A while(*--last=='/' && last > first);
1N/A if(last==first && *last=='/')
1N/A {
1N/A /* all '/' or "" */
1N/A if(*first=='/')
1N/A if(*++last=='/') /* keep leading // */
1N/A last++;
1N/A }
1N/A else
1N/A {
1N/A for(first=last++;first>pathname && *first!='/';first--);
1N/A if(*first=='/')
1N/A first++;
1N/A /* check for trailing suffix */
1N/A if(suffix && (n=strlen(suffix)) && n<(last-first))
1N/A {
1N/A if(memcmp(last-n,suffix,n)==0)
1N/A last -=n;
1N/A }
1N/A }
1N/A if(last>first)
1N/A sfwrite(outfile,first,last-first);
1N/A sfputc(outfile,'\n');
1N/A}
1N/A
1N/Aint
1N/Ab_basename(int argc,register char *argv[], void* context)
1N/A{
1N/A char* string;
1N/A char* suffix = 0;
1N/A int all = 0;
1N/A
1N/A cmdinit(argc, argv, context, ERROR_CATALOG, 0);
1N/A for (;;)
1N/A {
1N/A switch (optget(argv, usage))
1N/A {
1N/A case 'a':
1N/A all = 1;
1N/A continue;
1N/A case 's':
1N/A all = 1;
1N/A suffix = opt_info.arg;
1N/A continue;
1N/A case ':':
1N/A error(2, "%s", opt_info.arg);
1N/A break;
1N/A case '?':
1N/A error(ERROR_usage(2), "%s", opt_info.arg);
1N/A break;
1N/A }
1N/A break;
1N/A }
1N/A argv += opt_info.index;
1N/A argc -= opt_info.index;
1N/A if (error_info.errors || argc < 1 || !all && argc > 2)
1N/A error(ERROR_usage(2), "%s", optusage(NiL));
1N/A if (!all)
1N/A namebase(sfstdout, argv[0], argv[1]);
1N/A else
1N/A while (string = *argv++)
1N/A namebase(sfstdout, string, suffix);
1N/A return 0;
1N/A}