14887Schin /*********************************************************************** 24887Schin * * 34887Schin * This software is part of the ast package * 4*8462SApril.Chin@Sun.COM * Copyright (c) 1992-2008 AT&T Intellectual Property * 54887Schin * and is licensed under the * 64887Schin * Common Public License, Version 1.0 * 7*8462SApril.Chin@Sun.COM * by AT&T Intellectual Property * 84887Schin * * 94887Schin * A copy of the License is available at * 104887Schin * http://www.opensource.org/licenses/cpl1.0.txt * 114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 124887Schin * * 134887Schin * Information and Software Systems Research * 144887Schin * AT&T Research * 154887Schin * Florham Park NJ * 164887Schin * * 174887Schin * Glenn Fowler <gsf@research.att.com> * 184887Schin * David Korn <dgk@research.att.com> * 194887Schin * * 204887Schin ***********************************************************************/ 214887Schin #pragma prototyped 224887Schin /* 234887Schin * David Korn 244887Schin * AT&T Bell Laboratories 254887Schin * 264887Schin * namebase pathname [suffix] 274887Schin * 284887Schin * print the namebase of a pathname 294887Schin */ 304887Schin 314887Schin static const char usage[] = 324887Schin "[-?\n@(#)$Id: basename (AT&T Research) 1999-04-10 $\n]" 334887Schin USAGE_LICENSE 344887Schin "[+NAME?basename - strip directory and suffix from filenames]" 354887Schin "[+DESCRIPTION?\bbasename\b removes all leading directory components " 364887Schin "from the file name defined by \astring\a. If the file name " 374887Schin "defined by \astring\a has a suffix that ends in \asuffix\a, " 384887Schin "it is removed as well.]" 394887Schin "[+?If \astring\a consists solely of \b/\b characters the output will " 404887Schin "be a single \b/\b unless \bPATH_LEADING_SLASHES\b returned by " 414887Schin "\bgetconf\b(1) is \b1\b and \astring\a consists of multiple " 424887Schin "\b/\b characters in which case \b//\b will be output. " 434887Schin "Otherwise, trailing \b/\b characters are removed, and if " 444887Schin "there are any remaining \b/\b characters in \astring\a, " 454887Schin "all characters up to and including the last \b/\b are removed. " 464887Schin "Finally, if \asuffix\a is specified, and is identical the end " 474887Schin "of \astring\a, these characters are removed. The characters " 484887Schin "not removed from \astring\a will be written to standard output.]" 494887Schin "\n" 504887Schin "\n string [suffix]\n" 514887Schin "\n" 524887Schin "[+EXIT STATUS?]{" 534887Schin "[+0?Successful Completion.]" 544887Schin "[+>0?An error occurred.]" 554887Schin "}" 564887Schin "[+SEE ALSO?\bdirname\b(1), \bgetconf\b(1), \bbasename\b(3)]" 574887Schin ; 584887Schin 594887Schin 604887Schin #include <cmd.h> 614887Schin 624887Schin static void namebase(Sfio_t *outfile, register char *pathname, char *suffix) 634887Schin { 644887Schin register char *first, *last; 654887Schin register int n=0; 664887Schin for(first=last=pathname; *last; last++); 674887Schin /* back over trailing '/' */ 684887Schin if(last>first) 694887Schin while(*--last=='/' && last > first); 704887Schin if(last==first && *last=='/') 714887Schin { 724887Schin /* all '/' or "" */ 734887Schin if(*first=='/') 744887Schin if(*++last=='/') /* keep leading // */ 754887Schin last++; 764887Schin } 774887Schin else 784887Schin { 794887Schin for(first=last++;first>pathname && *first!='/';first--); 804887Schin if(*first=='/') 814887Schin first++; 824887Schin /* check for trailing suffix */ 834887Schin if(suffix && (n=strlen(suffix)) && n<(last-first)) 844887Schin { 854887Schin if(memcmp(last-n,suffix,n)==0) 864887Schin last -=n; 874887Schin } 884887Schin } 894887Schin if(last>first) 904887Schin sfwrite(outfile,first,last-first); 914887Schin sfputc(outfile,'\n'); 924887Schin } 934887Schin 944887Schin int 954887Schin b_basename(int argc,register char *argv[], void* context) 964887Schin { 974887Schin register int n; 984887Schin 994887Schin cmdinit(argc, argv, context, ERROR_CATALOG, 0); 1004887Schin while (n = optget(argv, usage)) switch (n) 1014887Schin { 1024887Schin case ':': 1034887Schin error(2, "%s", opt_info.arg); 1044887Schin break; 1054887Schin case '?': 1064887Schin error(ERROR_usage(2), "%s", opt_info.arg); 1074887Schin break; 1084887Schin } 1094887Schin argv += opt_info.index; 1104887Schin argc -= opt_info.index; 1114887Schin if(error_info.errors || argc < 1 || argc > 2) 1124887Schin error(ERROR_usage(2), "%s", optusage(NiL)); 1134887Schin namebase(sfstdout,argv[0],argv[1]); 1144887Schin return(0); 1154887Schin } 1164887Schin 117