1 /* $OpenBSD: basename.c,v 1.6 2003/06/10 22:20:44 deraadt Exp $ */ 2 /* $NetBSD: basename.c,v 1.9 1995/09/02 05:29:46 jtc Exp $ */ 3 4 /*- 5 * Copyright (c) 1991, 1993, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 static char copyright[] = 35 "@(#) Copyright (c) 1991, 1993, 1994\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif /* not lint */ 38 39 #ifndef lint 40 #if 0 41 static char sccsid[] = "@(#)basename.c 8.4 (Berkeley) 5/4/95"; 42 #endif 43 static char rcsid[] = "$OpenBSD: basename.c,v 1.6 2003/06/10 22:20:44 deraadt Exp $"; 44 #endif /* not lint */ 45 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <locale.h> 50 #include <unistd.h> 51 52 void usage(void); 53 54 int 55 main(int argc, char *argv[]) 56 { 57 char *p; 58 59 setlocale(LC_ALL, ""); 60 61 if (argc != 2 && argc != 3) 62 usage(); 63 argc--; 64 argv++; 65 66 /* 67 * (1) If string is // it is implementation defined whether steps (2) 68 * through (5) are skipped or processed. 69 * 70 * (2) If string consists entirely of slash characters, string shall 71 * be set to a single slash character. In this case, skip steps 72 * (3) through (5). 73 */ 74 for (p = *argv;; ++p) { 75 if (!*p) { 76 if (p > *argv) 77 (void)putchar('/'); 78 (void)putchar('\n'); 79 exit(0); 80 } 81 if (*p != '/') 82 break; 83 } 84 85 /* 86 * (3) If there are any trailing slash characters in string, they 87 * shall be removed. 88 */ 89 for (; *p; ++p) 90 continue; 91 while (*--p == '/') 92 continue; 93 *++p = '\0'; 94 95 /* 96 * (4) If there are any slash characters remaining in string, the 97 * prefix of string up to an including the last slash character 98 * in string shall be removed. 99 */ 100 while (--p >= *argv) 101 if (*p == '/') 102 break; 103 ++p; 104 105 /* 106 * (5) If the suffix operand is present, is not identical to the 107 * characters remaining in string, and is identical to a suffix 108 * of the characters remaining in string, the suffix suffix 109 * shall be removed from string. 110 */ 111 if (*++argv) { 112 int suffixlen, stringlen, off; 113 114 suffixlen = strlen(*argv); 115 stringlen = strlen(p); 116 117 if (suffixlen < stringlen) { 118 off = stringlen - suffixlen; 119 if (!strcmp(p + off, *argv)) 120 p[off] = '\0'; 121 } 122 } 123 (void)puts(p); 124 exit(0); 125 } 126 127 void 128 usage(void) 129 { 130 131 (void)fprintf(stderr, "usage: basename string [suffix]\n"); 132 exit(1); 133 } 134